|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
This section describes how to get theImageobject 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 anImageobject for it: just use one of theAppletorToolkitgetImagemethods. ThegetImagemethods 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
MediaTrackerclass and theImageObserverinterface.
Note: TheImageIconclass automatically uses a
MediaTrackerto load its image.Finally, this section tells you how to create images on the fly, using a class such as
MemoryImageSource.
This section discusses first theToolkitgetImagemethods and then theAppletgetImagemethods.The
Toolkitclass declares twogetImagemethods:
Image getImage(URL url)
Image getImage(String filename)Here are examples of using the
ToolkitgetImagemethods. 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 togetImagebecause 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
Appletclass supplies twogetImagemethods:
Image getImage(URL url)
Image getImage(URL url, String name)Only applets can use the
AppletgetImagemethods. Moreover, theAppletgetImagemethods 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 callgetImagefrom a method such asinit.This following code examples show you how to use the
AppletgetImagemethods. See Using the AWT to Create a GUIfor an explanation of the
getCodeBaseandgetDocumentBasemethods.//In a method in anAppletsubclass: Image image1 = getImage(getCodeBase(), "imageFile.gif"); Image image2 = getImage(getDocumentBase(), "anImageFile.jpeg"); Image image3 = getImage( new URL("http://java.sun.com/graphics/people.gif"));
You can track image loading in two ways: theMediaTrackerclass and the
ImageObserverinterface. The
MediaTrackerclass is sufficient for many programs. You just create aMediaTrackerinstance, tell it to track one or more images, and then ask theMediaTrackerthe 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
MediaTrackerfeatures: 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 ofcheckIDandcheckAllthat take a boolean argument. Setting the boolean argument totruestarts 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 thewaitForIDandwaitForAllmethods.The
ImageObserverinterface lets you keep even closer track of image loading thanMediaTrackerallows. TheComponentclass uses it so that components are repainted as the images they display are loaded. To use theImageObserverinterface, you implement theImageObserverimageUpdatemethod and make sure the implementing object is registered as the image observer. Usually, this registration happens when you specify anImageObserverto thedrawImagemethod, as described in a later section. TheimageUpdatemethod is called whenever information about an image becomes available.If you browse the
MediaTrackerAPI documentation, you might notice that theComponentclass defines two useful-looking methods:checkImageandprepareImage. TheMediaTrackerclass has made these methods largely unnecessary.
With the help of an image producer such as theMemoryImageSourceclass, 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 Feedback Form | 
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.