Example usage for java.awt Toolkit checkImage

List of usage examples for java.awt Toolkit checkImage

Introduction

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

Prototype

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

Source Link

Document

Indicates the construction status of a specified image that is being prepared for display.

Usage

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 w w . j a  va2 s .c  om*/

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;
            }
        }
    }
}

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./*from   www  .jav  a 2 s .  c om*/
 *
 * @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;
}