Reading an Image or Icon from a File - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

Reading an Image or Icon from a File

Demo Code

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;

public class Main {
  public static void main(String[] args) throws Exception {
    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 {//from  w w  w  . java2 s .co m
      // The image has not been fully loaded
    }

    // This method ensures that all pixels have been loaded before returning
    image = new ImageIcon("image.gif").getImage();

    width = image.getWidth(null);
    height = image.getHeight(null);
  }
}

Related Tutorials