⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediatracker.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * @(#)MediaTracker.java	1.26 97/06/17
 * 
 * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sun.
 * 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 * 
 * CopyrightVersion 1.1_beta
 * 
 */

package java.awt;

import java.awt.Component;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.image.ImageObserver;

/**
 * The <code>MediaTracker</code> class is a utility class to track 
 * the status of a number of media objects. Media objects could 
 * include audio clips as well as images, though currently only 
 * images are supported. 
 * <p>
 * To use a media tracker, create an instance of  
 * <code>MediaTracker</code> and call its <code>addImage</code>  
 * method for each image to be tracked. In addition, each image can 
 * be assigned a unique identifier. This identifier controls the 
 * priority order in which the images are fetched. It can also be used 
 * to identify unique subsets of the images that can be waited on 
 * independently. Images with a lower ID are loaded in preference to 
 * those with a higher ID number. 
 * <p>
 * Here is an example: 
 * <p>
 * <hr><blockquote><pre>
 * 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);
 *	    }
 *	}
 * }
 * </pre></blockquote><hr>
 * <p>
 * @version 	1.26, 06/17/97
 * @author 	Jim Graham
 * @since       JDK1.0
 */
public class MediaTracker implements java.io.Serializable {
    Component target;
    MediaEntry head;

    /*
     * JDK 1.1 serialVersionUID 
     */
    private static final long serialVersionUID = -483174189758638095L;

    /**
     * Creates a media tracker to track images for a given component.
     * @param     comp the component on which the images 
     *                     will eventually be drawn.
     * @since     JDK1.0
     */
    public MediaTracker(Component comp) {
	target = comp;
    }

    /**
     * Adds an image to the list of images being tracked by this media 
     * tracker. The image will eventually be rendered at its default 
     * (unscaled) size. 
     * @param     image   the image to be tracked.
     * @param     id      an identifier used to track this image.
     * @since     JDK1.0
     */
    public void addImage(Image image, int id) {
	addImage(image, id, -1, -1);
    }

    /**
     * Adds a scaled image to the list of images being tracked  
     * by this media tracker. The image will eventually be 
     * rendered at the indicated width and height.
     * @param     image   the image to be tracked.
     * @param     id   an identifier that can be used to track this image.
     * @param     w    the width at which the image is rendered.
     * @param     h    the height at which the image is rendered.
     * @since     JDK1.0
     */
    public synchronized void addImage(Image image, int id, int w, int h) {
	head = MediaEntry.insert(head,
				 new ImageMediaEntry(this, image, id, w, h));
    }

    /**
     * Flag indicating some media is currently being loaded.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     * @since       JDK1.0
     */
    public static final int LOADING = 1;

    /**
     * Flag indicating that the downloading of some media was aborted.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     * @since       JDK1.0
     */
    public static final int ABORTED = 2;

    /**
     * Flag indicating that the downloading of some media encountered 
     * an error.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     * @since       JDK1.0
     */
    public static final int ERRORED = 4;

    /**
     * Flag indicating that the downloading of media was completed 
     * successfully.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     * @since       JDK1.0
     */
    public static final int COMPLETE = 8;

    static final int DONE = (ABORTED | ERRORED | COMPLETE);

    /**
     * Checks to see if all images being tracked by this media tracker 
     * have finished loading. 
     * <p>
     * This method does not start loading the images if they are not 
     * already loading. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise.
     * @see         java.awt.MediaTracker#checkAll(boolean)
     * @see         java.awt.MediaTracker#checkID
     * @see         java.awt.MediaTracker#isErrorAny
     * @see         java.awt.MediaTracker#isErrorID
     * @since       JDK1.0
     */
    public boolean checkAll() {
	return checkAll(false, true);
    }

    /**
     * Checks to see if all images being tracked by this media tracker 
     * have finished loading. 
     * <p>
     * If the value of the <code>load</code> flag is <code>true</code>, 
     * then this method starts loading any images that are not yet 
     * being loaded. 
     * <p>
     * If there is an error while loading or scaling an image, that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> and <code>isErrorID</code> methods to 
     * check for errors. 
     * @param       load   if <code>true</code>, start loading any 
     *                       images that are not yet being loaded.
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise.
     * @see         java.awt.MediaTracker#checkID
     * @see         java.awt.MediaTracker#checkAll()
     * @see         java.awt.MediaTracker#isErrorAny()
     * @see         java.awt.MediaTracker#isErrorID(int)
     * @since       JDK1.0
     */
    public boolean checkAll(boolean load) {
	return checkAll(load, true);
    }

    private synchronized boolean checkAll(boolean load, boolean verify) {
	MediaEntry cur = head;
	boolean done = true;
	while (cur != null) {
	    if ((cur.getStatus(load, verify) & DONE) == 0) {
		done = false;
	    }
	    cur = cur.next;
	}
	return done;
    }

    /**
     * Checks the error status of all of the images.
     * @return   <code>true</code> if any of the images tracked
     *                  by this media tracker had an error during 
     *                  loading; <code>false</code> otherwise.
     * @see      java.awt.MediaTracker#isErrorID
     * @see      java.awt.MediaTracker#getErrorsAny
     * @since    JDK1.0
     */
    public synchronized boolean isErrorAny() {
	MediaEntry cur = head;
	while (cur != null) {
	    if ((cur.getStatus(false, true) & ERRORED) != 0) {
		return true;
	    }
	    cur = cur.next;
	}
	return false;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -