简体   繁体   中英

Java Applet Images will not display when run in browser

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.

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...

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