-2

EDIT: To those idiots who negged this question merely over the title, the button is clearly meant to be pressed if the question is out of the blue, without any effort put into it at all. I have researched and I have asked, and I have tried. All I am asking for is help.

It's hardly necessary to say just how much work I have put into trying to find a solution to my problem - I have asked questions, Googled, read documents, you name it, but all to no avail.

What I want to do is something I thought I could figure out within minutes: How to run images with JApplet, and use these images in the paint(Graphics g) function. I am running the JavaProject.html file from the build (also known as bin) folder, and it is from the file system, not HTTP. I did not include the "package" line in my code as well.

A recap of my journey is that the following have not worked for me:

This is my HTML file:

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

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

This method gives me the "Access Denied" "java.io.FilePermission" "Image.jpg" "read" Error. Needless to say, trying to work with images on a website does not work either. This one is one of the more frustrating ones because it works to with other people, yet not for me.

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

public class JavaProject extends JApplet
{
    Image image;

    public void init()
    {
        image=getImage(getDocumentBase(),"/Space.gif");
    }

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

        g.drawImage(image,20,20,this);
    }
}

So that one didn't work. They suggested the getResourceAsStream method, so I tried that.

Image image;
Exception lastException=null;

try    
{
      image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));
}

catch (IOException ex) 
{
      lastException=ex;
}

But this one ended up giving me the "Illegal Argument Exception" input=null! Error.

This is my file arrangement: http://oi61.tinypic.com/5ohydc.jpg

Most other methods do not really work or are just far too complex to write down here, but none seem to work. I ask then, is my last resort just to get this thing signed? I have no idea how to go about doing that, and I think it's ridiculous that I even have to go through the trouble just to display images on my JApplet.

I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience, but I would really appreciate any help. I am new to Java, so I can't really discuss much technically, but I pick up from examples rather quickly.

  • "They" have *not* done anything to Java, aside from making it secure from evil applets and [documenting any changes in behaviour at the same time](http://docs.oracle.com/javase/8/docs/api/). It's far more likely that you're not reading or understanding some part of the documentation. – nanofarad May 11 '14 at 22:23
  • My program works fine except for these lines that I am adding. If you see a problem, then, please tell me where I am going wrong. – user3624649 May 11 '14 at 22:24
  • Have you looked at the documentation I've linked to see when such exceptions might be thrown? *That* should be your first port of call, not Stack Overflow. – nanofarad May 11 '14 at 22:26
  • I have actually looked at several of those pages from Oracle, I can't seem to formulate an answer. I have tried the "Permission" method more than a few times, I have tried modifying my HTML file, it's all for naught. On forums like these, I may at least encounter someone who has had the issue before, instead of treading blindly through code that is likely to confuse me, as I am new to this. – user3624649 May 11 '14 at 22:32

2 Answers2

1

The first method you attempted does work. The only change you need to make is to remove the slash ( / ) before the file name you are attempting to use. Here's your original code (which works fine) with that one character removed:

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

public class JavaProject extends JApplet
{
    Image image;

    public void init()
    {
        image = getImage(getDocumentBase(),"Space.gif");
    }

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

        g.drawImage(image,20,20,this);
    }
}

Note that in order for this to work, your 'space.gif' image must be in the root directory of your project, alongside the Java file that references it, like in this picture:

enter image description here

See how the image is in the 'default package' directory where my JavaProject.java file is also located? Use the code above, make sure your image is in the right place, and your program will run successfully. Here's mine in a running state:

enter image description here

trevor
  • 2,300
  • 1
  • 14
  • 13
1

The reason that the second attempt is failing is that it can't find the resource at the path you gave. That causes getResourceAsStream to return null which then results in the exception message that you saw.


You say that the error message in the first case was "Access Denied" "java.io.FilePermission" "Image.jpg" "read" or something. But according to the code, you are not trying to load "Image.jpg". That might be the root of your original problem.

A related issue is that in the getResourceAsStream version, you are using the wrong absolute path. You've used the path "/Space.gif", but given where you have put the file, it should be "/javaproject/Space.gif". Alternatively, since you are calling getResourceAsStream as on a class in the same (javaproject) package that contains the file, you could also use a relative path; i.e. "Space.gif".


I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience ...

Actually, the real solution is to carefully read the documentation for the classes / methods you are using, rather than replying on random examples you found on the internet.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216