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

📄 mediatracker.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @see           java.awt.MediaTracker#isErrorID(int)
     * @exception     InterruptedException  if another thread has 
     *                          interrupted this thread.
     * @since         JDK1.0 
     */
    public synchronized boolean waitForID(int id, long ms)
	throws InterruptedException
    {
	long end = System.currentTimeMillis() + ms;
	boolean first = true;
	while (true) {
	    int status = statusID(id, first, first);
	    if ((status & LOADING) == 0) {
		return (status == COMPLETE);
	    }
	    first = false;
	    long timeout;
	    if (ms == 0) {
		timeout = 0;
	    } else {
		timeout = end - System.currentTimeMillis();
		if (timeout <= 0) {
		    return false;
		}
	    }
	    wait(timeout);
	}
    }

    /**
     * Calculates and returns the bitwise inclusive <b>OR</b> of the 
     * status of all media with the specified identifier that are 
     * tracked by this media tracker. 
     * <p>
     * Possible flags defined by the 
     * <code>MediaTracker</code> class are <code>LOADING</code>, 
     * <code>ABORTED</code>, <code>ERRORED</code>, and 
     * <code>COMPLETE</code>. An image that hasn't started 
     * loading has zero as its status. 
     * <p>
     * If the value of <code>load</code> is <code>true</code>, then
     * this method starts loading any images that are not yet being loaded. 
     * @param        id   the identifier of the images to check.
     * @param        load   if <code>true</code>, start loading 
     *                            any images that are not yet being loaded.
     * @return       the bitwise inclusive <b>OR</b> of the status of 
     *                            all of the media with the specified
     *                            identifier that are being tracked.
     * @see          java.awt.MediaTracker#statusAll(boolean)
     * @see          java.awt.MediaTracker#LOADING
     * @see          java.awt.MediaTracker#ABORTED
     * @see          java.awt.MediaTracker#ERRORED
     * @see          java.awt.MediaTracker#COMPLETE
     * @since        JDK1.0
     */
    public int statusID(int id, boolean load) {
	return statusID(id, load, true);
    }

    private synchronized int statusID(int id, boolean load, boolean verify) {
	MediaEntry cur = head;
	int status = 0;
	while (cur != null) {
	    if (cur.getID() == id) {
		status = status | cur.getStatus(load, verify);
	    }
	    cur = cur.next;
	}
	return status;
    }

    /**
     * Remove the specified image from this media tracker.
     * All instances of the specified image are removed, 
     * regardless of scale or ID.
     * @param   image     the image to be removed
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
     * @since   JDK1.1
     */
    public synchronized void removeImage(Image image) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getMedia() == image) {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    /**
     * Remove the specified image from the specified tracking 
     * ID of this media tracker.
     * All instances of <code>Image</code> being tracked 
     * under the specified ID are removed regardless of scale.
     * @param      image the image to be removed.
     * @param      id the tracking ID frrom which to remove the image.
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image)
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
     * @since      JDK1.1
     */
    public synchronized void removeImage(Image image, int id) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getID() == id && cur.getMedia() == image) {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    /**
     * Remove the specified image with the specified 
     * width, height, and ID from this media tracker.
     * Only the specified instance (with any duplicates) is removed.
     * @param   image the image to be removed
     * @param   id the tracking ID from which to remove the image.
     * @param   width the width to remove (-1 for unscaled).
     * @param   height the height to remove (-1 for unscaled).
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image)
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
     * @since   JDK1.1
     */
    public synchronized void removeImage(Image image, int id,
					 int width, int height) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getID() == id && cur instanceof ImageMediaEntry
		&& ((ImageMediaEntry) cur).matches(image, width, height))
	    {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    synchronized void setDone() {
	notifyAll();
    }
}

abstract class MediaEntry {
    MediaTracker tracker;
    int ID;
    MediaEntry next;

    int status;
    boolean cancelled;

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

    MediaEntry(MediaTracker mt, int id) {
	tracker = mt;
	ID = id;
    }

    abstract Object getMedia();

    static MediaEntry insert(MediaEntry head, MediaEntry me) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    if (cur.ID > me.ID) {
		break;
	    }
	    prev = cur;
	    cur = cur.next;
	}
	me.next = cur;
	if (prev == null) {
	    head = me;
	} else {
	    prev.next = me;
	}
	return head;
    }

    int getID() {
	return ID;
    }

    abstract void startLoad();

    void cancel() {
	cancelled = true;
    }

    static final int LOADING = MediaTracker.LOADING;
    static final int ABORTED = MediaTracker.ABORTED;
    static final int ERRORED = MediaTracker.ERRORED;
    static final int COMPLETE = MediaTracker.COMPLETE;

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

    synchronized int getStatus(boolean doLoad, boolean doVerify) {
	if (doLoad && ((status & LOADSTARTED) == 0)) {
	    status = (status & ~ABORTED) | LOADING;
	    startLoad();
	}
	return status;
    }

    void setStatus(int flag) {
	synchronized (this) {
	    status = flag;
	}
	tracker.setDone();
    }
}

class ImageMediaEntry extends MediaEntry implements ImageObserver, 
java.io.Serializable {
    Image image;
    int width;
    int height;

    ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) {
	super(mt, c);
	image = img;
	width = w;
	height = h;
    }

    boolean matches(Image img, int w, int h) {
	return (image == img && width == w && height == h);
    }

    Object getMedia() {
	return image;
    }

    int getStatus(boolean doLoad, boolean doVerify) {
	if (doVerify) {
	    int flags = tracker.target.checkImage(image, width, height, this);
	    int s = parseflags(flags);
	    if (s == 0) {
		if ((status & (ERRORED | COMPLETE)) != 0) {
		    setStatus(ABORTED);
		}
	    } else if (s != status) {
		setStatus(s);
	    }
	}
	return super.getStatus(doLoad, doVerify);
    }

    void startLoad() {
	if (tracker.target.prepareImage(image, width, height, this)) {
	    setStatus(COMPLETE);
	}
    }

    int parseflags(int infoflags) {
	if ((infoflags & ERROR) != 0) {
	    return ERRORED;
	} else if ((infoflags & ABORT) != 0) {
	    return ABORTED;
	} else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
	    return COMPLETE;
	}
	return 0;
    }

    public boolean imageUpdate(Image img, int infoflags,
			       int x, int y, int w, int h) {
	if (cancelled) {
	    return false;
	}
	int s = parseflags(infoflags);
	if (s != 0 && s != status) {
	    setStatus(s);
	}
	return ((status & LOADING) != 0);
    }
}

⌨️ 快捷键说明

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