762 Part II: The Java Library
Notice that it is okay to pass in anullas the fourth parameter todrawImage( ). This is
the parameter used to pass anImageObserverobject that receives notification of image
events. Since this is an image that is not being produced from a network stream, we have no
need for notification. The left snapshot in Figure 25-2 is what the applet looks like with the
mouse button not pressed. As you can see, the image was in the middle of repainting when
this snapshot was taken. The right snapshot shows how, when a mouse button is pressed,
the image is always complete and clean due to double buffering.
MediaTracker
Many early Java developers found theImageObserverinterface far too difficult to understand
and manage when there were multiple images to be loaded. The developer community asked
for a simpler solution that would allow programmers to load all of their images synchronously,
without having to worry aboutimageUpdate( ). In response to this, Sun Microsystems added a
class tojava.awtcalledMediaTrackerin a subsequent release of the JDK. AMediaTrackeris an
object that will check the status of an arbitrary number of images in parallel.
To useMediaTracker, you create a new instance and use itsaddImage( )method to track
the loading status of an image.addImage( )has the following general forms:
void addImage(ImageimgObj, intimgID)
void addImage(ImageimgObj, intimgID, intwidth, intheight)
Here,imgObjis the image being tracked. Its identification number is passed inimgID. ID
numbers do not need to be unique. You can use the same number with several images as a
means of identifying them as part of a group. In the second form,widthandheightspecify
the dimensions of the object when it is displayed.
Once you’ve registered an image, you can check whether it’s loaded, or you can wait for
it to completely load. To check the status of an image, callcheckID( ). The version used in
this chapter is shown here:
boolean checkID(intimgID)
Here,imgIDspecifies the ID of the image you want to check. The method returnstrueif all
images that have the specified ID have been loaded (or if an error or user-abort has terminated
FIGURE 25-2
Output from
DoubleBuffer
without (left) and
with (right) double
buffering