The Java TM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Creating a GUI with JFC/Swing
Lesson: Working with Graphics

## Loading Images

> This section 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. The actual loading of image data normally doesn't start until the first time the program tries to paint the image.

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

> > Note: The `ImageIcon` class automatically uses a `MediaTracker` to load its image.

> Finally, this section tells you how to create images on the fly, using a class such as `MemoryImageSource`.

### Using the getImage Methods

> This section discusses first the `Toolkit` `getImage` methods and then the `Applet` `getImage` methods.

> The `Toolkit` class declares two `getImage` methods:

> - `Image getImage(URL url)`
> - `Image getImage(String filename)`

> Here are examples of using the `Toolkit` `getImage` methods. Although every Java application and applet can use these methods, applets are subject to the usual security restrictions. In particular, untrusted applets can't successfully specify a filename to `getImage` because untrusted applets can't load data from the local file system. You can find information about restrictions on untrusted applets in Security Restrictions.

> >     Toolkit toolkit = Toolkit.getDefaultToolkit();
> >     Image image1 = toolkit.getImage("imageFile.gif");
> >     Image image2 = toolkit.getImage(
> >         new URL("http://java.sun.com/graphics/people.gif"));

> The `Applet` class supplies two `getImage` methods:

> - `Image getImage(URL url)`
> - `Image getImage(URL url, String name)`

> Only applets can use the `Applet` `getImage` methods. Moreover, the `Applet` `getImage` methods don't work until the applet has a full context (`AppletContext`). For this reason, these methods do not work if called in a constructor or in a statement that declares an instance variable. You should instead call `getImage` from a method such as `init`.

> This following code examples show you how to use the `Applet` `getImage` methods. See Creating a GUI for an explanation of the `getCodeBase` and `getDocumentBase` methods.

> >     //In a method 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"));

### Requesting and Tracking Image Loading: MediaTracker and ImageObserver

> You can track image loading in two ways: 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 animation example shows two particularly useful `MediaTracker` features: requesting that the data for a group of images be loaded, and waiting for a group of images to be loaded. To request that the image data for a group of images be loaded, you can use the forms of `checkID` and `checkAll` that take a boolean argument. Setting the boolean argument to `true` starts loading the data for any images that aren't yet being loaded. Or you can request that the image data be loaded and wait for it using the `waitForID` and `waitForAll` methods.

> The `ImageObserver` interface lets you keep even closer track of image loading than `MediaTracker` allows. 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 in a later section. The `imageUpdate` method is called whenever information about an image becomes available.

> If you browse the `MediaTracker` API documentation, you might notice that the `Component` class defines two useful-looking methods: `checkImage` and `prepareImage`. The `MediaTracker` class has made these methods largely unnecessary.

### Creating Images with MemoryImageSource

> With the help of an image producer such as the `MemoryImageSource` class, you can construct images from scratch. The following 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));

| Start of Tutorial > Start of Trail > Start of Lesson | Search