Previous | Next | Trail Map | Creating a User Interface | Working with Graphics


Loading Images

This page describes how to get the Image object corresponding to an image. As long as the image data is in GIF or JPEG format and you know its filename or URL, it's easy to get an Image object for it: Just use one of the Applet or Toolkit getImage() methods. The getImage() methods return immediately, without checking whether the image data exists, much less whether it's been successfully loaded.

For many programs, this invisible background loading works well. Others, though, need to keep track of the progress of the image loading. This page explains how to do so using the MediaTracker class and the ImageObserver interface.

If the image you want to load is in a format other than GIF or JPEG, you'll need to write code to parse the image data and create an Image object.

Using the getImage() Methods

The Applet class supplies two getImage() methods: The Toolkit class declares two more getImage() methods: Below are some code examples for using the Applet getImage() methods. See Creating a GUI(in the Writing Applets trail) for an explanation of the getCodeBase() and getDocumentBase() methods. Only applets can use the Applet getImage() methods.
//In an Applet subclass:
Image image1 = getImage(getCodeBase(), "imageFile.gif");
Image image2 = getImage(getDocumentBase(), "anImageFile.jpeg");
Image image3 = getImage(new URL("http://java.sun.com/graphics/people.gif"));
Next are examples of using the Toolkit getImage() methods. To use these methods, you must first get a Toolkit object, which you can do by invoking the Toolkit class method getDefaultToolkit(). Every Java application and applet can use these methods, subject to the usual security restrictions. [Link to applet security page.]
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image1 = toolkit.getImage("imageFile.gif");
Image image2 = toolkit.getImage(new URL("http://java.sun.com/graphics/people.gif"));

Tracking Image Loading: MediaTracker and ImageObserver

[What about the Component checkImage() and prepareImage() methods? I don't see those used anywhere.]

The AWT provides two ways for you to track image loading: the MediaTracker(in the API reference documentation) class and the ImageObserver(in the API reference documentation) interface. The MediaTracker class is sufficient for many programs. You just create a MediaTracker instance, tell it to track one or more images, and then ask the MediaTracker the status of those images, as needed. An example is explained in Improving the Appearance and Performance of Image Animation.

The ImageObserver interface lets you keep even closer track of image loading. The Component class uses it so that components are repainted as the images they display are loaded. To use the ImageObserver interface, you implement the ImageObserver imageUpdate() method and make sure the implementing object is registered as the image observer. (Usually, this registration happens when you specify an ImageObserver to the drawImage method, as described on the next page.) The imageUpdate() method is called whenever information about an image becomes available.

Here is an example of implementing the ImageObserver interface's imageUpdate() method. This example uses imageUpate() to position two images as soon as their size is known, and to repaint every 100 milliseconds until both images are loaded. (Here's the whole program.)

public boolean imageUpdate(Image theimg, int infoflags,
			   int x, int y, int w, int h) {
    if ((infoflags & (ERROR)) != 0) {
        errored = true;
    }
    if ((infoflags & (WIDTH | HEIGHT)) != 0) {
        positionImages();
    }
    boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
    // Repaint immediately if we are done, otherwise batch up
    // repaint requests every 100 milliseconds
    repaint(done ? 0 : 100);
    return !done;
}

Creating Images with MemoryImageSource

With the help of the MemoryImageSource(in the API reference documentation) class, you can create Image objects for formats that the AWT doesn't yet support. You can also construct images from scratch, as the following code example shows. This code example calculates a 100x100 image representing a fade from black to blue along the X axis and a fade from black to red along the Y axis.
int w = 100;
int h = 100;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
    int red = (y * 255) / (h - 1);
    for (int x = 0; x < w; x++) {
  	int blue = (x * 255) / (w - 1);
  	pix[index++] = (255 << 24) | (red << 16) | blue;
    }
}
Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
[Provide an applet that includes the above code. + one that implements a very simple image format. Do I really need to cover the java.awt.image.ImageProducer interface (which MemoryImageSource implements)?]


Previous | Next | Trail Map | Creating a User Interface | Working with Graphics