- java.lang.Object
-
- java.awt.MediaTracker
-
- All Implemented Interfaces:
-
Serializable
public class MediaTracker extends Object implements Serializable
MediaTracker类是一个实用程序类,用于跟踪多个媒体对象的状态。 媒体对象可以包括音频剪辑以及图像,但是目前只支持图像。要使用媒体跟踪器,请创建
MediaTracker的实例,并为要跟踪的每个图像调用其addImage方法。 此外,每个图像可以被分配唯一的标识符。 该标识符控制获取图像的优先级顺序。 它还可以用于识别可独立等待的图像的唯一子集。 具有较低ID的图像优先于具有较高ID号的那些加载。跟踪动画图像可能不总是有用的,因为动画图像加载和绘画的多部分性质,但它是受支持的。
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
-
-
构造方法摘要
构造方法 Constructor 描述 MediaTracker(Component comp)创建媒体跟踪器以跟踪给定组件的图像。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 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)开始使用指定的标识符加载由此媒体跟踪器跟踪的所有图像。
-
-
-
构造方法详细信息
-
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- 要移除的宽度(对于unscaled为-1) -
height- 要移除的高度(对于unscaled为-1) - 从以下版本开始:
- 1.1
- 另请参见:
-
removeImage(java.awt.Image),removeImage(java.awt.Image, int)
-
-