java.lang.Object | +----java.awt.MediaTracker
MediaTracker 是一个实用工具类,跟踪多种媒体对象的状态。媒体对象包括声音和图象,但当前仅支持图象。
为了使用媒体跟踪器,创建 MediaTracker 的一个实例,且为每一跟踪的图象调用此类的 addImage 方法。另外,分配给每个图象唯一的一个标识符。此标识符可控制图象的返回优先次序。它也可用于唯一标识能单独等候的图象子集。具有较低的 ID 的图象比具有较高的 ID 的图象优先装载。
一示例如下:
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 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);
}
}
}
public static final int LOADING
public static final int ABORTED
public static final int ERRORED
public static final int COMPLETE
public MediaTracker(Component comp)
public void addImage(Image image,
int id)
public synchronized void addImage(Image image,
int id,
int w,
int h)
public boolean checkAll()
如果它们已被载入,此方法将不再开始装载。
如果装载或按比例缩放图象时发生错误,则认为此图象装载完毕。使用 isErrorAny 或 isErrorID 方法以检测错误。
true,否则为 false。
public boolean checkAll(boolean load)
如果 load 标志的值为 true,那么此方法开始装载任何没有载入的图象。
如果装载或按比例缩放图象时发生错误,那么认为此图象已装载完毕。 使用 isErrorAny 或 isErrorID 方法以检测错误。
true, 那么开始装载任何没有载入的图象。
true,否则为 false。
public synchronized boolean isErrorAny()
true,否则为 false。
public synchronized Object[] getErrorsAny()
null。
public void waitForAll() throws InterruptedException
如果装载或按比例缩放图象时发生错误,则认为此图象已装载完毕。使用 isErrorAny 或 isErrorID 方法以检测错误。
public synchronized boolean waitForAll(long ms) throws InterruptedException
ms 参数指定的时间值。
如果装载或按比例缩放图象时发生错误,则认为此图象装载完毕。 使用 isErrorAny 或 isErrorID 方法以检测错误。
true,否则为
false。
public int statusAll(boolean load)
MediaTracker 类定义的标志可为 LOADING,
ABORTED, ERRORED 和 COMPLETE 。
未开始装载的图象状态标志为零。
如果 load 为 true, 那么开始装载任何没有载入的图象。
true, 那么开始装载任何没有载入的图象。
public boolean checkID(int id)
如果它们已被载入,此方法将不再开始装载。
如果装载或按比例缩放图象时发生错误,则认为此图象已装载完毕。使用 isErrorAny 或 isErrorID 方法以检测错误。
true,否则为 false。
public boolean checkID(int id,
boolean load)
如果 load 标志的值为 true,那么此方法开始装载任何没有载入的图象。
如果装载或按比例缩放图象时发生错误,则认为此图象已装载完毕。使用 isErrorAny 或 isErrorID 方法以检测错误。
true, 那么开始装载任何没有载入的图象。
true,否则为 false。
public synchronized boolean isErrorID(int id)
true,否则为 false。
public synchronized Object[] getErrorsID(int id)
null。
public void waitForID(int id) throws InterruptedException
如果装载或按比例缩放图象时发生错误,则认为此图象已装载完毕。使用 isErrorAny 或 isErrorID 方法以检测错误。
public synchronized boolean waitForID(int id,
long ms) throws InterruptedException
ms 参数指定的时间值。
如果装载或按比例缩放图象时发生错误,则认为此图象已装载完毕。使用
statusID, isErrorID和
isErrorAny 方法来检测错误。
public int statusID(int id,
boolean load)
MediaTracker 类定义的标志可为 LOADING,
ABORTED, ERRORED 和 COMPLETE 。
未开始装载的图象状态标志为零。
如果 load 为 true, 那么开始装载任何没有载入的图象。
true, 那么开始装载任何没有载入的图象。
public synchronized void removeImage(Image image)
public synchronized void removeImage(Image image,
int id)
Image 的所有实例。
public synchronized void removeImage(Image image,
int id,
int width,
int height)