0

I am writing a Java Applet. I am using images to display content on the Applet window. I have a separate folder in which all of these images are contained. This folder is not in the src or bin folder but a separate folder. This folder is called 'assets'. Inside this folder, there is a folder called 'images'. Inside this asset folder, I have a couple sub folders. In these sub folders, I have the images I am using. I use the methods listed below to load these images and display them onto the screen. The Applet runs fine in Eclipse. The problem occurs when I try putting the Applet onto a website. I have done quite a bit of searching, but of the things I tried, none of them solved my problem. I scan an image for certain pixel colors. My code I am using for the images is listed below. When I look at my .jar file, my images seem to be in the .jar file. (I used the Unarchived and JD-GUI to view the .jar file) Please let me know what I should do.

~Rane

Method for drawing image onto the screen:

loadImg.renderImage("images/walls/lawn.png", xCoord, yCoord, imgWidth, imgHeight, graphics);

renderImage method:

    public void renderImage(String path, int x, int y, int width, int height,
        Graphics g) {

    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);
    g.drawImage(getImage(path), x, y, width, height, applet);

}

getImage method:

    public Image getImage(String path) {

    Image img;
    URL url = null;

    try {
        url = applet.getDocumentBase();
    } catch (Exception e) {
        // TODO: handle exception
    }

    img = applet.getImage(url, path);

    return img;
}

My HTML:

    <object type="application/x-java-applet" archive="Applet.jar" code="AppletGame.class" width="600" height="600"></object>

Two of the many answered that DIDN'T work for me:

http://www.java-gaming.org/index.php?topic=27203.0

Java Applet Images will not display when run in browser

Community
  • 1
  • 1
Rane
  • 191
  • 2
  • 13
  • Images need to be within the same context/server location as the applet – MadProgrammer Dec 16 '14 at 00:08
  • The images are in the .jar file of the Applet. (According to JD-GUI and The Unarchiver) – Rane Dec 16 '14 at 00:09
  • 1
    Then you should be able to use `Class#getResource` to load them. It's been awhile, but I believe `Applet#getImage` will try and load the images from the web server... – MadProgrammer Dec 16 '14 at 00:11
  • Ok. I'll try that and let you know how it goes. – Rane Dec 16 '14 at 00:17
  • `ImageIO.read(getClass().getResource(path));` ;) - See [Reading/Loading an Image](http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) for more details – MadProgrammer Dec 16 '14 at 00:18
  • Ok. I tried doing that. ("img = ImageIO.read(getClass().getResource(path));") It did not work, unfortunately. – Rane Dec 16 '14 at 00:54
  • Did it throw an exception, if so, what? Oh and try using `/images/walls/lawn.png` assuming `images` is not relative to the class which is trying to load it :P – MadProgrammer Dec 16 '14 at 00:58
  • Ok. When I loaded the Applet with the changes, it displayed a white screen. No images were displayed – Rane Dec 16 '14 at 03:10
  • 1
    Sounds like it threw an exception, you'll need to check the Java Console for details if you're not running it from an IDE – MadProgrammer Dec 16 '14 at 03:13
  • Ok. I checked the Java Console. It does throw an exception. This exception being a security exception. It calls a security control. In my Applet, I am scanning an image for pixel colors. Here, it throws this exception. – Rane Dec 16 '14 at 22:07
  • I'd need to see a working copy of the code... – MadProgrammer Dec 16 '14 at 23:03

1 Answers1

0

I figured out my problem. In my Applet, I was loading a game map by scanning an image for a certain pixel color value. On the website, the Applet was white. In Eclipse, the screen appeared as I wanted it to. The reason for this is due to a security issue with Java Applets on webpages. It would throw a security exception every time it would try to scan the image because I was using a File. I needed to use a URL. I adjusted my code, and it works fine on the website now.

I could not have done this without your help, MadProgrammer. You helped guide me into the right direction. For example, I couldn't have found the problem without your advice to view the Java Console. Thank You!

~Rane

Sources:

Java applet game has not granted permission to read images

Community
  • 1
  • 1
Rane
  • 191
  • 2
  • 13