Example usage for java.awt Toolkit prepareImage

List of usage examples for java.awt Toolkit prepareImage

Introduction

In this page you can find the example usage for java.awt Toolkit prepareImage.

Prototype

public abstract boolean prepareImage(Image image, int width, int height, ImageObserver observer);

Source Link

Document

Prepares an image for rendering.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    ImageObserver myObserver = new ImageObserver() {
        public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
            if ((flags & HEIGHT) != 0)
                System.out.println("Image height = " + height);
            if ((flags & WIDTH) != 0)
                System.out.println("Image width = " + width);
            if ((flags & FRAMEBITS) != 0)
                System.out.println("Another frame finished.");
            if ((flags & SOMEBITS) != 0)
                System.out.println("Image section :" + new Rectangle(x, y, width, height));
            if ((flags & ALLBITS) != 0)
                System.out.println("Image finished!");
            if ((flags & ABORT) != 0)
                System.out.println("Image load aborted...");
            return true;
        }/*from  w ww .j  a v  a  2s .  co m*/
    };

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image img = toolkit.getImage(args[0]);
    toolkit.prepareImage(img, -1, -1, myObserver);
}

From source file:ImageUtilities.java

/**
 * Starts loading the given images, returns only when all the images are done
 * loading. If you just need to preload one Image, use the preload(Image) method
 * instead./*  w ww  .  j  a v  a 2 s.  c  o m*/
 *
 * @return An array specifying the loading result of each image. Possible values
 * are {@link #COMPLETE}, {@link #ERRORED} and {@link #ABORTED}.
 *
 * @see #preload(Image)
 */

public static int[] preload(Image[] images, int[] results) {
    Object[] locks = new Object[images.length];
    ImageLoadObserver[] loadObservers = new ImageLoadObserver[images.length];
    if ((results == null) || (results.length < images.length))
        results = new int[images.length];
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    for (int i = 0; i < images.length; i++) {
        locks[i] = new Object();
        loadObservers[i] = new ImageLoadObserver(locks[i]);
        toolkit.prepareImage(images[i], -1, -1, loadObservers[i]);
    }

    for (int i = 0; i < images.length; i++) {
        synchronized (locks[i]) {
            int result = toolkit.checkImage(images[i], -1, -1, null);

            if ((result & ImageObserver.ALLBITS) != 0) {
                results[i] = COMPLETE;
                continue;
            }
            if ((result & ImageObserver.ERROR) != 0) {
                results[i] = ERRORED;
                continue;
            }
            if ((result & ImageObserver.ABORT) != 0) {
                results[i] = ABORTED;
                continue;
            }

            try {
                locks[i].wait();
                results[i] = loadObservers[i].getResult();
            } catch (InterruptedException e) {
                results[i] = INTERRUPTED;
            }
        }
    }

    return results;
}

From source file:ImageUtilities.java

/**
 * Starts loading the given image, returns only when it's done loading.
 * Note that it's much more efficient to preload a lot of images at once using
 * the preload(Image []) method instead of this one.
 *
 * @return The result of the image loading, either {@link #COMPLETE},
 * {@link #ERRORED}, {@link #ABORTED} or {@link #INTERRUPTED}.
 *
 * @see #preload(java.awt.Image [], int [])
 *//*from w ww.j av a  2  s . co  m*/

public static int preload(Image image) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();

    // Check if already loaded
    if ((toolkit.checkImage(image, -1, -1, null) & ImageObserver.ALLBITS) != 0)
        return COMPLETE;

    Object lock = new Object();
    synchronized (lock) {
        while (true) {
            ImageLoadObserver observer = new ImageLoadObserver(lock);
            toolkit.prepareImage(image, -1, -1, observer);
            int result = toolkit.checkImage(image, -1, -1, null);
            if ((result & ImageObserver.ALLBITS) != 0)
                return COMPLETE;
            if ((result & ImageObserver.ERROR) != 0)
                return ERRORED;
            if ((result & ImageObserver.ABORT) != 0)
                return ABORTED;

            try {
                lock.wait();
                return observer.getResult();
            } catch (InterruptedException e) {
                return INTERRUPTED;
            }
        }
    }
}