0

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>

3 Answers3

0

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?

samu
  • 1,936
  • 4
  • 22
  • 26
  • Ops sorry, I misreaded that javadoc: There should be first absolute URL and then relative locatioN just as you've done getImage(getDocumentBase(), "/Image.jpg". – samu May 11 '14 at 09:31
0

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:

enter image description here

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

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • It still gives me the AccessControlException error...sigh, I never knew how troublesome it would be to get an image going – user3624649 May 11 '14 at 10:33
  • How are you running your applet? From eclipse or as HTML page. – Braj May 11 '14 at 10:36
  • I have added one more link in my post. Please have a look and read comments as well. – Braj May 11 '14 at 10:38
  • I am running it as an HTML page – user3624649 May 11 '14 at 10:40
  • No dice thus far, I am using NetBeans by the way, if that makes any difference. – user3624649 May 11 '14 at 10:54
  • Is there no help with this post? – Braj May 11 '14 at 10:56
  • I don't think it helped my issue as I keep getting the access denied message. Tell me, does such a program work for you? The one I posted I mean. I want to know why it is that MINE doesn't work yet to others it does. – user3624649 May 11 '14 at 11:00
  • Yes it works. Can you try to access images from any HTTP URL? – Braj May 11 '14 at 11:01
  • Also, I don't know how to use run the program through appletviewer, I am opening it by clicking the html file (running the code on NetBeans does nothing), not sure if that has anything to do with it. – user3624649 May 11 '14 at 11:01
  • Someone else mentioned HTTP, but I don't know how to go about doing that. Google doesn't provide any good results that explain how to run it through HTTP and not the file system. – user3624649 May 11 '14 at 11:02
  • Oh I tried accessing images from an HTTP URL, it denied me access as well. – user3624649 May 11 '14 at 11:03
  • Can you try with my sample code first with HTTP URL? – Braj May 11 '14 at 11:06
  • Yes I did, gives me access denied except this time it says acess denied "java.net.SocketPermission" and not "java.io.FilePermission". It's like I don't have permission for anything. – user3624649 May 11 '14 at 11:09
  • Then its the problem with your system only. May be settings of Firewall or Network or ... – Braj May 11 '14 at 11:10
  • Hmm any idea how to fix that then? – user3624649 May 11 '14 at 11:11
  • Alright I tried the sample code, same socket permission error – user3624649 May 11 '14 at 11:15
  • Could you show the code that you have for this? Maybe I am doing something wrong. I eliminated the "package" line for example, since it is an applet I am trying to make. I have the html file in my build folder and that's where I am running it from. – user3624649 May 11 '14 at 11:17
  • I don't have a friend readily available, so I am not sure I can really test it on another computer – user3624649 May 11 '14 at 11:18
  • add your HTML file in your question itself along with the folder structure. – Braj May 11 '14 at 11:19
  • @ Braj Need 10 reputation to post pictures – user3624649 May 11 '14 at 11:28
  • share it using any link and mention link in your post. – Braj May 11 '14 at 11:29
  • Space.gif would be the image I was trying to display at the start of the question. – user3624649 May 11 '14 at 11:31
  • Try with `` for more info have a look at [Manually Coding Applet Tag, Launching Without JNLP](http://docs.oracle.com/javase/tutorial/deployment/applet/html.html) – Braj May 11 '14 at 11:46
  • Try to access [this URL](http://www.cis.upenn.edu/~bcpierce/courses/629/papers/Java-tutorial/getStarted/applet/example/Hello.html) and let me know the result. – Braj May 11 '14 at 11:51
  • It yet gives me the access denied message. Truly, I appreciate that you have aided me this far. Do you think I ought to try the getResourceAsStream method? – user3624649 May 11 '14 at 11:51
  • It said "Hello World", no image though – user3624649 May 11 '14 at 11:54
  • I have added it in my post. – Braj May 11 '14 at 11:57
  • What does it mean that I can run it but you can't? Also, I'd like to send you the code somehow, I am not sure I feel comfortable about posting it online as it is a project – user3624649 May 11 '14 at 12:00
0

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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • will `Class.getResourceAsStream` work in case of Applet? I think `getDocumentBase()` is designed for this purpose to access files form the class-path. – Braj May 11 '14 at 11:35
  • That's one of the methods I saw when I was browsing on how to place images. Thing is, I have no idea how to implement this into my code – user3624649 May 11 '14 at 11:39
  • If course getResourceAsStream works. Just put the image into the jar file in a suitable location and it will be there. – bmargulies May 11 '14 at 12:17