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?

Recommended Answers

All 4 Replies

EDIT: wrong answer, sorry

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)

Marking this solved. I think the OP has found an answer by now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.