Media Tracker
Many early Java developers found the Imageobserver interface far too difficult to understand and manage when there were multiple eLhage to be loaded. The developer community-asked for q simpler solution that would allow programmers to load all of their images synchronously, without having to worry about Image Tlpd Atct ). In response to this, Sun Micro systems added a class to java.awt called MediaTracker in a subsequent release of the JDK. Al\1ediaTrackcr is an object that will check the status of ‘an arbitrary number of Images in parallel. To use Media Tracker, you create a new instance and Use its addimage ) method to track the loading status of an imag7. add image ( ) has the following general forms:

void add image image illg Object img ID)

void addlmagetlrnage imgObj, int imgID, int W~dtll, int Jrl!igl/t)
Here. it /gOiJ,( ls the image being tracked. Its identification number is passed in imgID:

ID nrrnbors 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 girth 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’ checklO(int il1 lgID) Here, imgID specified The 10 of the image you want to check. The method returns true if all images that have t!te specified 10 have been loaded (or if an error or user-abort has terminated loading). Otherwise, it returns false. You can use the checkAll() method to see if all images being tracked have been loa’dcci You should use Media’I’racker when loading a group of images. If all’of the images, that you’re interested in aren’t downloaded, you can display something else to e.itertain the user until they all arrive.

Here’s an example that ioads a seven-image slide show and displays a nice bar chart of the loading progress:

~ <applet code=”TrackedlmageLoad” width=300 height=400>
* <param name=”img”
* value=”vincent+leonardo+matisse+picasso+renoir+seurat+vermeer”>
* <applet>
*  import java.util.

* import j ava, app let..

*i import java.awt.

* This example cr ates a new MediaTracker in the Inlt( ) method, and then adds each of the named images as a tracked image with addlmage( ), In the paint( ) method, it calls check ID ( ) on all of the images that we’re tracking. IE all of the images arc loaded, they are displayed, If not, a simple bar chart of the number of’imnges loaded is shown, with the names of the iully loaded images displayed the bar, Figure 23-4 shows two scencs from this applet running. One is tho bar chart, displaying that three of th images h v been 10 ded. The other io the Van Cogh self-portrait during the slide how:

[vfb id=1]

Share This