MediaTrackerpublic class MediaTracker extends Object implements SerializableThe 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 ImageObserver s are observing the image
when the first frame has finished loading,
the image might flush itself
to conserve resources
(see {@link 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);
}
}
}
|
Fields Summary |
---|
Component | targetA given Component that will be
tracked by a media tracker where the image will
eventually be drawn. | MediaEntry | headThe head of the list of Images that is being
tracked by the MediaTracker . | private static final long | serialVersionUID | public static final int | LOADINGFlag indicating that media is currently being loaded. | public static final int | ABORTEDFlag indicating that the downloading of media was aborted. | public static final int | ERROREDFlag indicating that the downloading of media encountered
an error. | public static final int | COMPLETEFlag indicating that the downloading of media was completed
successfully. | static final int | DONE |
Constructors Summary |
---|
public MediaTracker(Component comp)Creates a media tracker to track images for a given component.
target = comp;
|
Methods Summary |
---|
public void | addImage(java.awt.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.
addImage(image, id, -1, -1);
| public synchronized void | addImage(java.awt.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.
head = MediaEntry.insert(head,
new ImageMediaEntry(this, image, id, w, h));
| 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.
return checkAll(false, true);
| 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.
return checkAll(load, true);
| private synchronized boolean | checkAll(boolean load, boolean verify)
MediaEntry cur = head;
boolean done = true;
while (cur != null) {
if ((cur.getStatus(load, verify) & DONE) == 0) {
done = false;
}
cur = cur.next;
}
return done;
| 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.
return checkID(id, false, true);
| 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.
return checkID(id, load, true);
| private synchronized boolean | checkID(int id, boolean load, boolean verify)
MediaEntry cur = head;
boolean done = true;
while (cur != null) {
if (cur.getID() == id
&& (cur.getStatus(load, verify) & DONE) == 0)
{
done = false;
}
cur = cur.next;
}
return done;
| public synchronized java.lang.Object[] | getErrorsAny()Returns a list of all media that have encountered an error.
MediaEntry cur = head;
int numerrors = 0;
while (cur != null) {
if ((cur.getStatus(false, true) & ERRORED) != 0) {
numerrors++;
}
cur = cur.next;
}
if (numerrors == 0) {
return null;
}
Object errors[] = new Object[numerrors];
cur = head;
numerrors = 0;
while (cur != null) {
if ((cur.getStatus(false, false) & ERRORED) != 0) {
errors[numerrors++] = cur.getMedia();
}
cur = cur.next;
}
return errors;
| public synchronized java.lang.Object[] | getErrorsID(int id)Returns a list of media with the specified ID that
have encountered an error.
MediaEntry cur = head;
int numerrors = 0;
while (cur != null) {
if (cur.getID() == id
&& (cur.getStatus(false, true) & ERRORED) != 0)
{
numerrors++;
}
cur = cur.next;
}
if (numerrors == 0) {
return null;
}
Object errors[] = new Object[numerrors];
cur = head;
numerrors = 0;
while (cur != null) {
if (cur.getID() == id
&& (cur.getStatus(false, false) & ERRORED) != 0)
{
errors[numerrors++] = cur.getMedia();
}
cur = cur.next;
}
return errors;
| public synchronized boolean | isErrorAny()Checks the error status of all of the images.
MediaEntry cur = head;
while (cur != null) {
if ((cur.getStatus(false, true) & ERRORED) != 0) {
return true;
}
cur = cur.next;
}
return false;
| public synchronized boolean | isErrorID(int id)Checks the error status of all of the images tracked by this
media tracker with the specified identifier.
MediaEntry cur = head;
while (cur != null) {
if (cur.getID() == id
&& (cur.getStatus(false, true) & ERRORED) != 0)
{
return true;
}
cur = cur.next;
}
return false;
| public synchronized void | removeImage(java.awt.Image image)Removes the specified image from this media tracker.
All instances of the specified image are removed,
regardless of scale or ID.
MediaEntry cur = head;
MediaEntry prev = null;
while (cur != null) {
MediaEntry next = cur.next;
if (cur.getMedia() == image) {
if (prev == null) {
head = next;
} else {
prev.next = next;
}
cur.cancel();
} else {
prev = cur;
}
cur = next;
}
notifyAll(); // Notify in case remaining images are "done".
| public synchronized void | removeImage(java.awt.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.
MediaEntry cur = head;
MediaEntry prev = null;
while (cur != null) {
MediaEntry next = cur.next;
if (cur.getID() == id && cur.getMedia() == image) {
if (prev == null) {
head = next;
} else {
prev.next = next;
}
cur.cancel();
} else {
prev = cur;
}
cur = next;
}
notifyAll(); // Notify in case remaining images are "done".
| public synchronized void | removeImage(java.awt.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.
MediaEntry cur = head;
MediaEntry prev = null;
while (cur != null) {
MediaEntry next = cur.next;
if (cur.getID() == id && cur instanceof ImageMediaEntry
&& ((ImageMediaEntry) cur).matches(image, width, height))
{
if (prev == null) {
head = next;
} else {
prev.next = next;
}
cur.cancel();
} else {
prev = cur;
}
cur = next;
}
notifyAll(); // Notify in case remaining images are "done".
| synchronized void | setDone()
notifyAll();
| 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.
return statusAll(load, true);
| private synchronized int | statusAll(boolean load, boolean verify)
MediaEntry cur = head;
int status = 0;
while (cur != null) {
status = status | cur.getStatus(load, verify);
cur = cur.next;
}
return status;
| 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.
return statusID(id, load, true);
| private synchronized int | statusID(int id, boolean load, boolean verify)
MediaEntry cur = head;
int status = 0;
while (cur != null) {
if (cur.getID() == id) {
status = status | cur.getStatus(load, verify);
}
cur = cur.next;
}
return status;
| public synchronized boolean | waitForAll(long ms)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.
long end = System.currentTimeMillis() + ms;
boolean first = true;
while (true) {
int status = statusAll(first, first);
if ((status & LOADING) == 0) {
return (status == COMPLETE);
}
first = false;
long timeout;
if (ms == 0) {
timeout = 0;
} else {
timeout = end - System.currentTimeMillis();
if (timeout <= 0) {
return false;
}
}
wait(timeout);
}
| public void | waitForAll()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.
waitForAll(0);
| public void | waitForID(int id)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.
waitForID(id, 0);
| public synchronized boolean | waitForID(int id, long ms)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.
long end = System.currentTimeMillis() + ms;
boolean first = true;
while (true) {
int status = statusID(id, first, first);
if ((status & LOADING) == 0) {
return (status == COMPLETE);
}
first = false;
long timeout;
if (ms == 0) {
timeout = 0;
} else {
timeout = end - System.currentTimeMillis();
if (timeout <= 0) {
return false;
}
}
wait(timeout);
}
|
|