所有类


java.awt
类 MediaTracker

java.lang.Object
  继承者 java.awt.MediaTracker
所有已实现的接口:
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);
            }
        }
 }
 

从以下版本开始:
JDK1.0
另请参见:
序列化表格

字段摘要
static intABORTED
          指示媒体下载已中止的标志。
static intCOMPLETE
          指示媒体下载已成功完成的标志。
static intERRORED
          指示媒体下载出错的标志。
static intLOADING
          指示当前正在加载媒体的标志。
 
构造方法摘要
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)
          计算并返回此媒体跟踪器跟踪的所有媒体状态的位逻辑
 intstatusID(int id, boolean load)
          计算或返回由此媒体跟踪器跟踪且具有指定标识符的所有媒体状态的位逻辑
 voidwaitForAll()
          开始加载由此媒体跟踪器跟踪的所有图像。
 booleanwaitForAll(long ms)
          开始加载由此媒体跟踪器跟踪的所有图像。
 voidwaitForID(int id)
          开始加载由此媒体跟踪器跟踪且具有指定标识符的所有图像。
 booleanwaitForID(int id, long ms)
          开始加载由此媒体跟踪器跟踪且具有指定标识符的所有图像。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

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 - 用来呈现图像的高度