简体   繁体   中英

How am I supposed to draw an image from my Java Applet?

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>

According JApplet java docs method getImage(URL url, String name) should have two parameter: URL-link to picture and String name.

Is method getDocumentBase() returnig an URL-link?

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.

You must start by understanding that an Applet, unless signed, may not read from the file system. It must use either classpath resources or things fetched from the same place it was fetched from. You have to decide which of these applies to you. If the image is a fixed image, you can put it in your classpath as a resource, and use Class.getResourceAsStream. If it's a different image every time, you'll have to use HTTP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM