Image.getWidth(null) and image..getHeight(null)  returns -1

Hi ,
Plz tell me whats wrong with code
import java.awt.Image;
import java.awt.Toolkit;
public class ImgSize {
    public static void main(String args[]) {
        Image image = Toolkit.getDefaultToolkit().getImage("Picture.jpg");
        double width = image.getWidth(null);
        double height = image.getHeight(null);
        System.out.println("width :" + width + "-- height :" + height);
        getImageDimesion("Picture.jpg");
    public static void getImageDimesion(String abc) {
        Image image = Toolkit.getDefaultToolkit().getImage(abc);
        double width = image.getWidth(null);
        double height = image.getHeight(null);
        System.out.println("width :" + width + "-- height :" + height);
}output:
width :-1.0-- height :-1.0
width :2592.0-- height :1944.0
There is no difference in main function getWidth/ getHeight and function getImageDimension getWidth/ getHeight
If u remove getWidth and getHeigth in the main function i.e comment the 1st four lines in main function then the output is
Output
width :-1.0-- height :-1.0
Plz help in understanding this.
Thanks
Venkat
Advertisement
Reply

Toolkit images are not loaded until they are first drawn or you request a property from it (such as getWidth). To force the image to load and wait for it to finish loading, you can use the MediaTracker class. The ImageIcon class has one built in.
Image image = Toolkit.getDefaultToolkit().getImage("Picture.jpg");
new ImageIcon(image); //loads the image