Here you can find the source of getWidth(Image pImage)
Parameter | Description |
---|---|
pImage | an image. |
public static int getWidth(Image pImage)
//package com.java2s; import java.awt.*; public class Main { /**//from w w w . j a v a 2s.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 width of an Image. * This method has the side-effect of completely loading the image. * * @param pImage an image. * * @return the width of the image, or -1 if the width could not be * determined (i.e. an error occured while waiting for the * image to load). */ public static int getWidth(Image pImage) { int width = pImage.getWidth(NULL_COMPONENT); if (width < 0) { if (!waitForImage(pImage)) { return -1; // Error while waiting } width = pImage.getWidth(NULL_COMPONENT); } return width; } /** * 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); } }