java.lang.Objectjava.awt.MediaTracker
public class MediaTracker
MediaTracker 类是一个跟踪多种媒体对象状态的实用工具类。媒体对象可以包括音频剪辑和图像,但目前仅支持图像。
要使用媒体跟踪器,需要创建一个 MediaTracker 实例,然后对每个要跟踪的图像调用其 addImage 方法。另外,还可以为每个图像分配一个唯一的标识符。此标识符可控制获取图像的优先级顺序。它还可用于标识可单独等待的唯一图像子集。具有较低 ID 的图像比具有较高 ID 的图像优先加载。
由于动画图像加载和绘制的多部分特性(multi-part nature),跟踪动画图像可能不是始终有用,但这一功能的确受支持。在完成动画图像的第一帧的加载之后,MediaTracker 会认为动画图像已经加载完毕。这时,MediaTracker 会向所有等待者发出图像已完全加载的信号。如果在第一帧加载完之后没有 ImageObserver 查看此图像,则该图像可能会自我刷新来保存资源(请参见 Image.flush())。
下面是一个使用 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);
}
}
}
| 字段摘要 | |
|---|---|
static int |
ABORTED 指示媒体下载已中止的标志。 |
static int |
COMPLETE 指示媒体下载已成功完成的标志。 |
static int |
ERRORED 指示媒体下载出错的标志。 |
static int |
LOADING 指示当前正在加载媒体的标志。 |
| 构造方法摘要 | |
|---|---|
MediaTracker(Component comp) 创建媒体跟踪器以跟踪给定组件的图像。 |
|
| 方法摘要 | |
|---|---|
void |
addImage(Image image, int id) 向此媒体跟踪器正在跟踪的图像列表添加一个图像。 |
void |
addImage(Image image, int id, int w, int h) 向此媒体跟踪器正在跟踪的图像列表添加一个经过缩放的图像。 |
boolean |
checkAll() 查看此媒体跟踪器正在跟踪的所有图像是否已完成加载。 |
boolean |
checkAll(boolean load) 检查此媒体跟踪器正在跟踪的所有图像是否都已完成加载。 |
boolean |
checkID(int id) 检查由此媒体跟踪器跟踪且使用指定标识符标记的所有图像是否已完成加载。 |
boolean |
checkID(int id, boolean load) 检查由此媒体跟踪器跟踪且使用指定标识符标记的所有图像是否已完成加载。 |
Object[] |
getErrorsAny() 返回所有出错媒体的列表。 |
Object[] |
getErrorsID(int id) 返回具有出错的指定 ID 的媒体列表。 |
boolean |
isErrorAny() 检查所有图像的错误状态。 |
boolean |
isErrorID(int id) 检查由此媒体跟踪器跟踪且具有指定标识符的所有图像的错误状态。 |
void |
removeImage(Image image) 从此媒体跟踪器移除指定的图像。 |
void |
removeImage(Image image, int id) 从此媒体跟踪器的指定跟踪 ID 中移除指定的图像。 |
void |
removeImage(Image image, int id, int width, int height) 从此媒体跟踪器移除具有指定宽度、高度和 ID 的指定图像。 |
int |
statusAll(boolean load) 计算并返回此媒体跟踪器跟踪的所有媒体状态的按位或。 |
int |
statusID(int id, boolean load) 计算或返回由此媒体跟踪器跟踪且具有指定标识符的所有媒体状态的按位或。 |
void |
waitForAll() 开始加载由此媒体跟踪器跟踪的所有图像。 |
boolean |
waitForAll(long ms) 开始加载由此媒体跟踪器跟踪的所有图像。 |
void |
waitForID(int id) 开始加载由此媒体跟踪器跟踪且具有指定标识符的所有图像。 |
boolean |
waitForID(int id, long ms) 开始加载由此媒体跟踪器跟踪且具有指定标识符的所有图像。 |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 字段详细信息 |
|---|
public static final int LOADING
public static final int ABORTED
public static final int ERRORED
public static final int COMPLETE
| 构造方法详细信息 |
|---|
public MediaTracker(Component comp)
comp - 最终将在其上绘制图像的组件。
| 方法详细信息 |
|---|
public void addImage(Image image, int id)
image - 要跟踪的图像
id - 用于跟踪此图像的标识符
public void addImage(Image image, int id, int w, int h)
image - 要跟踪的图像
id - 一个可用于跟踪此图像的标识符
w - 用来呈现图像的宽度
h - 用来呈现图像的高度
public boolean checkAll()
如果图像尚未完成加载,则此方法不能开始加载图像。
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 isErrorAny 或 isErrorID 方法检查错误。
true;否则返回
false
checkAll(boolean),
checkID(int),
isErrorAny(),
isErrorID(int)
public boolean checkAll(boolean load)
如果此 load 标志的值为 true,则此方法将开始加载任何尚未加载的图像。
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 isErrorAny 和 isErrorID 方法检查错误。
load - 如果为
true,则开始加载任何尚未加载的图像
true;否则返回
false
checkID(int),
checkAll(),
isErrorAny(),
isErrorID(int)
public boolean isErrorAny()
true;否则返回
false
isErrorID(int),
getErrorsAny()
public Object[] getErrorsAny()
null
isErrorAny(),
getErrorsID(int)
public void waitForAll()
throws InterruptedException
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 isErrorAny 或 isErrorID 方法检查错误。
InterruptedException - 如果任何线程中断了此线程
waitForID(int),
waitForAll(long),
isErrorAny(),
isErrorID(int)
public boolean waitForAll(long ms)
throws InterruptedException
ms 参数(以毫秒为单位)指定的时间到期之前,此方法将一直等待。
如果在加载或缩放图像时出错,则认为该图像已完成加载。使用 isErrorAny 或 isErrorID 方法检查错误。
ms - 等待加载完成的毫秒数
true;否则返回
false
InterruptedException - 如果任何线程中断了此线程
waitForID(int),
waitForAll(long),
isErrorAny(),
isErrorID(int)
public int statusAll(boolean load)
由 MediaTracker 类定义的可能标志有 LOADING、ABORTED、ERRORED 和 COMPLETE。对于尚未开始加载的图像,其状态值为零。
如果 load 值为 true,则此方法开始加载任何尚未加载的图像。
load - 如果为
true,则开始加载任何尚未加载的图像
statusID(int, boolean),
LOADING,
ABORTED,
ERRORED,
COMPLETE
public boolean checkID(int id)
如果图像尚未完成加载,则此方法不能开始加载图像。
如果在加载或缩放图像时出错,则认为该图像已完成加载。使用 isErrorAny 或 isErrorID 方法检查错误。
id - 要检查的图像标识符
true;否则返回
false
checkID(int, boolean),
checkAll(),
isErrorAny(),
isErrorID(int)
public boolean checkID(int id,
boolean load)
如果此 load 标志的值为 true,则此方法将开始加载任何尚未加载的图像。
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 isErrorAny 或 isErrorID 方法检查错误。
id - 要检查的图像标识符
load - 如果为
true,则开始加载任何尚未加载的图像。
true;否则返回
false
checkID(int, boolean),
checkAll(),
isErrorAny(),
isErrorID(int)
public boolean isErrorID(int id)
id - 要检查图像的标识符
true;否则返回
false
isErrorAny(),
getErrorsID(int)
public Object[] getErrorsID(int id)
id - 要检查图像的标识符
null
isErrorID(int),
isErrorAny(),
getErrorsAny()
public void waitForID(int id)
throws InterruptedException
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 isErrorAny 和 isErrorID 方法检查错误。
id - 要检查图像的标识符
InterruptedException - 如果任何线程中止了此线程。
waitForAll(),
isErrorAny(),
isErrorID(int)
public boolean waitForID(int id,
long ms)
throws InterruptedException
ms 参数(以毫秒为单位)指定的时间到期之前,此方法一直等待。
如果在加载或缩放图像时出错,则该图像被视为已完成加载。使用 statusID、isErrorID 和 isErrorAny 方法检查错误。
id - 要检查图像的标识符
ms - 等待加载完成的时间长度(以毫秒为单位)
InterruptedException - 如果任何线程中断了此线程
waitForAll(),
waitForID(int),
statusID(int, boolean),
isErrorAny(),
isErrorID(int)
public int statusID(int id,
boolean load)
由 MediaTracker 类定义的可能标志有 LOADING、ABORTED、ERRORED 和 COMPLETE。尚未开始加载的图像,其状态值为零。
如果 load 值为 true,则此方法开始加载任何尚未加载的图像。
id - 要检查图像的标识符
load - 如果为
true,则开始加载任何尚未加载的图像
statusAll(boolean),
LOADING,
ABORTED,
ERRORED,
COMPLETE
public void removeImage(Image image)
image - 要移除的图像
removeImage(java.awt.Image, int),
removeImage(java.awt.Image, int, int, int)
public void removeImage(Image image, int id)
Image 实例都将被移除。
image - 要移除的图像
id - 从其移除图像的跟踪 ID
removeImage(java.awt.Image),
removeImage(java.awt.Image, int, int, int)
public void removeImage(Image image, int id, int width, int height)
image - 要移除的图像
id - 从其移除图像的跟踪 ID
width - 要移除的宽度(-1 为未缩放)
height - 要移除的高度(-1 为未缩放)
removeImage(java.awt.Image),
removeImage(java.awt.Image, int)