• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Image Dimensions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the width/height of an image.
The image is named 'image'. I'm using image.getWidth(this); (same for height) and it returns -1. Anyone got any tips?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use java.awt.Image (or the equivalent, look up in the javadocs). Notably, if you are running under *nix wihout an X session open you will need to specify headless execution on the commandline or it will get crnaky.
-Brian
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.awt.Image API explains why -1 is returned. This API is actually rather tricky - it's asynchronous. This means that you can (and often do) get an Image object that's not yet fully loaded. Methods like getHeight() may return -1 to indicate that the height() data is not yet available - try again later. The class was designed this way because frequently you might be downloading images from a slow source, and you have the option to use those parts of the image data that are available, as they become available, rather than waiting for the whole thing. (A typical example - if you're laying out a web page, you can set aside space for an image once you know it's height and width - even though you can't display the image yet.) Anyway, in order to use Image effectively you typically have to make use of the ImageObserver class to create an object that will notify you when the data you need is loaded. Here's a class I made some time ago to do this:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic