img
FIGURE 25-2
Output from
DoubleBuffer
without (left) and
with (right) double
buf fering
Notice that it is okay to pass in a null as the fourth parameter to drawImage( ). This is
the parameter used to pass an ImageObserver object 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 the ImageObserver interface 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 about imageUpdate( ). In response to this, Sun Microsystems added a
class to java.awt called MediaTracker in a subsequent release of the JDK. A MediaTracker is an
object that will check the status of an arbitrary number of images in parallel.
To use MediaTracker, you create a new instance and use its addImage( ) method to track
the loading status of an image. addImage( ) has the following general forms:
void addImage(Image imgObj, int imgID)
void addImage(Image imgObj, int imgID, int width, int height)
Here, imgObj is the image being tracked. Its identification number is passed in imgID. 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, width and height specify
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, call checkID( ). The version used in
this chapter is shown here:
boolean checkID(int imgID)
Here, imgID specifies the ID of the image you want to check. The method returns true if all
images that have the specified ID have been loaded (or if an error or user-abort has terminated
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home