Package java.awt Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class MediaTracker

Fields , Constructors , Methods

public  class  java.awt.MediaTracker
    extends  java.lang.Object  
{
        // Fields
    public final static int ABORTED;	
    public final static int COMPLETE;	
    public final static int ERRORED;	
    public final static int LOADING;	

        // Constructors
    public MediaTracker(Component  comp);	

        // Methods
    public void addImage(Image  image, int  id);	
    public void addImage(Image  image, int  id, int w, int h);	
    public boolean checkAll();	
    public boolean checkAll(boolean  load);	
    public boolean checkID(int  id);	
    public boolean checkID(int  id, boolean  load);	
    public Object[] getErrorsAny();	
    public Object[] getErrorsID(int  id);	
    public boolean isErrorAny();	
    public boolean isErrorID(int  id);	
    public int statusAll(boolean  load);	
    public int statusID(int  id, boolean  load);	
    public void waitForAll();	
    public boolean waitForAll(long  ms);	
    public void waitForID(int  id);	
    public boolean waitForID(int  id, long  ms);	
}

The MediaTracker class is a utility class to trace the status of a number of media objects. Media objects could include images as well as audio clips, though currently only images are supported.

To use the media tracker, create an instance of the MediaTracker class and then call the addImage method for each image to be tracked. In addition each image can be assigned a unique identifier. The identifier controls both the priority order in which the images are fetched as well as identifying 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 
        // Media Tracker
    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 point.  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 our 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()  &&  
                  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)  &&  
                  MediaTracker.COMPLETE)  !=  0)  {
            g.drawImage(anim[index],  10,  10,  this);
        }
    }
}



Fields


ABORTED

public final static int ABORTED = 2 

Flag indicating the download of some media was aborted.

See Also: statusAll statusID .


COMPLETE

public final static int COMPLETE = 8 

Flag indicating the download of media completed successfully.

See Also: statusAll statusID .


ERRORED

public final static int ERRORED = 4 

Flag indicating the download of some media encountered an error.

See Also: statusAll statusID .


LOADING

public final static int LOADING = 1 

Flag indicating some media is currently being loaded.

See Also: statusAll statusID .


Constructors


MediaTracker

public MediaTracker(Component  comp) 

Creates a media tracker to track images for a given component.

ParameterDescription
comp the component on which the images will eventually be drawn


Methods


addImage

public void addImage(Image  image, int  id) 

Adds an image to the list of images being tracked by this media tracker. The image will eventually be rendered at its default (unscaled) size.

ParameterDescription
image the image to be tracked
id the identifier used to later track this image


addImage

public void addImage(Image  image, int  id, int  w, int  h) 

Adds a scaled image to the list of images being tracked by this media tracker. The image will eventually be rendered at the indicated width and height.

ParameterDescription
image the image to be tracked
id the identifier used to later track this image
w the width at which the image will be rendered
h the height at which the image will be rendered


checkAll

public boolean checkAll() 

Checks to see if all images being tracked by this media tracker have finished loading.

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 method or isErrorID method to check for errors.

Return Value:

Returns true if all images have finished loading, were aborted or encountered an error; false otherwise.

See Also: checkID .


checkAll

public boolean checkAll(boolean  load) 

Checks to see if all images being tracked by this media tracker have finished loading.

If the load flag is true, then start 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 method or isErrorID method to check for errors.

Return Value:

Returns true if all images have finished loading, were aborted or encountered an error; false otherwise.

ParameterDescription
load if true, start loading any images that are not yet being loaded.

See Also: checkID .


checkID

public boolean checkID(int  id) 

Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading/

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 method or isErrorID method to check for errors.

Return Value:

Returns true if all images have finished loading, were aborted or encountered an error; false otherwise.

ParameterDescription
id the identifier of the images to check.

See Also: checkAll .


checkID

public boolean checkID(int  id, boolean  load) 

Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading.

If the load flag is true, then start 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 tohave finished loading. Use the isErrorAny method or isErrorID method to check for errors.

Return Value:

Returns true if all images have finished loading, were aborted or encountered an error; false otherwise.

ParameterDescription
id the identifier of the images to check.
load if true, start loading any images that are not yet being loaded.

See Also: checkAll .


getErrorsAny

public Object[] getErrorsAny() 

Return Value:

Returns an array of media objects tracked by this media tracker that have encountered an error, or null if there are none with errors.

See Also: isErrorAny getErrorsID .


getErrorsID

public Object[] getErrorsID(int  id) 

Return Value:

Returns an array of media objects tracked by this media tracker with the specified identifer that have encountered an error, or null if there are none with errors.

ParameterDescription
id the identifier of the images to check

See Also: isErrorID getErrorsAny .


isErrorAny

public boolean isErrorAny() 

Return Value:

Returns true if any of the images tracked by this media tracker had an error during loading; false otherwise.

See Also: isErrorID getErrorsAny .


isErrorID

public boolean isErrorID(int  id) 

Checks the error status of all of the images tracked by this media tracker with the specified ID.

Return Value:

Returns true if any of the images with the specified identifier had an error during loading; false otherwise.

ParameterDescription
id the identifier of the images to check

See Also: isErrorAny getErrorsID .


statusAll

public int statusAll(boolean  load) 

Calculates and returns the bitwise inclusive OR of the status of all the media being tracked by this media tracker. The possible flags are specified by the following four constants:

An image that hasn't started loading has zero as its status.

If the load flag is true, then start loading any images that are not yet being loaded.

Return Value:

Returns the bitwise inclusive OR of the status of all of the media being tracked

ParameterDescription
load if true, start loading any images that are not yet being loaded.

See Also: statusID .


statusID

public int statusID(int  id, boolean  load) 

Calculates and returns the bitwise inclusive OR of the status of all the media tracked by this media tracker with the specified identifier. The possible flags are as above in the statusAll method . An image that hasn't started loading has zero as its status.

If the load flag is true, then start loading any images that are not yet being loaded.

Return Value:

Returns the bitwise inclusive OR of the status of all of the media being tracked.

ParameterDescription
id the identifier of the images to check
load if true, start loading any images that are not yet being loaded.


waitForAll

public void waitForAll() 
throws InterruptedException 

Starts loading all images tracked by this media tracker. This method waits until all the images being tracked have finished loading.

If there is an error while loading or scaling an image then that image is considered finished loading. Use the isErrorAny method or isErrorID method to check for errors.

Throw:

InterruptedException

If another thread has interrupted this thread.

See Also: waitForID .


waitForAll

public boolean waitForAll(long  ms) 
throws InterruptedException 

Starts loading all images tracked by this media tracker. This method waits until all the images being tracked have finished loading, or until the length of time specified in milliseconds by the ms argument have passed.

If there is an error while loading or scaling an image then that image is considered finished loading. Use the isErrorAny method or isErrorID method to check for errors.

Return Value:

Returns true if all images were successfully loaded; false otherwise.

ParameterDescription
ms the number of milliseconds to wait for the loading to complete

Throw:

InterruptedException

If another thread has interrupted this thread.

See Also: waitForID .


waitForID

public void waitForID(int  id) 
throws InterruptedException 

Starts loading all images tracked by this media tracker with the specified identifier. This method waits until all the images with the specified identifier have finished loading.

If there is an error while loading or scaling an image then that image is considered finished loading. Use the isErrorAny method or isErrorID method to check for errors.

ParameterDescription
id the identifier of the images to check

Throw:

InterruptedException

If another thread has interrupted this thread.

See Also: waitForAll .


waitForID

public boolean waitForID(int  id, long  ms) 
throws InterruptedException 

Starts loading all images tracked by this media tracker with the specified identifier. This method waits until all the images with the specified identifier have finished loading, or until the length of time specified in milliseconds by the ms argument have passed

If there is an error while loading or scaling an image then that image is considered finished loading. Use the isErrorAny method or isErrorID method to check for errors.

Return Value:

Returns true if all images were successfully loaded; false otherwise.

ParameterDescription
id the identifier of the images to check
ms the number of milliseconds to wait for the loading to complete

Throw:

InterruptedException

If another thread has interrupted this thread.

See Also: waitForAll .



Top© 1996 Sun Microsystems, Inc. All rights reserved.