I'm trying to make a class called "Sprite", which is supposed to handle spritecards and such. However, I can't seem to be able to get the image height without having an image observer object.
Do I have to pass the image observer from the main class to every sprite class just to be able to get the width?

import javax.imageio.ImageIO;

...
File file = new File("myImage.png");
BufferedImage img =  ImageIO.read(file);
int w = img.getWidth();
int h = img.getHeight();

You normally only need an ImageObserver when you are loading an image asynchronously and need to know when it's finished loading (eg in a web page). Once the Image is loaded you just need the Image itself (or a suitable public method in the class that "owns" the Image).

ps It's OK to pass null as the ImageObserver to getWidth/Height if you are confident the Image has finished loading.
int width = image.getWidth(null);


pps: Crazy Dieter - did you notice this zombie thread is a year and a half old when you tried to resurrect it? (And stupid me for not checking)

Edited 5 Years Ago by JamesCherrill: ps, pps

This question has already been answered. Start a new discussion instead.