|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.MediaTracker
A utility class to track 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 it, simply create an instance and then call addImage() for each image to be tracked. Each image can be assigned a unique ID for indentification purposes. The IDs control the priority order in which the images are fetched as well as identifying unique subsets of the images that can be waited on independently. 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 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.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 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(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); } } }
Field Summary | |
static int |
ABORTED
Flag indicating the download of some media was aborted. |
static int |
COMPLETE
Flag indicating the download of media completed successfully. |
static int |
ERRORED
Flag indicating the download of some media encountered an error. |
static int |
LOADING
Flag indicating some media is currently being loaded. |
Constructor Summary | |
MediaTracker(Component comp)
Creates a Media tracker to track images for a given Component. |
Method Summary | |
void |
addImage(Image image,
int id)
Adds an image to the list of images being tracked. |
void |
addImage(Image image,
int id,
int w,
int h)
Adds a scaled image to the list of images being tracked. |
boolean |
checkAll()
Checks to see if all images have finished loading but does not start loading the images if they are not already loading. |
boolean |
checkAll(boolean load)
Checks to see if all images have finished loading. |
boolean |
checkID(int id)
Checks to see if all images tagged with the indicated ID have finished loading, but does not start loading the images if they are not already loading. |
boolean |
checkID(int id,
boolean load)
Checks to see if all images tagged with the indicated ID have finished loading. |
Object[] |
getErrorsAny()
Returns a list of all media that have encountered an error. |
Object[] |
getErrorsID(int id)
Returns a list of media with the specified ID that have encountered an error. |
boolean |
isErrorAny()
Checks the error status of all of the images. |
boolean |
isErrorID(int id)
Checks the error status of all of the images with the specified ID. |
int |
statusAll(boolean load)
Returns the boolean OR of the status of all of the media being tracked. |
int |
statusID(int id,
boolean load)
Returns the boolean OR of the status of all of the media with a given ID. |
void |
waitForAll()
Starts loading all images. |
boolean |
waitForAll(long ms)
Starts loading all images. |
void |
waitForID(int id)
Starts loading all images with the specified ID and waits until they have finished loading or receive an error. |
boolean |
waitForID(int id,
long ms)
Starts loading all images with the specified ID. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int LOADING
statusAll(boolean)
,
statusID(int, boolean)
,
Constant Field Valuespublic static final int ABORTED
statusAll(boolean)
,
statusID(int, boolean)
,
Constant Field Valuespublic static final int ERRORED
statusAll(boolean)
,
statusID(int, boolean)
,
Constant Field Valuespublic static final int COMPLETE
statusAll(boolean)
,
statusID(int, boolean)
,
Constant Field ValuesConstructor Detail |
public MediaTracker(Component comp)
comp
- the component on which the images will eventually be drawnMethod Detail |
public void addImage(Image image, int id)
image
- the image to be trackedid
- the identifier used to later track this imagepublic void addImage(Image image, int id, int w, int h)
image
- the image to be trackedid
- the identifier used to later track this imagew
- the width that the image will be rendered ath
- the height that the image will be rendered atpublic boolean checkAll()
checkAll(boolean)
,
checkID(int)
,
isErrorAny()
,
isErrorID(int)
public boolean checkAll(boolean load)
load
- start loading the images if this parameter is true
isErrorAny()
,
isErrorID(int)
,
checkID(int, boolean)
,
checkAll()
public boolean isErrorAny()
isErrorID(int)
,
getErrorsAny()
public Object[] getErrorsAny()
isErrorAny()
,
getErrorsID(int)
public void waitForAll() throws InterruptedException
InterruptedException
- Another thread has interrupted this thread.waitForID(int)
,
waitForAll(long)
,
isErrorAny()
,
isErrorID(int)
public boolean waitForAll(long ms) throws InterruptedException
ms
- the length of time to wait for the loading to complete
InterruptedException
- Another thread has interrupted this thread.waitForID(int)
,
waitForAll()
,
isErrorAny()
,
isErrorID(int)
public int statusAll(boolean load)
load
- specifies whether to start the media loadingstatusID(int, boolean)
,
LOADING
,
ABORTED
,
ERRORED
,
COMPLETE
public boolean checkID(int id)
id
- the identifier used to determine which images to check
checkID(int, boolean)
,
checkAll()
,
isErrorAny()
,
isErrorID(int)
public boolean checkID(int id, boolean load)
id
- the identifier used to determine which images to checkload
- start loading the images if this parameter is true
checkID(int)
,
checkAll()
,
isErrorAny()
,
isErrorID(int)
public boolean isErrorID(int id)
id
- the identifier used to determine which images to check
isErrorAny()
,
getErrorsID(int)
public Object[] getErrorsID(int id)
id
- the identifier used to determine which images to return
isErrorID(int)
,
getErrorsAny()
public void waitForID(int id) throws InterruptedException
id
- the identifier used to determine which images to wait for
InterruptedException
- Another thread has interrupted this thread.waitForAll()
,
waitForID(int, long)
,
isErrorAny()
,
isErrorID(int)
public boolean waitForID(int id, long ms) throws InterruptedException
id
- the identifier used to determine which images to wait forms
- the length of time to wait for the loading to complete
InterruptedException
- Another thread has interrupted this thread.waitForAll()
,
waitForID(int)
,
isErrorAny()
,
isErrorID(int)
public int statusID(int id, boolean load)
id
- the identifier used to determine which images to checkload
- specifies whether to start the media loadingstatusAll(boolean)
,
LOADING
,
ABORTED
,
ERRORED
,
COMPLETE
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |