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

📄 memoryimagesource.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * image data through the ImageConsumer interface.
     * @see ImageConsumer
     */
    public void startProduction(ImageConsumer ic) {
	addConsumer(ic);
    }

    /**
     * Requests that a given ImageConsumer have the image data delivered
     * one more time in top-down, left-right order.
     * @see ImageConsumer
     */
    public void requestTopDownLeftRightResend(ImageConsumer ic) {
	// Ignored.  The data is either single frame and already in TDLR
	// format or it is multi-frame and TDLR resends aren't critical.
    }

    /**
     * Change this memory image into a multi-frame animation or a
     * single-frame static image depending on the animated parameter.
     * <p>This method should be called immediately after the
     * MemoryImageSource is constructed and before an image is
     * created with it to ensure that all ImageConsumers will
     * receive the correct multi-frame data.  If an ImageConsumer
     * is added to this ImageProducer before this flag is set then
     * that ImageConsumer will see only a snapshot of the pixel
     * data that was available when it connected.
     * @param animated true if the image is a multi-frame animation
     */
    public synchronized void setAnimated(boolean animated) {
	this.animating = animated;
	if (!animating) {
	    Enumeration enum = theConsumers.elements();
	    while (enum.hasMoreElements()) {
	    	ImageConsumer ic = (ImageConsumer) enum.nextElement();
		ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
		if (isConsumer(ic)) {
		    ic.imageComplete(ImageConsumer.IMAGEERROR);
		}
	    }
	    theConsumers.removeAllElements();
	}
    }

    /**
     * Specify whether this animated memory image should always be
     * updated by sending the complete buffer of pixels whenever
     * there is a change.
     * This flag is ignored if the animation flag is not turned on
     * through the setAnimated() method.
     * <p>This method should be called immediately after the
     * MemoryImageSource is constructed and before an image is
     * created with it to ensure that all ImageConsumers will
     * receive the correct pixel delivery hints.
     * @param fullbuffers true if the complete pixel buffer should always
     * be sent
     * @see #setAnimated
     */
    public synchronized void setFullBufferUpdates(boolean fullbuffers) {
	if (this.fullbuffers == fullbuffers) {
	    return;
	}
	this.fullbuffers = fullbuffers;
	if (animating) {
	    Enumeration enum = theConsumers.elements();
	    while (enum.hasMoreElements()) {
	    	ImageConsumer ic = (ImageConsumer) enum.nextElement();
		ic.setHints(fullbuffers
			    ? (ImageConsumer.TOPDOWNLEFTRIGHT |
			       ImageConsumer.COMPLETESCANLINES)
			    : ImageConsumer.RANDOMPIXELORDER);
	    }
	}
    }

    /**
     * Send a whole new buffer of pixels to any ImageConsumers that
     * are currently interested in the data for this image and notify
     * them that an animation frame is complete.
     * This method only has effect if the animation flag has been
     * turned on through the setAnimated() method.
     * @see ImageConsumer
     * @see #setAnimated
     */
    public void newPixels() {
	newPixels(0, 0, width, height, true);
    }

    /**
     * Send a rectangular region of the buffer of pixels to any
     * ImageConsumers that are currently interested in the data for
     * this image and notify them that an animation frame is complete.
     * This method only has effect if the animation flag has been
     * turned on through the setAnimated() method.
     * If the full buffer update flag was turned on with the
     * setFullBufferUpdates() method then the rectangle parameters
     * will be ignored and the entire buffer will always be sent.
     * @param x the x coordinate of the upper left corner of the rectangle
     * of pixels to be sent
     * @param y the y coordinate of the upper left corner of the rectangle
     * of pixels to be sent
     * @param w the width of the rectangle of pixels to be sent
     * @param h the height of the rectangle of pixels to be sent
     * @see ImageConsumer
     * @see #setAnimated
     * @see #setFullBufferUpdates
     */
    public synchronized void newPixels(int x, int y, int w, int h) {
	newPixels(x, y, w, h, true);
    }

    /**
     * Send a rectangular region of the buffer of pixels to any
     * ImageConsumers that are currently interested in the data for
     * this image.
     * If the framenotify parameter is true then the consumers are
     * also notified that an animation frame is complete.
     * This method only has effect if the animation flag has been
     * turned on through the setAnimated() method.
     * If the full buffer update flag was turned on with the
     * setFullBufferUpdates() method then the rectangle parameters
     * will be ignored and the entire buffer will always be sent.
     * @param x the x coordinate of the upper left corner of the rectangle
     * of pixels to be sent
     * @param y the y coordinate of the upper left corner of the rectangle
     * of pixels to be sent
     * @param w the width of the rectangle of pixels to be sent
     * @param h the height of the rectangle of pixels to be sent
     * @param framenotify true if the consumers should be sent a
     * SINGLEFRAMEDONE notification
     * @see ImageConsumer
     * @see #setAnimated
     * @see #setFullBufferUpdates
     */
    public synchronized void newPixels(int x, int y, int w, int h,
				       boolean framenotify) {
	if (animating) {
	    if (fullbuffers) {
		x = y = 0;
		w = width;
		h = height;
	    } else {
		if (x < 0) {
		    w += x;
		    x = 0;
		}
		if (x + w > width) {
		    w = width - x;
		}
		if (y < 0) {
		    h += y;
		    y = 0;
		}
		if (y + h > height) {
		    h = height - y;
		}
	    }
	    if ((w <= 0 || h <= 0) && !framenotify) {
		return;
	    }
	    Enumeration enum = theConsumers.elements();
	    while (enum.hasMoreElements()) {
	    	ImageConsumer ic = (ImageConsumer) enum.nextElement();
		if (w > 0 && h > 0) {
		    sendPixels(ic, x, y, w, h);
		}
		if (framenotify && isConsumer(ic)) {
		    ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
		}
	    }
	}
    }

    /**
     * Change to a new byte array to hold the pixels for this image.
     * If the animation flag has been turned on through the setAnimated()
     * method, then the new pixels will be immediately delivered to any
     * ImageConsumers that are currently interested in the data for
     * this image.
     * @see #setAnimated
     */
    public synchronized void newPixels(byte[] newpix, ColorModel newmodel,
				       int offset, int scansize) {
	this.pixels = newpix;
	this.model = newmodel;
	this.pixeloffset = offset;
	this.pixelscan = scansize;
	newPixels();
    }

    /**
     * Change to a new int array to hold the pixels for this image.
     * If the animation flag has been turned on through the setAnimated()
     * method, then the new pixels will be immediately delivered to any
     * ImageConsumers that are currently interested in the data for
     * this image.
     * @see #setAnimated
     */
    public synchronized void newPixels(int[] newpix, ColorModel newmodel,
				       int offset, int scansize) {
	this.pixels = newpix;
	this.model = newmodel;
	this.pixeloffset = offset;
	this.pixelscan = scansize;
	newPixels();
    }

    private void initConsumer(ImageConsumer ic) {
	if (isConsumer(ic)) {
	    ic.setDimensions(width, height);
	}
	if (isConsumer(ic)) {
	    ic.setProperties(properties);
	}
	if (isConsumer(ic)) {
	    ic.setColorModel(model);
	}
	if (isConsumer(ic)) {
	    ic.setHints(animating
			? (fullbuffers
			   ? (ImageConsumer.TOPDOWNLEFTRIGHT |
			      ImageConsumer.COMPLETESCANLINES)
			   : ImageConsumer.RANDOMPIXELORDER)
			: (ImageConsumer.TOPDOWNLEFTRIGHT |
			   ImageConsumer.COMPLETESCANLINES |
			   ImageConsumer.SINGLEPASS |
			   ImageConsumer.SINGLEFRAME));
	}
    }

    private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) {
	int off = pixeloffset + pixelscan * y + x;
	if (isConsumer(ic)) {
	    if (pixels instanceof byte[]) {
		ic.setPixels(x, y, w, h, model,
			     ((byte[]) pixels), off, pixelscan);
	    } else {
		ic.setPixels(x, y, w, h, model,
			     ((int[]) pixels), off, pixelscan);
	    }
	}
    }
}

⌨️ 快捷键说明

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