我怎么从我的Java小程序绘制图像? [英] How am I supposed to draw an image from my Java Applet?

查看:246
本文介绍了我怎么从我的Java小程序绘制图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何充分EX preSS这一点,但我有可能通过10页关于这个主题谷歌链接消失了,没有一个人帮我解决我的问题。

我想这将是很简单,我只是想给图像添加到油漆函数我的Java小程序,像任何其他形状,但是这已经转向出来对我来说是一场噩梦。

问题是,每次我尝试运行它口口声声说的拒绝访问(java.io.FilePermission中的image.jpg文件的的drawImage 作用时间读)。然而,没有一个教程都提到这一点,他们曾经说的都是我应该做到以下几点:

 进口java.applet中的*。
进口java.awt中的*。图片IMG;//这些会去在内线功能
IMG =的getImage(getDocumentBase(),/ image.jpg的); //我都没有尝试过的斜线太g.drawImage(IMG,20,20,这一点);

这是他们做的,它为他们工作,但它只是不会为我工作。其他方法是迄今为止只是添加图像着想太复杂了,甚至当我通过做这些它不断给我消息拒绝访问的辛劳。还有签约,它的方法,但我真的不认为这会帮助给予的一切,我都试过了,所以恐怕它可能只是另一个浪费的努力。教程都没有,甚至告诉你有你的小应用程序签名。

我在打造的形象与类(也称为垃圾箱)文件夹在一起。

该程序看起来运行,当我包含整个文件路径,但即使这样,图像不显示。这并不是说我真的不能包括我自己的电脑的完整路径,因为那就不是工作,当我真正将其发送给其他人。

请,我只是想知道为什么它不为我工作尚未似乎完美地为别人打工。也就是说,如果有办法解决。

这是我在做什么的例子:

 进口java.applet中的*。
进口java.awt中的*。公共类JavaProject扩展JApplet的
{
    图片IMG;    公共无效的init()
    {        IMG =的getImage(getDocumentBase(),/ image.jpg的);    }    公共无效漆(图形G)
    {
        super.paint(G);        g.drawImage(IMG,20,20,这一点);    }
}

这是我的HTML文件:

 < HTML和GT;
< HEAD>
    <标题>我的第一个网页< /标题>
< /头><身体GT;
    <小程序code =JavaProject.classWIDTH =400HEIGHT =500>
    < /小程序>
< /身体GT;
< / HTML>


解决方案

试试这一个,如果的在建(又称槽)的图像文件夹中的类在一起。

 进口java.awt.Graphics;
进口java.awt.Image中;
进口的java.net.URL;进口javax.swing.JApplet中;公共类JavaProject扩展JApplet的{
    图片IMG;    公共无效的init(){
        IMG =的getImage(getDocumentBase(),图像/ 222.png);
        //请确保222.png被划归斌/图像文件夹直接
    }    @覆盖
    公共无效漆(图形G){
        更新(G);
    }    @覆盖
    公共无效更新(图形G){
        g.drawImage(IMG,20,20,这一点);    }}


与HTTP URL首先尝试

  URL myURL =新URL(\"https://www.gravatar.com/avatar/a94613cea642e6e9e2105867bc2e103e?s=32&d=identicon&r=PG&f=1\");
IMG =的getImage(myURL);

如果您使用的是Eclipse Windows下再看看下面的截图:

请看看下面后得到关于它的一些理解。

I am not sure how to fully express this but, I have probably gone through 10 pages of Google links on this topic and not one of them has helped me solve my issue.

I thought it would be simple enough, I was just trying to add an image to the paint function of my java applet, like any other shape, but this has turned out to be a nightmare for me.

The problem is that every time I try to run the drawImage function it keeps saying Access Denied ("java.io.FilePermission" "Image.jpg" "read"). Yet, none of the tutorials mention this at all, all they ever say is that I should do the following:

import java.applet.*;
import java.awt.*;

Image img;

//These would go in the paint function
img=getImage(getDocumentBase(),"/Image.jpg"); //I have tried without the slash too

g.drawImage(img,20,20,this);

This is all they do and it works for them, but it just won't work for me. Other methods are far too complex for the sake of just adding an image, and even when I go through the toil of doing those it keeps giving me the "Access Denied" message. There's also the method of "signing" it, but I really don't think that's going to help given all that I have tried, so I am afraid it might just be another wasted endeavor. None of the tutorials even tell you to have your applet signed.

I have the image in the "build" (also called bin) folder together with the classes.

The program seemed to run when I included the entire file path, but even then the image did not display. That is not to mention I can't really include the complete path from my own computer because then it wouldn't work when I actually send it to another person.

Please, I just want to know why it doesn't work for me yet seems to work perfectly for others. That, and if there's a way around this.

This is an example of what I am doing:

import java.applet.*;
import java.awt.*;

public class JavaProject extends JApplet
{
    Image img;

    public void init()
    {

        img=getImage(getDocumentBase(),"/Image.jpg");

    }



    public void paint(Graphics g)
    {
        super.paint(g);

        g.drawImage(img,20,20,this);

    }


}

This is my HTML file:

<html>
<head>
    <title> My First Web Page </title>
</head>

<body>
    <applet code="JavaProject.class" width="400" height="500">
    </applet>
</body>
</html>

解决方案

Try this one if the image in the "build" (also called bin) folder together with the classes.

import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;

import javax.swing.JApplet;

public class JavaProject extends JApplet {
    Image img;

    public void init() {
        img = getImage(getDocumentBase(), "images/222.png");
        // Please ensure that 222.png is placed under bin/images folder directly
    }

    @Override
    public void paint(Graphics g) {
        update(g);
    }

    @Override
    public void update(Graphics g) {
        g.drawImage(img, 20, 20, this);

    }

}


Try with HTTP URL first

URL myURL = new URL("https://www.gravatar.com/avatar/a94613cea642e6e9e2105867bc2e103e?s=32&d=identicon&r=PG&f=1");
img = getImage(myURL);

If you are using Eclipse under Windows then have a look at below screenshot:

Please have a look at below post to get some understanding about it.

这篇关于我怎么从我的Java小程序绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆