Java Reference
In-Depth Information
int image - id = 1;
img = getImage (getCodeBase (), " m20.gif " );
tracker = new MediaTracker (this);
// Pass the image reference and image ID nummber.
tracker.addImage (img, image - id);
Here we pass the MediaTracker constructor a reference to the component
object and then add the image to the tracker with an ID number. We can use the
image ID to find whether a particular image (or group of images if we use the
same number for more than one image) has finished loading.
For checking on the status of an image, MediaTracker provides the method
int status (int image - id, boolean load)
If the last parameter is true , the tracker will start loading any images that are
not yet being loaded. This method returns an integer that is an OR of four flags:
1. MediaTracker.ABORTED
2. MediaTracker.COMPLETE
3. MediaTracker.ERRORED
4. MediaTracker.LOADING
The MediaTracker is especially convenient when you need to load many
images. The statusAll() method returns a similar value as above except
that it is an OR of the status of all images currently loading.
In the following snippet the run() method uses the waitForID() method
to wait for the tracker to signal that the image has loaded. (We could wait for
several images to finish loading using the waitForAll() method.) If there is
no error in the loading, the method allows the image to be painted.
public void run () {
// Wait for the image to finish loading
try {
tracker.waitForID (fImageNum);
}
catch (InterruptedException e) {}
// Check if there was a loading error
if (tracker.isErrorID (fImageNum))
fMessage="Error";
else
fShow = true;
// Draw the image if it loaded OK
if (fShow) repaint ();
} // run
 
Search WWH ::




Custom Search