The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > Images  [4 examples]

e594. Reading an Image or Icon from a File

See also e595 Drawing an Image.
    // This call returns immediately and pixels are loaded in the background
    Image image = Toolkit.getDefaultToolkit().getImage("image.gif");
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    
    if (width >= 0) {
        // The image has been fully loaded
    } else {
        // The image has not been fully loaded
    }
    
    
    // This method ensures that all pixels have been loaded before returning
    image = new ImageIcon("image.gif").getImage();
    
    // Get the dimensions of the image; these will be non-negative
    width = image.getWidth(null);
    height = image.getHeight(null);

 Related Examples
e595. Drawing an Image
e596. Scaling, Shearing, Translating, and Rotating a Drawn Image
e597. Creating a Gray Version of an Icon

See also: Colors    Components    Containers    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Shapes    Simulating Events    Text    The Screen   


© 2002 Addison-Wesley.