All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----java.awt.MediaTracker
MediaTracker class is a utility class to track 
 the status of a number of media objects. Media objects could 
 include audio clips as well as images, though currently only 
 images are supported. 
 
 To use a media tracker, create an instance of  
 MediaTracker and call its addImage  
 method for each image to be tracked. In addition, each image can 
 be assigned a unique identifier. This identifier controls the 
 priority order in which the images are fetched. It can also be used 
 to identify unique subsets of the images that can be waited on 
 independently. Images with a lower ID are loaded in preference to 
 those with a higher ID number. 
 
Here is an example:
 import java.applet.Applet;
 import java.awt.Color;
 import java.awt.Image;
 import java.awt.Graphics;
 import java.awt.MediaTracker;
 public class ImageBlaster extends Applet implements Runnable {
	MediaTracker tracker;
	Image bg;
	Image anim[] = new Image[5];
	int index;
	Thread animator;
	// Get the images for the background (id == 0) 
	// and the animation frames (id == 1) 
      // and add them to the MediaTracker
	public void init() {
	    tracker = new MediaTracker(this);
	    bg = getImage(getDocumentBase(), 
                  "images/background.gif");
	    tracker.addImage(bg, 0);
	    for (int i = 0; i < 5; i++) {
		anim[i] = getImage(getDocumentBase(), 
                      "images/anim"+i+".gif");
		tracker.addImage(anim[i], 1);
	    }
	}
	// Start the animation thread.
	public void start() {
	    animator = new Thread(this);
	    animator.start();
	}
	// Stop the animation thread.
	public void stop() {
	    animator.stop();
	    animator = null;
	}
	// Run the animation thread.
	// First wait for the background image to fully load 
      // and paint.  Then wait for all of the animation 
	// frames to finish loading. Finally, loop and 
	// increment the animation frame index.
	public void run() {
	    try {
		tracker.waitForID(0);
		tracker.waitForID(1);
	    } catch (InterruptedException e) {
		return;
	    }
	    Thread me = Thread.currentThread();
	    while (animator == me) {
		try {
		    Thread.sleep(100);
		} catch (InterruptedException e) {
		    break;
		}
		synchronized (this) {
		    index++;
		    if (index >= anim.length) {
			index = 0;
		    }
		}
		repaint();
	    }
	}
	// The background image fills the frame so we 
	// don't need to clear the applet on repaints. 
      // Just call the paint method.
	public void update(Graphics g) {
	    paint(g);
	}
	// Paint a large red rectangle if there are any errors 
	// loading the images.  Otherwise always paint the 
	// background so that it appears incrementally as it 
      // is loading.  Finally, only paint the current animation 
	// frame if all of the frames (id == 1) are done loading,
	// so that we don't get partial animations.
	public void paint(Graphics g) {
	    if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
		g.setColor(Color.red);
		g.fillRect(0, 0, size().width, size().height);
		return;
	    }
	    g.drawImage(bg, 0, 0, this);
	    if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
		g.drawImage(anim[index], 10, 10, this);
	    }
	}
 }
 
 
 ABORTED
	ABORTED
   COMPLETE
	COMPLETE
   ERRORED
	ERRORED
   LOADING
	LOADING
   
 MediaTracker(Component)
	MediaTracker(Component)
   
 addImage(Image, int)
	addImage(Image, int)
   addImage(Image, int, int, int)
	addImage(Image, int, int, int)
   checkAll()
	checkAll()
   checkAll(boolean)
	checkAll(boolean)
   checkID(int)
	checkID(int)
   checkID(int, boolean)
	checkID(int, boolean)
   getErrorsAny()
	getErrorsAny()
   getErrorsID(int)
	getErrorsID(int)
   isErrorAny()
	isErrorAny()
   isErrorID(int)
	isErrorID(int)
   removeImage(Image)
	removeImage(Image)
   removeImage(Image, int)
	removeImage(Image, int)
   removeImage(Image, int, int, int)
	removeImage(Image, int, int, int)
   statusAll(boolean)
	statusAll(boolean)
   statusID(int, boolean)
	statusID(int, boolean)
   waitForAll()
	waitForAll()
   waitForAll(long)
	waitForAll(long)
   waitForID(int)
	waitForID(int)
   waitForID(int, long)
	waitForID(int, long)
   
 LOADING
LOADING
public static final int LOADING
 ABORTED
ABORTED
public static final int ABORTED
 ERRORED
ERRORED
public static final int ERRORED
 COMPLETE
COMPLETE
public static final int COMPLETE
 
 MediaTracker
MediaTracker
public MediaTracker(Component comp)
 
 addImage
addImage
 public void addImage(Image image,
                      int id)
 addImage
addImage
 public synchronized void addImage(Image image,
                                   int id,
                                   int w,
                                   int h)
 checkAll
checkAll
public boolean checkAll()
This method does not start loading the images if they are not already loading.
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 isErrorAny or isErrorID methods to 
 check for errors.
true if all images have finished loading,
                       have been aborted, or have encountered 
                       an error; false otherwise.
     checkAll
checkAll
public boolean checkAll(boolean load)
 If the value of the load flag is true, 
 then this method starts loading any images that are not yet 
 being loaded. 
 
 If there is an error while loading or scaling an image, that 
 image is considered to have finished loading. Use the 
 isErrorAny and isErrorID methods to 
 check for errors.
true, start loading any
                       images that are not yet being loaded.
    true if all images have finished loading,
                       have been aborted, or have encountered 
                       an error; false otherwise.
     isErrorAny
isErrorAny
public synchronized boolean isErrorAny()
true if any of the images tracked
                  by this media tracker had an error during 
                  loading; false otherwise.
     getErrorsAny
getErrorsAny
public synchronized Object[] getErrorsAny()
null if 
                        there are none with errors.
     waitForAll
waitForAll
public void waitForAll() throws InterruptedException
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 isErrorAny or isErrorID methods to 
 check for errors.
 waitForAll
waitForAll
public synchronized boolean waitForAll(long ms) throws InterruptedException
ms argument has passed. 
 
 If there is an error while loading or scaling an image, then  
 that image is considered to have finished loading. Use the 
 isErrorAny or isErrorID methods to 
 check for errors.
true if all images were successfully
                       loaded; false otherwise.
     statusAll
statusAll
public int statusAll(boolean load)
 Possible flags defined by the 
 MediaTracker class are LOADING, 
 ABORTED, ERRORED, and 
 COMPLETE. An image that hasn't started 
 loading has zero as its status. 
 
 If the value of load is true, then
 this method starts loading any images that are not yet being loaded.
true, start loading
                            any images that are not yet being loaded.
     checkID
checkID
public boolean checkID(int id)
This method does not start loading the images if they are not already loading.
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 isErrorAny or isErrorID methods to 
 check for errors.
true if all images have finished loading,
                       have been aborted, or have encountered 
                       an error; false otherwise.
     checkID
checkID
 public boolean checkID(int id,
                        boolean load)
 If the value of the load flag is true, 
 then this method starts loading any images that are not yet 
 being loaded. 
 
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 isErrorAny or isErrorID methods to 
 check for errors.
true, start loading any
                       images that are not yet being loaded.
    true if all images have finished loading,
                       have been aborted, or have encountered 
                       an error; false otherwise.
     isErrorID
isErrorID
public synchronized boolean isErrorID(int id)
true if any of the images with the
                          specified identifier had an error during 
                          loading; false otherwise.
     getErrorsID
getErrorsID
public synchronized Object[] getErrorsID(int id)
null if there are none with errors.
     waitForID
waitForID
public void waitForID(int id) throws InterruptedException
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 isErrorAny and isErrorID methods to 
 check for errors.
 waitForID
waitForID
 public synchronized boolean waitForID(int id,
                                       long ms) throws InterruptedException
ms 
 argument has passed. 
 
 If there is an error while loading or scaling an image, then that 
 image is considered to have finished loading. Use the 
 statusID, isErrorID, and
 isErrorAny methods to check for errors.
 statusID
statusID
 public int statusID(int id,
                     boolean load)
 Possible flags defined by the 
 MediaTracker class are LOADING, 
 ABORTED, ERRORED, and 
 COMPLETE. An image that hasn't started 
 loading has zero as its status. 
 
 If the value of load is true, then
 this method starts loading any images that are not yet being loaded.
true, start loading
                            any images that are not yet being loaded.
     removeImage
removeImage
public synchronized void removeImage(Image image)
 removeImage
removeImage
 public synchronized void removeImage(Image image,
                                      int id)
Image being tracked 
 under the specified ID are removed regardless of scale.
 removeImage
removeImage
 public synchronized void removeImage(Image image,
                                      int id,
                                      int width,
                                      int height)
All Packages Class Hierarchy This Package Previous Next Index