![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
This section describes how to get theImage
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 anImage
object for it: just use one of theApplet
orToolkit
getImage
methods. ThegetImage
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 theImageObserver
interface.
Note: TheImageIcon
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
.
This section discusses first theToolkit
getImage
methods and then theApplet
getImage
methods.The
Toolkit
class declares twogetImage
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 togetImage
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 twogetImage
methods:
Image getImage(URL url)
Image getImage(URL url, String name)
Only applets can use the
Applet
getImage
methods. Moreover, theApplet
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 callgetImage
from a method such asinit
.This following code examples show you how to use the
Applet
getImage
methods. See Using the AWT to Create a GUIfor an explanation of the
getCodeBase
andgetDocumentBase
methods.//In a method in anApplet
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"));
You can track image loading in two ways: theMediaTracker
class and the
ImageObserver
interface. The
MediaTracker
class is sufficient for many programs. You just create aMediaTracker
instance, tell it to track one or more images, and then ask theMediaTracker
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 ofcheckID
andcheckAll
that take a boolean argument. Setting the boolean argument totrue
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 thewaitForID
andwaitForAll
methods.The
ImageObserver
interface lets you keep even closer track of image loading thanMediaTracker
allows. TheComponent
class uses it so that components are repainted as the images they display are loaded. To use theImageObserver
interface, you implement theImageObserver
imageUpdate
method and make sure the implementing object is registered as the image observer. Usually, this registration happens when you specify anImageObserver
to thedrawImage
method, as described in a later section. TheimageUpdate
method is called whenever information about an image becomes available.If you browse the
MediaTracker
API documentation, you might notice that theComponent
class defines two useful-looking methods:checkImage
andprepareImage
. TheMediaTracker
class has made these methods largely unnecessary.
With the help of an image producer such as theMemoryImageSource
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 |