Applet Deployment - Images not showing

Hello everyone,

After hours of frustration, I have finally gotten my Java applet to display on my web page, but only adding to the frustration. The problem I am having now is none of my images are displaying. Only shapes, text and colors drawn by Java are showing up.

Images in my program are called like so:
Image treeimg = getImage(getDocumentBase(), “…/Images/tree.png”);

…which after researching, I found out can cause problems. If that is the case, can anyone give suggestions as how to modify this in the least painful way? I’m not even 100% sure that this is the problem.

I’m also pretty new to html but I’m pretty sure I have everything in the right folders and whatnot. The game loads fine, just no images. Maybe I need to call some parameters for the applet in the html file?

I have researched pretty thoroughly and have basically come up with nothing so any help would be very appreciated =)

Dang… Nobody?? Oh well… I ended up figuring it out after messing around a bit.

After researching this topic I have found tons of posts with people asking the same question, none of them ever really seeming to find the solution to their problem. I would expect there to be resources for this topic out there somewhere, but after hours of searching I found absolutely nothing. When searching ‘Applet deployment’ basically all that comes up, is how to embed applets in a webpage… Great… I saw person after person asking ‘Why do my images not show when run through the browser, but work fine when run in my IDE?’ I then saw people throwing around very vague answers like ‘add image parameters in html’ or ‘download this applet bundling program…’

The solution is extremely simple (at least with the problem I was having) and so I will post it here, to hopefully save someone the trouble I just went through. My Images folder for my project was a separate folder, which was called as a resource through the Java Build Path. This works fine when running the project through eclipse or command line, but does not show the images when run through a browser.

So I copied my images folder into the source folder and erased the old Images folder. Then adjusted my code to read from the correct path.

Image treeimg = getImage(getDocumentBase(), “…/Images/tree.png”);

Image treeimg = getImage(getDocumentBase(), “Images/tree.png”);

It now shows my images on the browser. I guess the reason is, when exporting an applet, files in the source folder are automatically compressed into the Jar file. So my understanding is that it is best to keep resources along with source (for Applets) I guess? I am sure there are other ways, but after hours of fuming over this, this seems like the easiest solution and works fine.

Anyways, I just hope someone else can use this info because it doesn’t seem to me that anyone else has taken the time to explain this very well. As long as you are putting everything in the correct path, this should work fine.

Have a separate folder called “res” (or whatever) and add it to your build path.

Project -> Properties -> Java Build Path -> Source -> Add folder.

This way its added to the jar when you export it.

To get at the files you should use an URL like this:

And to load an image from that url use url );

This is how I had it set up before. But I did not try adjusting my code to read from a URL. I just tried it this way and it seems to work fine also, so thank you for that =). Anybody know if one way is more reliable than the other? (URL or adding images to source folder) What I am thinking is (using URL), as long as the webpage loads, using URL would be able to find the images. Or (using source folder for images) as long as the applet loads fine the images should load fine. They seem like pretty equal solutions to me, but I am not 100% sure. Anyways I’m just glad I got it working now and thank you jonjava =)

Using the URL is better. Actually you’re not using the URL but you’re using the ClassLoader and its getResource method gives you an URL (Uniform Resource Locator) object. The URL object is a flexible way to represent a path to something as opposed to hard coded full paths.

The ClassLoader automatically looks for your resource and tries to find it in a much more flexible way than absolute paths.

Some things take an URI (Uniform Resource Identifier) which you can simply get by using the URL’s toURI() method.