0

I finally managed to get my java applet to run in my browser, however I now face the issue that none of my images will display. The only thing that displays is black text that is drawn in the screen in the applet.

In order to get the applet to work at all, I had to export as a jar and sign it myself. Now, I'm wondering why it is that the images won't display. I checked and the jar file does include all the image files. Likewise, the applet runs perfectly well in Eclipse.

What could be the issue here?

mainscreen = new ImageIcon("main.png").getImage(); is the first image that should be accessed.

Additionally, all of my classes including the applet class are in packages. The images are all outside of those packages.

fvgs
  • 21,412
  • 9
  • 33
  • 48
  • How are you accessing/loading the images from your jar file? Are you using getClass().getResource() calls. Show us some of your code example where you pull the images and tell us where you placed them in the jar file. – djsumdog May 30 '13 at 23:18

1 Answers1

5

ImageIcon("main.png").getImage() suggests that you are trying to load an icon from a File, which is not the case for applets

With embedded resources you need to use getClass().getResource("/path/to/resource"). This will allow your application to search within it's class path to find the resource...

Try something more like ImageIcon(getClass().getResource("/main.png")).getImage(), assuming that the main.png resides within the root folder of the jar file...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I was just reading this in another thread and will report back in a couple minutes. – fvgs May 30 '13 at 23:31
  • Hmm ok I tried changing this and running it in Eclipse to see if it still works, but now I get a NullPointerException when trying to compile. – fvgs May 30 '13 at 23:33
  • `mainscreen = new ImageIcon(getClass().getResource("main.png")).getImage();` this is my new line which is not compiling due to a NullPointerException – fvgs May 30 '13 at 23:34
  • How can you get a `NullPointerException` when compiling? If you get it while the applet is running, it's because the image doesn't exist in the path you specified. From memory, Eclipse requires it's resources to stored in the resource folder – MadProgrammer May 30 '13 at 23:35
  • I'm still somewhat new at Eclipse, so perhaps I did mean to say running. But you're right, it was the leading /! I'm going to convert all my image statements and will post back once I try the .jar file. – fvgs May 30 '13 at 23:44
  • On a related note, when I sign the jar file using jarsigner in the CMD, I can only sign a uniquely named jar file for a key. I'm new to this, but when I tried signing Game.jar the second time, it would not allow me to do so saying that it was unable to rename something to Game.jar.orig. My solution was to use a different name for the jar file, but this is merely a workaround. I'd appreciate if you might point me in the right direction here as well. – fvgs May 30 '13 at 23:46
  • Alright, everything appears to be working now. Once again, thank you very much for the help MadProgrammer! – fvgs May 31 '13 at 00:07