Java Applet Images will not display when run in browser

陌路散爱 提交于 2019-12-20 06:29:20

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/16847394/java-applet-images-will-not-display-when-run-in-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!