Java Applet image loading help

Started by
1 comment, last by Endurion 17 years, 7 months ago
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)
Advertisement
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.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement