Packages  This Package  Prev  Next  Index  

§1.28 Class MediaTracker

public  class  java.awt.MediaTracker
    extends  java.lang.Object  (I-§1.12)
{
        // Fields
    public final static int ABORTED;	§1.28.1
    public final static int COMPLETE;	§1.28.2
    public final static int ERRORED;	§1.28.3
    public final static int LOADING;	§1.28.4

        // Constructors
    public MediaTracker(Component  comp);	§1.28.5

        // Methods
    public void addImage(Image  image, int  id);	§1.28.6
    public void addImage(Image  image, int  id, int w, int h);	§1.28.7
    public boolean checkAll();	§1.28.8
    public boolean checkAll(boolean  load);	§1.28.9
    public boolean checkID(int  id);	§1.28.10
    public boolean checkID(int  id, boolean  load);	§1.28.11
    public Object[] getErrorsAny();	§1.28.12
    public Object[] getErrorsID(int  id);	§1.28.13
    public boolean isErrorAny();	§1.28.14
    public boolean isErrorID(int  id);	§1.28.15
    public int statusAll(boolean  load);	§1.28.16
    public int statusID(int  id, boolean  load);	§1.28.17
    public void waitForAll();	§1.28.18
    public boolean waitForAll(long  ms);	§1.28.19
    public void waitForID(int  id);	§1.28.20
    public boolean waitForID(int  id, long  ms);	§1.28.21
}
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 (II-§1.28.6) 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 (II-§1.28.16)
statusID (II-§1.28.17).

COMPLETE

public final static int COMPLETE = 8
Flag indicating the download of media completed successfully.
See Also:
statusAll (II-§1.28.16)
statusID (II-§1.28.17).

ERRORED

public final static int ERRORED = 4
Flag indicating the download of some media encountered an error.
See Also:
statusAll (II-§1.28.16)
statusID (II-§1.28.17).

LOADING

public final static int LOADING = 1
Flag indicating some media is currently being loaded.
See Also:
statusAll (II-§1.28.16)
statusID (II-§1.28.17).

Constructors

MediaTracker

public MediaTracker(Component comp)
Creates a media tracker to track images for a given component.
Parameters:
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.
Parameters:
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.
Parameters:
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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Returns:
true if all images have finished loading, were aborted or encountered an error; false otherwise.
See Also:
checkID (II-§1.28.10) .

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
load - if true, start loading any images that are not yet being loaded.
Returns:
true if all images have finished loading, were aborted or encountered an error; false otherwise.
See Also:
checkID (II-§1.28.11).

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
id - the identifier of the images to check.
Returns:
true if all images have finished loading, were aborted or encountered an error; false otherwise.
See Also:
checkAll (II-§1.28.8).

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
id - the identifier of the images to check.
load - if true, start loading any images that are not yet being loaded.
Returns:
true if all images have finished loading, were aborted or encountered an error; false otherwise.
See Also:
checkAll (II-§1.28.8).

getErrorsAny

public Object[] getErrorsAny()
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 (II-§1.28.14)
getErrorsID (II-§1.28.13).

getErrorsID

public Object[] getErrorsID(int id)
Parameters:
id - the identifier of the images to check
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.
See Also:
isErrorID (II-§1.28.15)
getErrorsAny (II-§1.28.12).

isErrorAny

public boolean isErrorAny()
Returns:
true if any of the images tracked by this media tracker had an error during loading; false otherwise.
See Also:
isErrorID (II-§1.28.15)
getErrorsAny (II-§1.28.12).

isErrorID

public boolean isErrorID(int id)
Checks the error status of all of the images tracked by this media tracker with the specified ID.
Parameters:
id - the identifier of the images to check
Returns:
true if any of the images with the specified identifier had an error during loading; false otherwise.
See Also:
isErrorAny (II-§1.28.14)
getErrorsID (II-§1.28.13).

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.
Parameters:
load - if true, start loading any images that are not yet being loaded.
Returns:
the bitwise inclusive OR of the status of all of the media being tracked
See Also:
statusID (II-§1.28.17).

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 (II-§1.28.16). 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.
Parameters:
id - the identifier of the images to check
load - if true, start loading any images that are not yet being loaded.
Returns:
the bitwise inclusive OR of the status of all of the media being tracked.

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Throws
InterruptedException (I-§1.37)
If another thread has interrupted this thread.
See Also:
waitForID (II-§1.28.20).

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
ms - the number of milliseconds to wait for the loading to complete
Returns:
true if all images were successfully loaded; false otherwise.
Throws
InterruptedException (I-§1.37)
If another thread has interrupted this thread.
See Also:
waitForID (II-§1.28.21).

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
id - the identifier of the images to check
Throws
InterruptedException (I-§1.37)
If another thread has interrupted this thread.
See Also:
waitForAll (II-§1.28.18).

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 (II-§1.28.14) or isErrorID method (II-§1.28.15) to check for errors.

Parameters:
id - the identifier of the images to check
ms - the number of milliseconds to wait for the loading to complete
Returns:
true if all images were successfully loaded; false otherwise.
Throws
InterruptedException (I-§1.37)
If another thread has interrupted this thread.
See Also:
waitForAll (II-§1.28.19).

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com