我应该如何从我的 Java Applet 中绘制图像? [英] How am I supposed to draw an image from my Java Applet?

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

问题描述

我不确定如何充分表达这一点,但是,我可能已经浏览了 10 页有关此主题的 Google 链接,但没有一个帮助我解决了问题.

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.

我认为这很简单,我只是想像任何其他形状一样向我的 java 小程序的 paint 函数添加一个图像,但结果证明这是一场噩梦我.

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.

问题是,每次我尝试运行 drawImage 函数时,它总是说拒绝访问(java.io.FilePermission"Image.jpg"read").然而,没有一个教程提到这一点,他们只是说我应该做以下事情:

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.

我在build"(也称为 bin)文件夹中有图像和类.

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);

    }


}

这是我的 HTML 文件:

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>

推荐答案

如果 build"(也称为 bin)文件夹中的图像与类一起使用,请尝试这个.

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);

    }

}

<小时>

先尝试使用 HTTP URL


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);

如果您在 Windows 下使用 Eclipse,请看下面的屏幕截图:

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 Applet 中绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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