#

| Chapter 19 java.awt Reference |

# MediaTracker

## Name

MediaTracker

##

## Description

The `MediaTracker` class assists in the loading of multimedia objects across the network. It can be used to wait until an object (or group of objects) has been loaded completely. Tracked objects are assigned to groups; if there is more than one object in a group, you can only track the behavior of the group as a whole (i.e., it isn't possible to track an individual object unless it is the only object in its group). Currently (1.0.2 and 1.1) `MediaTracker` only works for `Image` objects; future releases may extend `MediaTracker` to other multi-media types.

## Class Definition

    public abstract class java.awt.MediaTracker
        extends java.lang.Object
        implements java.io.Serializable {

      // Constants
      public static final int ABORTED;
      public static final int COMPLETE;
      public static final int ERRORED;
      public static final int LOADING;

      // Constructors
      public MediaTracker (Component component);

      // Instance Methods
      public void addImage (Image image, int id);
      public synchronized void addImage (Image image, int id, int width, int height);
      public boolean checkAll();
      public synchronized boolean checkAll (boolean load);
      public boolean checkID (int id);
      public synchronized boolean checkID (int id, boolean load);
      public synchronized Object[] getErrorsAny();
      public synchronized Object[] getErrorsID (int id);
      public synchronized boolean isErrorAny();
      public synchronized boolean isErrorID (int id);
      public synchronized void removeImage(Image image);
      public synchronized void removeImage(Image image, int id);
      public synchronized void removeImage(Image image, int id, int width, int height);
      public synchronized int statusAll (boolean load);
      public synchronized int statusID (int id, boolean load);
      public void waitForAll() throws InterruptedException;
      public synchronized boolean waitForAll (long ms) throws InterruptedException;
      public void waitForID (int id) throws InterruptedException;
      public synchronized boolean waitForID (int id, long ms) throws InterruptedException;
    }

## Constants

### ABORTED

#### public static final int ABORTED

Flag that indicates that the loading process aborted while loading a particular image.

### COMPLETE

#### public static final int COMPLETE

Flag that indicates a particular image loaded successfully.

### ERRORED

#### public static final int ERRORED

Flag that indicates an error occurred while a particular image was loading.

### LOADING

#### public static final int LOADING

Flag that indicates a particular image is still loading.

## Constructors

### MediaTracker

#### public MediaTracker (Component component)

Parameters

  component

    `Component` that eventually renders objects being tracked.

Description

  Constructs an `MediaTracker` object.

## Instance Methods

### addImage

#### public void addImage (Image image, int id)

Parameters

  image

    `Image` to track.

  id

    ID of a group.

Description

  Tells a MediaTracker to track the loading of `image`, placing the image in the group identified by `id`.

#### public synchronized void addImage (Image image, int id, int width, int height)

Parameters

  image

    Image to track.

  id

    ID of a group.

  width

    Eventual rendering width.

  height

    Eventual rendering height.

Description

  Tells a MediaTracker to track the loading of `image`, which will be scaled to the given `height` and `width`, placing the image in the group identified by `id`.

### checkAll

#### public boolean checkAll()

Returns

  `true` if images completed loading (successfully or unsuccessfully), `false` otherwise.

Description

  Determines if all images have finished loading.

#### public synchronized boolean checkAll (boolean load)

Parameters

  load

    Flag to force image loading to start.

Returns

  `true` if all images have completed loading (successfully or unsuccessfully), `false` otherwise.

Description

  Determines if all images have finished loading; the `load` parameter may be used to force images to start loading.

### checkID

#### public boolean checkID (int id)

Parameters

  id

    ID of a group.

Returns

  `true` if all images have completed loading (successfully or unsuccessfully), `false` otherwise.

Description

  Determines if all images with the given ID tag have finished loading.

#### public synchronized boolean checkID (int id, boolean load)

Parameters

  id

    ID of a group.

  load

    Flag to force image loading to start.

Returns

  `true` if all images have completed loading (successfully or unsuccessfully), `false` otherwise.

Description

  Determines if all images with the given ID tag have finished loading; the `load` parameter may be used to force images to start loading.

### getErrorsAny

#### public synchronized Object[] getErrorsAny()

Returns

  An array of objects managed by this media tracker that encountered a loading error.

Description

  Checks to see if any media encountered an error while loading.

### getErrorsID

#### public synchronized Object[] getErrorsID (int id)

Parameters

  id

    ID of a group.

Returns

  An array of objects that encountered a loading error.

Description

  Checks to see if any media with the given ID tag encountered an error while loading.

### isErrorAny

#### public synchronized boolean isErrorAny()

Returns

  `true` if an error occurred, `false` otherwise.

Description

  Checks to see if any media monitored by this media tracker encountered an error while loading.

### isErrorID

#### public synchronized boolean isErrorID (int id)

Parameters

  id

    ID of a group.

Returns

  `true` if error happened, `false` otherwise.

Description

  Checks to see if any media in the given group encountered an error while loading.

### removeImage

#### public synchronized void removeImage (Image image)

Parameters

  image

    The image to remove.

Description

  Removes the specified image from this `MediaTracker`.

#### public synchronized void removeImage (Image image, int id)

Parameters

  image

    The image to remove.

  id

    ID of a group.

Description

  Removes the specified image from this `MediaTracker`. Only instances matching the given `id` will be removed.

#### public synchronized void removeImage (Image image, int id, int width, int height)

Parameters

  image

    The image to remove.

  id

    ID of a group.

  width

    Width of the scaled image, or -1 for unscaled.

  height

    Height of the scaled image, or -1 for unscaled.

Description

  Removes the specified image from this `MediaTracker`. Only instances matching the given `id` and scale sizes will be removed.

### statusAll

#### public synchronized int statusAll (boolean load)

Parameters

  load

    Flag to force image loading to start.

Returns

  `MediaTracker` status flags ORed together.

Description

  Checks load status of all the images monitored by this media tracker; the `load` parameter may be used to force images to start loading.

### statusID

#### public synchronized int statusID (int id, boolean load)

Parameters

  id

    ID of a group.

  load

    Flag to force image loading to start.

Returns

  `MediaTracker` status flags ORed together.

Description

  Checks load status of all the images in the given group; the `load` parameter may be used to force images to start loading.

### waitForAll

#### public void waitForAll() throws InterruptedException

Throws

  InterruptedException

    If waiting interrupted.

Description

  Waits for all the images monitored by this media tracker to load.

#### public synchronized boolean waitForAll (long ms) throws InterruptedException

Parameters

  ms

    Time to wait for loading.

Throws

  InterruptedException

    If waiting interrupted.

Returns

  `true` if images fully loaded, `false` otherwise.

Description

  Waits at most `ms` milliseconds for all images monitored by this media tracker to load.

### waitForID

#### public void waitForID (int id) throws InterruptedException

Parameters

  id

    ID of a group.

Throws

  InterruptedException

    If waiting interrupted.

Description

  Waits for images in the given group to load.

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

Parameters

  id

    ID of a group.

  ms

    Maximum time to wait for loading.

Throws

  InterruptedException

    If waiting interrupted.

Returns

  `true` if images fully loaded, `false` otherwise.

Description

  Waits at most `ms` milliseconds for the images in the given group to load.

## See Also

`Component`, `Image`, `Object`

List |  | Menu