The MediaTracker Class

The ImageObserver interface is useful for monitoring a single image, but it starts to fall apart when faced with multiple images. The java.awt.MediaTracker class can track the loading status of many different images and organize them into logical groups. In future versions of Java, it may even be able to monitor the loading of audio clips and other kinds of multimedia content. However, Sun’s been promising this since Java 1.0 and still hasn’t delivered, so I’m not holding my breath.

To use java.awt.MediaTracker, simply create an instance of MediaTracker and then use the MediaTracker’s addImage( ) method to place each Image you care about under the MediaTracker’s control. When you add an Image to a MediaTracker, you give it a numeric ID. This ID does not have to be unique; it is really a group ID that is used to organize different images into groups. Before using the image, you call a method such as checkID( ) to see whether the image is ready. Other methods let you force loading to start, discover which images in a group have failed to load successfully, wait for images to finish loading, and so on.

Note

One difference between a MediaTracker and an ImageObserver is that the MediaTracker is called before the Image is used, while an ImageObserver is called after the Image is used.

Example 9.7 is an applet that loads an image from the Net. As usual, the image is loaded from the document base, and the name of the image file is read from a <PARAM> tag; the name ...

Get Java Network Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.