且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎么从我的Java小程序绘制图像?

更新时间:2023-11-19 21:11:16

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

 进口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下再看看下面的截图:

我怎么从我的Java小程序绘制图像?

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

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.