Working with Graphics |
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 ToolkitgetImage()
methods. ThegetImage()
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 twogetImage()
methods:The Toolkit class declares two more
public Image getImage(URL url)
public Image getImage(URL url, String name)
getImage()
methods:Below are some code examples for using the Applet
public abstract Image getImage(URL url)
public abstract Image getImage(String filename)
getImage()
methods. See Creating a GUI for an explanation of thegetCodeBase()
andgetDocumentBase()
methods. Only applets can use the AppletgetImage()
methods.Next are examples of using the Toolkit//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"));getImage()
methods. To use these methods, you must first get a Toolkit object, which you can do by invoking the Toolkit class methodgetDefaultToolkit()
. 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 class and the ImageObserver 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 thedrawImage
method, as described on the next page.) TheimageUpdate()
method is called whenever information about an image becomes available.Here is an example of implementing the ImageObserver interface's
imageUpdate()
method. This example usesimageUpate()
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 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.[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)?]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));
Working with Graphics