0

Java has this method: getWidth(ImageObserver observer)

It returns the Width of an image. This method seems simple enough until you get to the part where an ImageObserver is requiered. What is the purpose of the ImageObserver as a parameter. Is there any other way around this?

Note that I can't just use an ImageIcon as mentioned here: Getting Height and Width of Image in Java without an ImageObserver The reason is because I will ultimately be loading my image from a JFileChooser. This is also why I can't just use constants for the Height and Width. I don't know which picture the user will be wanting to use.

Also, I don't feel like I should post my code for these two reasons

A) My code is not anywhere close to being a SSCCE piece of code: http://sscce.org/

B) This is such a general question that I feel my code would be too specific.

Community
  • 1
  • 1
Avi Caspe
  • 537
  • 4
  • 12

1 Answers1

3

From the JavaDocs

Determines the width of the image. If the width is not yet known, this method returns -1 and the specified ImageObserver object is notified later.

This basically means that if the Image is not fully realised (as it is generally loaded in a background thread), the ImageObserver will be notified when the value is known.

A better solution would be to make use of the ImageIO API as the read method won't return until the image is fully loaded (and realised) or throws a IOException if the image can't be loaded.

Have a look at Reading/Loading an Image for more details

If you "really" have to use Image and Toolkit to load them, then you should also have a look at MediaTracker, but since BufferedImage (which is returned by ImageIO.read) extends Image, there really isn't much need

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • But I am doing the ImageIO quite a while before this code is executed. When I need to get the height I will have already thrown away the location of the file. – Avi Caspe Aug 07 '15 at 03:37
  • Wait, how would ImageIO help me here, it seems that I will have to use `getWidth(ImageObserver observer)` at some point to actually get the width of the image. – Avi Caspe Aug 07 '15 at 03:40
  • So? Use `BufferedImage#getWidth` and `BufferedImage#getHeight` instead, which won't need `ImageObserver` – MadProgrammer Aug 07 '15 at 03:40
  • `ImageIO` ensures that the returned image is fully loaded/realised, so you wouldn't need `ImageObserver`, but if all you had was a reference to `Image` you could never guarantee that it was fully loaded without an `ImageObsever` or `MediaTracker` – MadProgrammer Aug 07 '15 at 03:41
  • Thank you for that. Side question: Why does Buffered image also have an option to pass and ImageObserver into the method? – Avi Caspe Aug 07 '15 at 03:43
  • Because, as I said, it extends `Image`, it's a side effect of the inheritance – MadProgrammer Aug 07 '15 at 03:45
  • And the error would be? What type is `backgroundImage`? – MadProgrammer Aug 07 '15 at 03:51
  • My IDE glitched, and It's fixed now. Comment deleted. – Avi Caspe Aug 07 '15 at 18:11