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

📄 mediatracker.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)MediaTracker.java	1.14 95/12/14 Jim Graham
 *
 * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * 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.
 */

package Acme;

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

/* Old class doc comment:
 *
 * A utility class to track the status of a number of media objects.
 * Media objects could include images as well as audio clips, though
 * currently only images are supported.  To use it, simply create an
 * instance and then call addImage() for each image to be tracked.
 * Each image can be assigned a unique ID for indentification purposes.
 * The IDs control the priority order in which the images are fetched
 * as well as identifying unique subsets of the images that can be
 * waited on independently.  Here is an example:
 * <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 our 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() & 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) & MediaTracker.COMPLETE) != 0) {
 *		g.drawImage(anim[index], 10, 10, this);
 *	    }
 *	}
 * }
 *
 * </pre>
 *
 * @version 	1.14, 12/14/95
 * @author 	Jim Graham
 */

/// Slightly enhanced MediaTracker.
// <P>
// All this does is add a second constructor to MediaTracker,
// taking a Toolkit instead of a Component.
// For use in cases where you
// don't happen to have a Component around - basically, non-GUI applications.
// <P>
// You may ask, well if it's a non-GUI context then why do you have
// a Toolkit?  Well, the Image stuff is pretty inextricably bound up with
// Toolkits, so when I wanted to write non-GUI Image stuff I just made
// myself a StubToolkit.  MediaTracker is the only dependency on
// Component, and it's not a very necessary dependency since it uses
// only one method from Component and that same method exists in Toolkit.
// Hence, make a MediaTracker that optionally takes a Toolkit instead
// of a Component.
// <P>
// Here's a diff of the changes:
// <BLOCKQUOTE><PRE><CODE>
// *** MediaTracker.orig        Sat Mar 23 11:41:07 1996
// --- MediaTracker.java        Sat Mar 23 13:01:31 1996
// ***************
// *** 125,131 ****
//    * @author         Jim Graham
//    *<moo>/
//   public class MediaTracker {
// !     Component target;
//       MediaEntry head;
//   
//       /**
// --- 126,133 ----
//    * @author         Jim Graham
//    *<moo>/
//   public class MediaTracker {
// !     Component compTarget;
// !     Toolkit tkTarget;
//       MediaEntry head;
//   
//       /**
// ***************
// *** 133,142 ****
//        * @param comp the component on which the images will eventually be drawn
//        *<moo>/
//       public MediaTracker(Component comp) {
// !         target = comp;
//       }
//   
//       /**
//        * Adds an image to the list of images being tracked.  The image
//        * will eventually be rendered at its default (unscaled) size.
//        * @param image the image to be tracked
// --- 135,153 ----
//        * @param comp the component on which the images will eventually be drawn
//        *<moo>/
//       public MediaTracker(Component comp) {
// !         compTarget = comp;
//       }
//   
//       /**
// +      * Creates a Media tracker to track images for a given Toolikit.
// +      * @param tk the Toolkit in which the images will eventually be drawn
// +      *<moo>/
// +     public MediaTracker(Toolkit tk) {
// +         compTarget = null;
// +         tkTarget = tk;
// +     }
// + 
// +     /**
//        * Adds an image to the list of images being tracked.  The image
//        * will eventually be rendered at its default (unscaled) size.
//        * @param image the image to be tracked
// ***************
// *** 621,629 ****
//       }
//   
//       void startLoad() {
// !         if (tracker.target.prepareImage(image, width, height, this)) {
// !             setStatus(COMPLETE);
// !         }
//       }
//   
//       public boolean imageUpdate(Image img, int infoflags,
// --- 632,645 ----
//       }
//   
//       void startLoad() {
// !         if ( tracker.compTarget != null )
// !             if (tracker.compTarget.prepareImage(image, width, height, this)) {
// !                 setStatus(COMPLETE);
// !             }
// !         else
// !             if (tracker.tkTarget.prepareImage(image, width, height, this)) {
// !                 setStatus(COMPLETE);
// !             }
//       }
//   
//       public boolean imageUpdate(Image img, int infoflags,
// </CODE></PRE></BLOCKQUOTE>
// <P>
// @see java.awt.MediaTracker
// @see java.awt.Toolkit

public class MediaTracker {
    Component compTarget;
    Toolkit tkTarget;
    MediaEntry head;

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

    /**
     * Creates a Media tracker to track images for a given Toolikit.
     * This is the only addition in the Acme version of MediaTracker.
     * @param tk the Toolkit in which the images will eventually be drawn
     */
    public MediaTracker(Toolkit tk) {
	compTarget = null;
	tkTarget = tk;
    }

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

    /**
     * Adds a scaled image to the list of images being tracked.  The
     * image will eventually be rendered at the indicated size.
     * @param image the image to be tracked
     * @param id the identifier used to later track this image
     * @param w the width that the image will be rendered at
     * @param h the height that the image will be rendered at
     */
    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 #statusAll
     * @see #statusID
     */
    public static final int LOADING = 1;

    /**
     * Flag indicating the download of some media was aborted.
     * @see #statusAll
     * @see #statusID
     */
    public static final int ABORTED = 2;

    /**
     * Flag indicating the download of some media encountered an error.
     * @see #statusAll
     * @see #statusID
     */
    public static final int ERRORED = 4;

    /**
     * Flag indicating the download of media completed successfully.
     * @see #statusAll
     * @see #statusID
     */
    public static final int COMPLETE = 8;

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

    /**
     * Checks to see if all images have finished loading but does not start
     * loading the images if they are not already loading.
     * If there is an error while loading or scaling an image then that
     * image is considered "complete."
     * Use isErrorAny() or isErrorID() to check for errors.
     * @return true if all images have finished loading, were aborted or
     * encountered an error
     * @see #checkAll(boolean)
     * @see #checkID
     * @see #isErrorAny
     * @see #isErrorID
     */
    public boolean checkAll() {
	return checkAll(false);
    }

    /**
     * Checks to see if all images have finished loading. If load is
     * true, starts loading any images that are not yet being loaded.
     * If there is an error while loading or scaling an image then
     * that image is considered "complete."  Use isErrorAny() or
     * isErrorID() to check for errors.

     * @param load start loading the images if this parameter is true
     * @return true if all images have finished loading, were aborted or
     * encountered an error
     * @see #isErrorAny
     * @see #isErrorID
     * @see #checkID(int, boolean)
     * @see #checkAll()
     */
    public synchronized boolean checkAll(boolean load) {
	MediaEntry cur = head;
	boolean done = true;
	while (cur != null) {
	    if ((cur.getStatus(load) & DONE) == 0) {
		done = false;
	    }
	    cur = cur.next;
	}
	return done;
    }

    /**
     * Checks the error status of all of the images.
     * @return true if any of the images had an error during loading
     * @see #isErrorID
     * @see #getErrorsAny
     */
    public synchronized boolean isErrorAny() {
	MediaEntry cur = head;
	while (cur != null) {
	    if ((cur.getStatus(false) & ERRORED) != 0) {
		return true;
	    }
	    cur = cur.next;
	}
	return false;
    }

    /**
     * Returns a list of all media that have encountered an error.
     * @return an array of media objects or null if there are no errors
     * @see #isErrorAny
     * @see #getErrorsID
     */
    public synchronized Object[] getErrorsAny() {
	MediaEntry cur = head;
	int numerrors = 0;
	while (cur != null) {
	    if ((cur.getStatus(false) & ERRORED) != 0) {
		numerrors++;
	    }
	    cur = cur.next;

⌨️ 快捷键说明

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