NotBoosh
NotBoosh

Reputation: 1

Java applet not able to find resource image with ImageIcon unless entire path is used

I've placed the image TestIcon.png in the src folder of a Java applet (the directory is C:\Users\User\workspace\applettest2\src\TestIcon.png). Everything I've read says that I should be able to use the image simply by referencing "TestIcon.png". Example:

ImageIcon xIcon = new ImageIcon("TestIcon.png");

Howerver, running the applet in Eclipse with this code doesn't work; the image is not displayed. If I type out the image's entire path:

ImageIcon xIcon = new ImageIcon("C:\\Users\\User\\workspace\\applettest2\\src\\TestIcon.png");

then the applet displays the image properly. Isn't the default path for resources supposed to be in the "src" folder? Thanks.

Upvotes: 0

Views: 152

Answers (1)

Braj
Braj

Reputation: 46841

If you are running it inside the Applet then try with Applet#getCodeBase() and Applet#getDocumentBase

Image image = getImage(getDocumentBase(), "TestIcon.png");

Find more samples Here and Here

Upvotes: 1

Related Questions