Sign in to follow this  

Java Applet image loading help

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just bought a website to run my java applet games on, and have already faced a bunch of technical problems. My most recent is that games play with entire sets of images just not showing up. The game code is extremely simple, and the images are just retrieved with <image>=getImage(getCodeBase(), "image.GIF") commands. However, random images seem not to load. For instance, in one simple game, the large title screen and cutscene images load, the largest screen at about 10 KB. But then a 975 byte sprite didn't load. I'm not entirely sure what's going on - all the images loaded fine to the site, and when I check my files, they all show up. At first I thought that it wasn't loading any partially transparent images, but that can't be right, since one game has a non-transparent background image (50 KB) that doesn't load. If the problem is that images aren't loading before the game starts, how would I implement a loading screen? (If you need, I can copy/paste the code of the games, but be warned - it's extremely simplistic and a bit embarrassing)

Share this post


Link to post
Share on other sites
Quote:

images aren't loading before the game starts


I had this problem and found this image loading method which fixed it using a MediaTracker to wait for the image to load completely:


private Image loadImage(String imgName)
{
Image img = null;
Toolkit toolkit = Toolkit.getDefaultToolkit();
try {
img = toolkit.getImage(new URL(getCodeBase(), imgName));
MediaTracker tracker = new MediaTracker(new Panel());
tracker.addImage(img, 0);
tracker.waitForID(0);
if (tracker.statusID(0, true) != MediaTracker.COMPLETE) {
throw new RuntimeException("Unable to load image");
}
} catch (InterruptedException e) {
// won't be interrupted
}
catch (Exception e){}

return img;

}


Then to load images do <image> = loadImage("image.GIF");
instead of <image>=getImage(getCodeBase(), "image.GIF"). Hope this fixes your problem.

Share this post


Link to post
Share on other sites
Simple thing maybe:

If your webserver is unix based the case of the file name does make a difference. For example: In your applet you load "image.GIF" but the real filename is "image.gif" (note the lower case extension).

In case of doubt always rename both the code filename and the real filename to lower case. Some image processing apps append the extension in upper case.

Share this post


Link to post
Share on other sites

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this