Java Image Height Get getHeight(Image pImage)

Here you can find the source of getHeight(Image pImage)

Description

Gets the height of an Image.

License

Open Source License

Parameter

Parameter Description
pImage an image.

Return

the height of the image, or -1 if the height could not be determined (i.e. an error occured while waiting for the image to load).

Declaration

public static int getHeight(Image pImage) 

Method Source Code


//package com.java2s;
import java.awt.*;

public class Main {
    /**//  ww  w. j  a v  a  2  s.c o  m
     * Component that can be used with the MediaTracker etc.
     */
    private static final Component NULL_COMPONENT = new Component() {
    };
    /** Our static image tracker */
    private static MediaTracker sTracker = new MediaTracker(NULL_COMPONENT);

    /**
     * Gets the height of an Image.
     * This method has the side-effect of completely loading the image.
     *
     * @param pImage an image.
     *
     * @return the height of the image, or -1 if the height could not be
     *         determined (i.e. an error occured while waiting for the
     *         image to load).
     */
    public static int getHeight(Image pImage) {
        int height = pImage.getHeight(NULL_COMPONENT);
        if (height < 0) {
            if (!waitForImage(pImage)) {
                return -1; // Error while waiting
            }
            height = pImage.getHeight(NULL_COMPONENT);
        }

        return height;
    }

    /**
     * Waits for an image to load completely.
     * Will wait forever.
     *
     * @param pImage an Image object to wait for.
     *
     * @return true if the image was loaded successfully, false if an error
     *         occured, or the wait was interrupted.
     *
     * @see #waitForImage(Image,long)
     */
    public static boolean waitForImage(Image pImage) {
        return waitForImages(new Image[] { pImage }, -1L);
    }

    /**
     * Waits for an image to load completely.
     * Will wait the specified time.
     *
     * @param pImage an Image object to wait for.
     * @param pTimeOut the time to wait, in milliseconds.
     *
     * @return true if the image was loaded successfully, false if an error
     *         occurred, or the wait was interrupted.
     *
     * @see #waitForImages(Image[],long)
     */
    public static boolean waitForImage(Image pImage, long pTimeOut) {
        return waitForImages(new Image[] { pImage }, pTimeOut);
    }

    /**
     * Waits for a number of images to load completely.
     * Will wait forever.
     *
     * @param pImages an array of Image objects to wait for.
     *
     * @return true if the images was loaded successfully, false if an error
     *         occurred, or the wait was interrupted.
     *
     * @see #waitForImages(Image[],long)
     */
    public static boolean waitForImages(Image[] pImages) {
        return waitForImages(pImages, -1L);
    }

    /**
     * Waits for a number of images to load completely.
     * Will wait the specified time.
     *
     * @param pImages an array of Image objects to wait for
     * @param pTimeOut the time to wait, in milliseconds
     *
     * @return true if the images was loaded successfully, false if an error
     *         occurred, or the wait was interrupted.
     */
    public static boolean waitForImages(Image[] pImages, long pTimeOut) {
        // TODO: Need to make sure that we don't wait for the same image many times
        // Use hashcode as id? Don't remove images from tracker? Hmmm...
        boolean success = true;

        // Create a local id for use with the mediatracker
        int imageId;

        // NOTE: This is very experimental...
        imageId = pImages.length == 1 ? System.identityHashCode(pImages[0]) : System.identityHashCode(pImages);

        // Add images to tracker
        for (Image image : pImages) {
            sTracker.addImage(image, imageId);

            // Start loading immediately
            if (sTracker.checkID(imageId, false)) {
                // Image is done, so remove again
                sTracker.removeImage(image, imageId);
            }
        }

        try {
            if (pTimeOut < 0L) {
                // Just wait
                sTracker.waitForID(imageId);
            } else {
                // Wait until timeout
                // NOTE: waitForID(int, long) return value is undocumented.
                // I assume that it returns true, if the image(s) loaded
                // successfully before the timeout, however, I always check
                // isErrorID later on, just in case...
                success = sTracker.waitForID(imageId, pTimeOut);
            }
        } catch (InterruptedException ie) {
            // Interrupted while waiting, image not loaded
            success = false;
        } finally {
            // Remove images from mediatracker
            for (Image pImage : pImages) {
                sTracker.removeImage(pImage, imageId);
            }
        }

        // If the wait was successfull, and no errors were reported for the
        // images, return true
        return success && !sTracker.isErrorID(imageId);
    }
}

Related

  1. getHeight(final File image)
  2. getHeight(final Image image)
  3. getHeight(java.awt.Image image)