Top Description Fields Constructors Methods
java.awt

public Class MediaTracker

Additional top-level classes in compilation unit: MediaEntry, ImageMediaEntry.

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.awt.image.ImageObserver, java.io.Serial, sun.awt.image.MultiResolutionToolkitImage

The 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.

Tracking an animated image might not always be useful due to the multi-part nature of animated image loading and painting, but it is supported. MediaTracker treats an animated image as completely loaded when the first frame is completely loaded. At that point, the MediaTracker signals any waiters that the image is completely loaded. If no ImageObservers are observing the image when the first frame has finished loading, the image might flush itself to conserve resources (see Image#flush()).

Here is an example of using MediaTracker:


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 = 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);
         }
     }
}
 

Author
Jim Graham
Since
1.0

Field Summary

Modifier and TypeField and Description
public static final int
ABORTED

Flag indicating that the downloading of media was aborted.

public static final int
COMPLETE

Flag indicating that the downloading of media was completed successfully.

pack-priv static final int
public static final int
ERRORED

Flag indicating that the downloading of media encountered an error.

pack-priv MediaEntry
head

The head of the list of Images that is being tracked by the MediaTracker.

public static final int
LOADING

Flag indicating that media is currently being loaded.

private static final long
serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

pack-priv Component
target

A given Component that will be tracked by a media tracker where the image will eventually be drawn.

Constructor Summary

AccessConstructor and Description
public
MediaTracker(Component
the component on which the images will eventually be drawn
comp
)

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

Method Summary

Modifier and TypeMethod and Description
public void
addImage(Image
the image to be tracked
image
,
int
an identifier used to track this image
id
)

Adds an image to the list of images being tracked by this media tracker.

public synchronized void
addImage(Image
the image to be tracked
image
,
int
an identifier that can be used to track this image
id
,
int
the width at which the image is rendered
w
,
int
the height at which the image is rendered
h
)

Adds a scaled image to the list of images being tracked by this media tracker.

private void
addImageImpl(Image image, int id, int w, int h)

public boolean

Returns:

true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
checkAll
()

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

public boolean

Returns:

true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
checkAll
(boolean
if true, start loading any images that are not yet being loaded
load
)

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

private synchronized boolean
checkAll(boolean load, boolean verify)

public boolean

Returns:

true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
checkID
(int
the identifier of the images to check
id
)

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

public boolean

Returns:

true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
checkID
(int
the identifier of the images to check
id
,
boolean
if true, start loading any images that are not yet being loaded
load
)

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

private synchronized boolean
checkID(int id, boolean load, boolean verify)

public synchronized Object[]

Returns:

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

Returns a list of all media that have encountered an error.

public synchronized Object[]

Returns:

an array of media objects tracked by this media tracker with the specified identifier that have encountered an error, or null if there are none with errors
getErrorsID
(int
the identifier of the images to check
id
)

Returns a list of media with the specified ID that have encountered an error.

private static Image
public synchronized boolean

Returns:

true if any of the images tracked by this media tracker had an error during loading; false otherwise
isErrorAny
()

Checks the error status of all of the images.

public synchronized boolean

Returns:

true if any of the images with the specified identifier had an error during loading; false otherwise
isErrorID
(int
the identifier of the images to check
id
)

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

public synchronized void
removeImage(Image
the image to be removed
image
)

Removes the specified image from this media tracker.

public synchronized void
removeImage(Image
the image to be removed
image
,
int
the tracking ID from which to remove the image
id
)

Removes the specified image from the specified tracking ID of this media tracker.

public synchronized void
removeImage(Image
the image to be removed
image
,
int
the tracking ID from which to remove the image
id
,
int
the width to remove (-1 for unscaled)
width
,
int
the height to remove (-1 for unscaled)
height
)

Removes the specified image with the specified width, height, and ID from this media tracker.

private void
private void
removeImageImpl(Image image, int id)

private void
removeImageImpl(Image image, int id, int width, int height)

pack-priv synchronized void
public int

Returns:

the bitwise inclusive OR of the status of all of the media being tracked
statusAll
(boolean
if true, start loading any images that are not yet being loaded
load
)

Calculates and returns the bitwise inclusive OR of the status of all media that are tracked by this media tracker.

private synchronized int
statusAll(boolean load, boolean verify)

public int

Returns:

the bitwise inclusive OR of the status of all of the media with the specified identifier that are being tracked
statusID
(int
the identifier of the images to check
id
,
boolean
if true, start loading any images that are not yet being loaded
load
)

Calculates and returns the bitwise inclusive OR of the status of all media with the specified identifier that are tracked by this media tracker.

private synchronized int
statusID(int id, boolean load, boolean verify)

public void
waitForAll()

Starts loading all images tracked by this media tracker.

public synchronized boolean

Returns:

true if all images were successfully loaded; false otherwise
waitForAll
(long
the number of milliseconds to wait for the loading to complete
ms
)

Starts loading all images tracked by this media tracker.

public void
waitForID(int
the identifier of the images to check
id
)

Starts loading all images tracked by this media tracker with the specified identifier.

public synchronized boolean

Returns:

true if the loading completed in time; otherwise false
waitForID
(int
the identifier of the images to check
id
,
long
the length of time, in milliseconds, to wait for the loading to complete
ms
)

Starts loading all images tracked by this media tracker with the specified identifier.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ABORTEDback to summary
public static final int ABORTED

Flag indicating that the downloading of media was aborted.

See Also
java.awt.MediaTracker#statusAll, java.awt.MediaTracker#statusID
COMPLETEback to summary
public static final int COMPLETE

Flag indicating that the downloading of media was completed successfully.

See Also
java.awt.MediaTracker#statusAll, java.awt.MediaTracker#statusID
DONEback to summary
pack-priv static final int DONE
ERROREDback to summary
public static final int ERRORED

Flag indicating that the downloading of media encountered an error.

See Also
java.awt.MediaTracker#statusAll, java.awt.MediaTracker#statusID
LOADINGback to summary
public static final int LOADING

Flag indicating that media is currently being loaded.

See Also
java.awt.MediaTracker#statusAll, java.awt.MediaTracker#statusID
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
targetback to summary
pack-priv Component target

A given Component that will be tracked by a media tracker where the image will eventually be drawn.

See Also
MediaTracker(Component)

Constructor Detail

MediaTrackerback to summary
public MediaTracker(Component comp)

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

Parameters
comp:Component

the component on which the images will eventually be drawn

Method Detail

addImageback to summary
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:Image

the image to be tracked

id:int

an identifier used to track this image

addImageback to summary
public synchronized 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:Image

the image to be tracked

id:int

an identifier that can be used to track this image

w:int

the width at which the image is rendered

h:int

the height at which the image is rendered

addImageImplback to summary
private void addImageImpl(Image image, int id, int w, int h)
checkAllback to summary
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 or isErrorID methods to check for errors.

Returns:boolean

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

See Also
java.awt.MediaTracker#checkAll(boolean), java.awt.MediaTracker#checkID, java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#isErrorID
checkAllback to summary
public boolean checkAll(boolean load)

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

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.

Parameters
load:boolean

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

Returns:boolean

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

See Also
java.awt.MediaTracker#checkID, java.awt.MediaTracker#checkAll(), java.awt.MediaTracker#isErrorAny(), java.awt.MediaTracker#isErrorID(int)
checkAllback to summary
private synchronized boolean checkAll(boolean load, boolean verify)
checkIDback to summary
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 or isErrorID methods to check for errors.

Parameters
id:int

the identifier of the images to check

Returns:boolean

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

See Also
java.awt.MediaTracker#checkID(int, boolean), java.awt.MediaTracker#checkAll(), java.awt.MediaTracker#isErrorAny(), java.awt.MediaTracker#isErrorID(int)
checkIDback to summary
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 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.

Parameters
id:int

the identifier of the images to check

load:boolean

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

Returns:boolean

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

See Also
java.awt.MediaTracker#checkID(int), java.awt.MediaTracker#checkAll(), java.awt.MediaTracker#isErrorAny(), java.awt.MediaTracker#isErrorID(int)
checkIDback to summary
private synchronized boolean checkID(int id, boolean load, boolean verify)
getErrorsAnyback to summary
public synchronized Object[] getErrorsAny()

Returns a list of all media that have encountered an error.

Returns:Object[]

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
java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#getErrorsID
getErrorsIDback to summary
public synchronized Object[] getErrorsID(int id)

Returns a list of media with the specified ID that have encountered an error.

Parameters
id:int

the identifier of the images to check

Returns:Object[]

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

See Also
java.awt.MediaTracker#isErrorID, java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#getErrorsAny
getResolutionVariantback to summary
private static Image getResolutionVariant(Image image)
isErrorAnyback to summary
public synchronized boolean isErrorAny()

Checks the error status of all of the images.

Returns:boolean

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

See Also
java.awt.MediaTracker#isErrorID, java.awt.MediaTracker#getErrorsAny
isErrorIDback to summary
public synchronized boolean isErrorID(int id)

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

Parameters
id:int

the identifier of the images to check

Returns:boolean

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

See Also
java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#getErrorsID
removeImageback to summary
public synchronized void removeImage(Image image)

Removes the specified image from this media tracker. All instances of the specified image are removed, regardless of scale or ID.

Parameters
image:Image

the image to be removed

Since
1.1
See Also
java.awt.MediaTracker#removeImage(java.awt.Image, int), java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
removeImageback to summary
public synchronized void removeImage(Image image, int id)

Removes the specified image from the specified tracking ID of this media tracker. All instances of Image being tracked under the specified ID are removed regardless of scale.

Parameters
image:Image

the image to be removed

id:int

the tracking ID from which to remove the image

Since
1.1
See Also
java.awt.MediaTracker#removeImage(java.awt.Image), java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
removeImageback to summary
public synchronized void removeImage(Image image, int id, int width, int height)

Removes the specified image with the specified width, height, and ID from this media tracker. Only the specified instance (with any duplicates) is removed.

Parameters
image:Image

the image to be removed

id:int

the tracking ID from which to remove the image

width:int

the width to remove (-1 for unscaled)

height:int

the height to remove (-1 for unscaled)

Since
1.1
See Also
java.awt.MediaTracker#removeImage(java.awt.Image), java.awt.MediaTracker#removeImage(java.awt.Image, int)
removeImageImplback to summary
private void removeImageImpl(Image image)
removeImageImplback to summary
private void removeImageImpl(Image image, int id)
removeImageImplback to summary
private void removeImageImpl(Image image, int id, int width, int height)
setDoneback to summary
pack-priv synchronized void setDone()
statusAllback to summary
public int statusAll(boolean load)

Calculates and returns the bitwise inclusive OR of the status of all media that are tracked by this media tracker.

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.

Parameters
load:boolean

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

Returns:int

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

See Also
java.awt.MediaTracker#statusID(int, boolean), java.awt.MediaTracker#LOADING, java.awt.MediaTracker#ABORTED, java.awt.MediaTracker#ERRORED, java.awt.MediaTracker#COMPLETE
statusAllback to summary
private synchronized int statusAll(boolean load, boolean verify)
statusIDback to summary
public int statusID(int id, boolean load)

Calculates and returns the bitwise inclusive OR of the status of all media with the specified identifier that are tracked by this media tracker.

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.

Parameters
id:int

the identifier of the images to check

load:boolean

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

Returns:int

the bitwise inclusive OR of the status of all of the media with the specified identifier that are being tracked

See Also
java.awt.MediaTracker#statusAll(boolean), java.awt.MediaTracker#LOADING, java.awt.MediaTracker#ABORTED, java.awt.MediaTracker#ERRORED, java.awt.MediaTracker#COMPLETE
statusIDback to summary
private synchronized int statusID(int id, boolean load, boolean verify)
waitForAllback to summary
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 to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Exceptions
InterruptedException:
if any thread has interrupted this thread
See Also
java.awt.MediaTracker#waitForID(int), java.awt.MediaTracker#waitForAll(long), java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#isErrorID
waitForAllback to summary
public synchronized 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 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.

Parameters
ms:long

the number of milliseconds to wait for the loading to complete

Returns:boolean

true if all images were successfully loaded; false otherwise

Exceptions
InterruptedException:
if any thread has interrupted this thread.
See Also
java.awt.MediaTracker#waitForID(int), java.awt.MediaTracker#isErrorAny, java.awt.MediaTracker#isErrorID
waitForIDback to summary
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 to have finished loading. Use the isErrorAny and isErrorID methods to check for errors.

Parameters
id:int

the identifier of the images to check

Exceptions
InterruptedException:
if any thread has interrupted this thread.
See Also
java.awt.MediaTracker#waitForAll, java.awt.MediaTracker#isErrorAny(), java.awt.MediaTracker#isErrorID(int)
waitForIDback to summary
public synchronized 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 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.

Parameters
id:int

the identifier of the images to check

ms:long

the length of time, in milliseconds, to wait for the loading to complete

Returns:boolean

true if the loading completed in time; otherwise false

Exceptions
InterruptedException:
if any thread has interrupted this thread.
See Also
java.awt.MediaTracker#waitForAll, java.awt.MediaTracker#waitForID(int), java.awt.MediaTracker#statusID, java.awt.MediaTracker#isErrorAny(), java.awt.MediaTracker#isErrorID(int)