Class MediaTracker in Java

The MediaTracker class is a utility class to trace the status of the number of media objects. Media objects could include images as well as audio clips(though currently only images are supported). So MediaTracker loads and keeps track asynchronously of the status of any number of Image objects.

To use the media tracker, we need to create an instance of the MediaTracker class and then call the addImage() method for each image we want to track.

The MediaTracker class assists in the loading of multimedia objects across the network. Tracking is necessary because Java loads images in separate threads. Calls to getImage() return immediately;image loading starts only when we  call the method drawImage(). MediaTracker lets us force images to start loading before we try to display them; it also gives us information about the loading process, so we can wait until an image is fully loaded before displaying it.

Currently, MediaTracker can monitor the loading of images, but not audio, movies, or anything else.

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 in identifying unique subsets of the images that can be waited on independently. Images with a lowed id are labeled in preference to those with a higher ID number.

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, we 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).

The class structure of class MediaTracker is given as:

public class java.awt.MediaTracker extends java.lang.Object implements java.io.Serializable{
//member elements
public final static final int ABORTED;
//ABORTED=2: flag indicating the download of some media was aborted.
public final static final int COMPLETE;
//COMPLETE=8:flag indicating the download of some media was completed successfully.
public final static final int ERRORED;
//ERRORED=4: flag indicating the download of some media was encountered an error.
public final static final int LOADING;
//LOADING=1: flag indicating that some media is currently being downloaded.
//constructor
public MediaTracker(Component comp);
//creates a media tracker to track images for a given component.
//methods:
public void addImage(Image img,int id);
public synchronized void addImage(Image img,int id,int weight,int height);
public void waitForAll()throws InterruptedException;
public void waitForID(int id)throws InterruptedException;
public synchronized boolean waitForAll(long ms)throws InterruptedException;
public synchronized boolean waitForID(int id,long ms)throws InterruptedException;
public boolean checkAll();
public synchronized boolean checkAll(boolean load);
public boolean checkID(int id);
public synchronized boolean checkID(int id,boolean load);
public synchronized boolean isErrorAny();
public synchronized boolean isErrorID(int id);
public synchronized Object[] getErrorAny();
public synchronized Object[] getErrorsID(int id);
public synchronized int statusAll(boolean load);
public synchronized int statusID(int id,boolean load);
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);
} 

The details of the class structure are given as follows:

public static final int ABORTED;

public static final int ABORTED represents a flag that indicates that the loading process aborted while loading a particular image.

For example, a timeout could have happened during the download. If something ABORTED during loading, it is possible to flush() the image to force a retry.

public static final int COMPLETE;

public static final int COMPLETE represents a flag that indicates a particular image loaded successfully.

public static final int ERRORED;

public static final int ERRORED represents a flag that indicates an error occurred while a particular image was loading.

For instance, the image file might not be available from the server(invalid URL) or the file format could be invalid. If an image has ERRORED, retrying it will fail.

public static final int LOADING;

public static final int LOADING represents a flag that indicates a particular image is still loading.

If COMPLETE, ABORTED, or ERRORED is set, the image has stopped loading. If we are checking multiple images, we can OR several of these values together to form a composite. For example, if we are loading several images and want to find out about any malfunctions, call statusAll() and we can check for a return value of ABORTED | ERRORED. Constructors

public MediaTracker(Component component);

public MediaTracker(Component component) constructs a MediaTracker object to track images to be rendered onto the component.

Parameter
component – Component that eventually renders objects being tracked.

The addImage() methods add objects for the MediaTracker for tracking. When placing an object under a MediaTracker’s control, we must provide an identifier for grouping purposes. When multiple images are grouped together, we can perform operations on the entire group with a single request.

For example, we might want to wait until all the images in an animation sequence are loaded before starting the animation; in this case, assigning the same ID to all the images makes good sense. However, when multiple images are grouped together, we cannot check on the status of a single image. The moral is: if we care about the status of individual images, we need to put each into a group by itself.

In the traditional beliefs has it that the identifier also serves as a loading priority, with a lower ID meaning a higher priority. This is not completely true. Current implementations start loading lower IDs first, but at most, this is implementation-specific functionality for the JDK. Furthermore, although an object with a lower identifier might be told to start loading first, the MediaTracker does nothing to ensure that it finishes first.

public void addImage(Image image, int id);

public void addImage(Image image, int id) method instructs a MediaTracker to track the loading of image, placing the image in the group identified by id.
The addImage() method tells the MediaTracker instance that it needs to track the loading of the image. The id is used as a grouping. The image will be rendered at its actual size, without scaling.

Parameter
image – Image to track.
id – ID of a group.

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

public synchronized void addImage(Image image, int id, int width, int height) method instructs 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.

The addImage() method tells the MediaTracker instance that it needs to track the loading of the image. The id is used as a grouping. Someone will eventually render the image at a scaled size of width x height. If width and height are both -1, the image will be rendered unscaled. If we forget to notify the MediaTracker that the image will be scaled and ask the MediaTracker to waitForID(id), it is possible that the image may not be fully ready when we try to draw it.

Parameter
image – Image to track.
id – ID of a group.
width – Eventual rendering width.
height – Eventual rendering height.

public boolean checkAll();

public boolean checkAll() method determines if all images have finished loading. It determines if all images associated with the MediaTracker have finished loading. The method returns true if all images have completed loading (successfully or unsuccessfully). Since this can return true on error, you should also use isErrorAny() to check for errors. If loading has not completed, checkAll() returns false. This method does not force images to start loading.

This method returns true if images completed loading(successfully or unsuccessfully), false otherwise.

public synchronized boolean checkAll(boolean load);

public synchronized boolean checkAll(boolean load) method determines if all images have finished loading; the load parameter may be used to force images to start loading. It determines if all images associated with the MediaTracker have finished loading. If the load flag is true, any image that has not started loading yet will start. The method returns true if all images have completed loading (successfully or unsuccessfully). Since this can return true on error, you should also use isErrorAny() to check for errors. If loading has not completed, checkAll() returns false.

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

Parameter
load – Flag to force image loading to start.

public boolean checkID(int id);

public boolean checkID(int id) method determines if all images with the given ID tag have finished loading. It determines if all the images added with the id tag have finished loading. The method returns true if all images have completed loading (successfully or unsuccessfully). Since this can return true on error, we should also use isErrorID() to check for errors. If loading has not completed, checkID() returns false. This method does not
force images to start loading. It checks whether any media in the id group encountered an error while loading. If any image resulted in an error, isErrorId() returns true; if there were no errors, it returns false.

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

Parameter
id – ID of a group.

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

public synchronized boolean checkID(int id, boolean load) method determines if all images with the given ID tag have finished loading; the load parameter may be used to force images to start loading. It determines if all the images added with the id tag have finished loading. If the load flag is true, any images in the id group that have not started loading yet will start. The method returns true if all images have completed loading (successfully or
unsuccessfully). Since this can return true on error, you should also use isErrorID() to check for errors. If loading has not completed, checkID() returns false.

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

Parameter
id – ID of a group.
load – Flag to force image loading to start.

public synchronized Object[] getErrorsAny();

public synchronized Object[] getErrorsAny() method checks to see if any media encountered an error while loading.

This method returns an array of objects managed by this media tracker that encountered a loading error.

public synchronized Object[] getErrorsID(int id);

public synchronized Object[] getErrorsID(int id) method checks to see if any media with the given ID tag encountered an error while loading. It returns an array of the objects that encountered errors in the group ID during loading. If loading caused no errors, the method returns null. The return type is an Object array instead of an Image array because MediaTracker will eventually support additional media types.

This method returns an array of objects that encountered a loading error.

Parameter
id – ID of a group.

public synchronized boolean isErrorAny();

public synchronized boolean isErrorAny() method checks to see if any media monitored by this media tracker encountered an error while loading. It checks to see if any image associated with the MediaTracker encountered an error. If there was an error, the method returns true; if none, false.

This method returns true if an error occurred, false otherwise.

public synchronized boolean isErrorID(int id);

public synchronized boolean isErrorID(int id) method checks to see if any media in the given group encountered an error while loading.

This method returns true if the error happened, false otherwise.

Parameter
id – ID of a group.
Images that have finished loading are still watched by the MediaTracker. The removeImage() methods, that allow us to remove objects from the MediaTracker. Once we no longer care about an image (usually after waiting for it to load), we can remove it from the tracker. Getting rid of loaded images results in better performance because the tracker has fewer objects to check.

public synchronized void removeImage(Image image);

public synchronized void removeImage(Image image) method removes the specified image from this MediaTracker.It removes all instances of the image from its tracking list.

Parameter
image – The image to remove.

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

public synchronized void removeImage(Image image, int id) method removes the specified image from this MediaTracker. Only instances matching the given id will be removed. It removes all instances of image from group id of its tracking list.

Parameter
image – The image to remove.
id – ID of a group.

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

public synchronized void removeImage(Image image, int id, int width, int height) method removes the specified image from this MediaTracker. Only instances matching the given id and scale sizes will be removed. It removes all instances of image from group id and scale width x height of its tracking list.

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

public synchronized int statusAll(boolean load);

public synchronized int statusAll(boolean load) method checks load status of all the images monitored by this media tracker; the load parameter may be used to force images to start loading. It determines the load status of all the images associated with the MediaTracker. If this MediaTracker is watching multiple images, the results are ORed
together. If the load flag is true, any image that has not started loading yet will start. The return value is some combination of the class constants LOADING, ABORTED, ERRORED, and COMPLETE.

This method returns the MediaTracker status flags ORed together.

Parameter
load – Flag to force image loading to start.

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

public synchronized int statusID(int id, boolean load) method checks load status of all the images in the given group; the load parameter may be used to force images to start loading. It checks on the load status of the images in the id group. If there are multiple images in the group, the results are ORed together. If the load flag is true, any image in the id group that has not started loading yet will start. The return value is some combination of the class constants LOADING, ABORTED, ERRORED, and COMPLETE.

This method returns the MediaTracker status flags ORed together.

Parameter
id – ID of a group.
load – Flag to force image loading to start.

These below methods allow us to wait for a particular image, image group, all images, or a particular time period. They enable us to be sure that an image has finished trying to load prior to continuing. The fact that an image has finished loading does not imply it has successfully loaded. It is possible that an error condition arose, which caused loading to stop. We must check the status of the image (or group) for actual success.

public void waitForAll();

public void waitForAll() method waits for all the images monitored by this media tracker to load.

public synchronized boolean waitForAll(long ms);

public synchronized boolean waitForAll(long ms) method waits at most ms milliseconds for all images monitored by this media tracker to load. It blocks the current thread from running until all images controlled by this MediaTracker finish loading. If the wait is interrupted, waitForAll() throws an InterruptedException.

This method returns true if images fully loaded, false otherwise.

Parameter
ms – Time to wait for loading.

public void waitForID(int id) ;

public void waitForID(int id) method waits for images in the given group to load. It blocks the current thread from running until the images added with id finish loading. If the wait is interrupted, waitForID() throws an InterruptedException.

Parameter
id – ID of a group.

public synchronized boolean waitForID(int id, long ms) ;

public synchronized boolean waitForID(int id, long ms) method waits at most ms milliseconds for the images in the given group to load. It blocks the current thread from running until the images added with id finish loading or until ms milliseconds have passed. If all the images have loaded, waitForID() returns true; if the timer has expired, it returns false, and one or more images in the id set have not finished loading. If ms is 0, it waits until all images added with id have loaded, with no timeout. If the wait is interrupted, waitForID() throws an InterruptedException.

This method returns true if images fully loaded, false otherwise.

Parameter
id – ID of a group.
ms – Maximum time to wait for loading.

public synchronized Object[] getErrorsAny()

public synchronized Object[] getErrorsAny() method returns an array of all the objects that encountered an error
during loading. If there were no errors, the method returns null. The return type is an Object array instead of an Image array because MediaTracker will eventually support additional media types.

This method returns an array of all the objects that encountered an error during loading. If there were no errors, the method returns null. The return type is an Object array instead of an Image array because MediaTracker will eventually support additional media types.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment