- java.lang.Object
-
- java.awt.MediaTracker
-
- 实现的所有接口
-
Serializable
public class MediaTracker extends Object implements Serializable
MediaTracker类是一个实用程序类,用于跟踪多个媒体对象的状态。 媒体对象可以包括音频剪辑和图像,但目前仅支持图像。要使用媒体跟踪器,请创建
MediaTracker的实例,并为要跟踪的每个图像调用其addImage方法。 此外,可以为每个图像分配唯一标识符。 该标识符控制获取图像的优先级顺序。 它还可以用于识别可以独立等待的图像的唯一子集。 具有较低ID的图像优先于具有较高ID号的图像加载。由于动画图像加载和绘制的多部分特性,跟踪动画图像可能并不总是有用,但它是受支持的。 当第一帧完全加载时,
MediaTracker将动画图像视为完全加载。 此时,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); } } }
- 从以下版本开始:
- 1.0
- 另请参见:
- Serialized Form
-
-
构造方法摘要
构造方法 构造器 描述 MediaTracker(Component comp)创建媒体跟踪器以跟踪给定组件的图像。
-
方法摘要
所有方法 实例方法 具体的方法 变量和类型 方法 描述 voidaddImage(Image image, int id)将图像添加到此媒体跟踪器正在跟踪的图像列表中。voidaddImage(Image image, int id, int w, int h)将缩放图像添加到此媒体跟踪器正在跟踪的图像列表中。booleancheckAll()检查此媒体跟踪器正在跟踪的所有图像是否已完成加载。booleancheckAll(boolean load)检查此媒体跟踪器正在跟踪的所有图像是否已完成加载。booleancheckID(int id)检查此媒体跟踪器跟踪的所有使用指定标识符标记的图像是否已完成加载。booleancheckID(int id, boolean load)检查此媒体跟踪器跟踪的所有使用指定标识符标记的图像是否已完成加载。Object[]getErrorsAny()返回遇到错误的所有媒体的列表。Object[]getErrorsID(int id)返回具有指定ID且遇到错误的媒体列表。booleanisErrorAny()检查所有图像的错误状态。booleanisErrorID(int id)使用指定的标识符检查此媒体跟踪器跟踪的所有图像的错误状态。voidremoveImage(Image image)从此媒体跟踪器中删除指定的图像。voidremoveImage(Image image, int id)从此媒体跟踪器的指定跟踪ID中删除指定的图像。voidremoveImage(Image image, int id, int width, int height)从此媒体跟踪器中删除具有指定宽度,高度和ID的指定图像。intstatusAll(boolean load)计算并返回此媒体跟踪器跟踪的所有媒体的状态的按位包含 OR 。intstatusID(int id, boolean load)计算并返回具有此媒体跟踪器跟踪的指定标识符的所有媒体的状态的按位包含 OR 。voidwaitForAll()开始加载此媒体跟踪器跟踪的所有图像。booleanwaitForAll(long ms)开始加载此媒体跟踪器跟踪的所有图像。voidwaitForID(int id)开始使用指定的标识符加载此媒体跟踪器跟踪的所有图像。booleanwaitForID(int id, long ms)开始使用指定的标识符加载此媒体跟踪器跟踪的所有图像。
-
-
-
字段详细信息
-
LOADING
public static final int LOADING
指示当前正在加载媒体的标志。- 另请参见:
-
statusAll(boolean),statusID(int, boolean), 常数字段值
-
ABORTED
public static final int ABORTED
表示媒体下载中止的标志。- 另请参见:
-
statusAll(boolean),statusID(int, boolean), 常数字段值
-
ERRORED
public static final int ERRORED
指示媒体下载遇到错误的标志。- 另请参见:
-
statusAll(boolean),statusID(int, boolean), 常数字段值
-
COMPLETE
public static final int COMPLETE
表示媒体下载已成功完成的标志。- 另请参见:
-
statusAll(boolean),statusID(int, boolean), 常数字段值
-
-
构造方法详细信息
-
MediaTracker
public MediaTracker(Component comp)
创建媒体跟踪器以跟踪给定组件的图像。- 参数
-
comp- 最终将在其上绘制图像的组件
-
-
方法详细信息
-
addImage
public void addImage(Image image, int id)
将图像添加到此媒体跟踪器正在跟踪的图像列表中。 图像最终将以其默认(未缩放)大小呈现。- 参数
-
image- 要跟踪的图像 -
id- 用于跟踪此图像的标识符
-
addImage
public void addImage(Image image, int id, int w, int h)
将缩放图像添加到此媒体跟踪器正在跟踪的图像列表中。 最终将以指定的宽度和高度渲染图像。- 参数
-
image- 要跟踪的图像 -
id- 可用于跟踪此图像的标识符 -
w- 渲染图像的宽度 -
h- 渲染图像的高度
-
checkAll
public boolean checkAll()
检查此媒体跟踪器正在跟踪的所有图像是否已完成加载。如果图像尚未加载,则此方法不会开始加载图像。
如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny或isErrorID方法检查错误。- 结果
-
true如果所有图像都已完成加载,已中止或遇到错误; 否则为false - 另请参见:
-
checkAll(boolean),checkID(int),isErrorAny(),isErrorID(int)
-
checkAll
public boolean checkAll(boolean load)
检查此媒体跟踪器正在跟踪的所有图像是否已完成加载。如果
load标志的值为true,则此方法将开始加载尚未加载的任何图像。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny和isErrorID方法检查错误。- 参数
-
load- 如果是true,则开始加载尚未加载的任何图像 - 结果
-
true如果所有图像都已完成加载,已中止或遇到错误; 否则为false - 另请参见:
-
checkID(int),checkAll(),isErrorAny(),isErrorID(int)
-
isErrorAny
public boolean isErrorAny()
检查所有图像的错误状态。- 结果
-
true如果此媒体跟踪器跟踪的任何图像在加载过程中出错; 否则为false - 另请参见:
-
isErrorID(int),getErrorsAny()
-
getErrorsAny
public Object[] getErrorsAny()
返回遇到错误的所有媒体的列表。- 结果
-
此媒体跟踪器跟踪遇到错误的媒体对象数组,如果没有错误
null - 另请参见:
-
isErrorAny(),getErrorsID(int)
-
waitForAll
public void waitForAll() throws InterruptedException开始加载此媒体跟踪器跟踪的所有图像。 此方法等待,直到所有被跟踪的图像都已完成加载。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny或isErrorID方法检查错误。- 异常
-
InterruptedException- 如果有任何线程中断了该线程 - 另请参见:
-
waitForID(int),waitForAll(long),isErrorAny(),isErrorID(int)
-
waitForAll
public boolean waitForAll(long ms) throws InterruptedException开始加载此媒体跟踪器跟踪的所有图像。 此方法等待,直到所有跟踪的图像都已完成加载,或者直到ms参数指定的时间长度(以毫秒为单位)已经过去。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny或isErrorID方法检查错误。- 参数
-
ms- 等待加载完成的毫秒数 - 结果
-
true如果所有图像都已成功加载; 否则为false - 异常
-
InterruptedException- 如果有任何线程中断了该线程。 - 另请参见:
-
waitForID(int),waitForAll(long),isErrorAny(),isErrorID(int)
-
statusAll
public int statusAll(boolean load)
计算并返回此媒体跟踪器跟踪的所有媒体的状态的按位包含OR 。由定义的可能标志
MediaTracker类是LOADING,ABORTED,ERRORED和COMPLETE。 未开始加载的图像的状态为零。如果
load值为true,则此方法将开始加载尚未加载的任何图像。- 参数
-
load- 如果是true,则开始加载尚未加载的任何图像 - 结果
- 被跟踪的所有媒体的状态的按位包含 OR
- 另请参见:
-
statusID(int, boolean),LOADING,ABORTED,ERRORED,COMPLETE
-
checkID
public boolean checkID(int id)
检查此媒体跟踪器跟踪的所有使用指定标识符标记的图像是否已完成加载。如果图像尚未加载,则此方法不会开始加载图像。
如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny或isErrorID方法检查错误。- 参数
-
id- 要检查的图像的标识符 - 结果
-
true如果所有图像都已完成加载,已中止或遇到错误; 否则为false - 另请参见:
-
checkID(int, boolean),checkAll(),isErrorAny(),isErrorID(int)
-
checkID
public boolean checkID(int id, boolean load)检查此媒体跟踪器跟踪的所有使用指定标识符标记的图像是否已完成加载。如果
load标志的值为true,则此方法将开始加载尚未加载的任何图像。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny或isErrorID方法检查错误。- 参数
-
id- 要检查的图像的标识符 -
load- 如果是true,则开始加载尚未加载的任何图像 - 结果
-
true如果所有图像都已完成加载,已中止或遇到错误; 否则为false - 另请参见:
-
checkID(int, boolean),checkAll(),isErrorAny(),isErrorID(int)
-
isErrorID
public boolean isErrorID(int id)
使用指定的标识符检查此媒体跟踪器跟踪的所有图像的错误状态。- 参数
-
id- 要检查的图像的标识符 - 结果
-
true如果任何具有指定标识符的图像在加载过程中出错; 否则为false - 另请参见:
-
isErrorAny(),getErrorsID(int)
-
getErrorsID
public Object[] getErrorsID(int id)
返回具有指定ID且遇到错误的媒体列表。- 参数
-
id- 要检查的图像的标识符 - 结果
-
此媒体跟踪器使用指定标识符跟踪错误的媒体对象数组,如果没有错误
null - 另请参见:
-
isErrorID(int),isErrorAny(),getErrorsAny()
-
waitForID
public void waitForID(int id) throws InterruptedException开始使用指定的标识符加载此媒体跟踪器跟踪的所有图像。 此方法等待,直到具有指定标识符的所有图像都已完成加载。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
isErrorAny和isErrorID方法检查错误。- 参数
-
id- 要检查的图像的标识符 - 异常
-
InterruptedException- 如果有任何线程中断了该线程。 - 另请参见:
-
waitForAll(),isErrorAny(),isErrorID(int)
-
waitForID
public boolean waitForID(int id, long ms) throws InterruptedException开始使用指定的标识符加载此媒体跟踪器跟踪的所有图像。 此方法等待,直到具有指定标识符的所有图像都已完成加载,或者直到ms参数指定的时间长度(以毫秒为单位)已经过去。如果在加载或缩放图像时出现错误,则认为该图像已完成加载。 使用
statusID,isErrorID和isErrorAny方法来检查错误。- 参数
-
id- 要检查的图像的标识符 -
ms- 等待加载完成的时间长度(以毫秒为单位) - 结果
-
true如果装货及时完成; 否则false - 异常
-
InterruptedException- 如果有任何线程中断了该线程。 - 另请参见:
-
waitForAll(),waitForID(int),statusID(int, boolean),isErrorAny(),isErrorID(int)
-
statusID
public int statusID(int id, boolean load)计算并返回具有此媒体跟踪器跟踪的指定标识符的所有媒体的状态的按位包含OR 。由定义的可能标志
MediaTracker类是LOADING,ABORTED,ERRORED和COMPLETE。 未开始加载的图像的状态为零。如果
load的值是true,则此方法开始加载尚未加载的任何图像。- 参数
-
id- 要检查的图像的标识符 -
load- 如果是true,则开始加载尚未加载的任何图像 - 结果
- 包含正在跟踪的指定标识符的所有媒体的状态的按位包含 OR
- 另请参见:
-
statusAll(boolean),LOADING,ABORTED,ERRORED,COMPLETE
-
removeImage
public void removeImage(Image image)
从此媒体跟踪器中删除指定的图像。 无论比例或ID如何,都会删除指定图像的所有实例。- 参数
-
image- 要删除的图像 - 从以下版本开始:
- 1.1
- 另请参见:
-
removeImage(java.awt.Image, int),removeImage(java.awt.Image, int, int, int)
-
removeImage
public void removeImage(Image image, int id)
从此媒体跟踪器的指定跟踪ID中删除指定的图像。 无论规模如何,都将删除在指定ID下跟踪的所有Image实例。- 参数
-
image- 要删除的图像 -
id- 要从中删除图像的跟踪ID - 从以下版本开始:
- 1.1
- 另请参见:
-
removeImage(java.awt.Image),removeImage(java.awt.Image, int, int, int)
-
removeImage
public void removeImage(Image image, int id, int width, int height)
从此媒体跟踪器中删除具有指定宽度,高度和ID的指定图像。 仅删除指定的实例(包含任何重复项)。- 参数
-
image- 要删除的图像 -
id- 要从中删除图像的跟踪ID -
width- 要移除的宽度(-1表示未缩放) -
height- 要移除的高度(-1表示未缩放) - 从以下版本开始:
- 1.1
- 另请参见:
-
removeImage(java.awt.Image),removeImage(java.awt.Image, int)
-
-