Problem with getting width/height of image

K

King W.Wang

Hi all gurus:
in the following program for displaying image I try to
get the width/height of the image. The first attempt
(marked with first) before setVisible() returns -1/-1,
only the second attempt returns the correct values. But
if I remove the first one, the second one returns ALSO
-1/-1. I don't understand this behavior. I thought that
after the variable "image" is instantiated (in the
constructor, the width/height data must already be
available.
What measure must be taken in order to get the correct
data before the frame becomes visible? Thanks in advance

k.wang

// the program:
import java.awt.*;
import javax.swing.*;
public class ImgDisplayer extends JPanel {
String imageFile = "images/rocketship.gif";
Image image;
public ImgDisplayer() {
super();
image = Toolkit.getDefaultToolkit().getImage(imageFile);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
private int getImageWidth() {
return image.getWidth(this);
}
private int getImageHeight() {
return image.getHeight(this);
}
public static void main(String[] args) {
int width, height;
JFrame frame = new JFrame("Image Displayer");
ImgDisplayer imgDisp = new ImgDisplayer();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(imgDisp);
frame.setSize(120, 120);
width = imgDisp.getImageWidth(); // First
height = imgDisp.getImageHeight();
System.out.println("First " + width + " " + height);
frame.setVisible(true);
width = imgDisp.getImageWidth(); // Second
height = imgDisp.getImageHeight();
System.out.println("Second " + width + " " + height);
}
}
 
A

ak

in the following program for displaying image I try to
get the width/height of the image. The first attempt
(marked with first) before setVisible() returns -1/-1,
only the second attempt returns the correct values. But
if I remove the first one, the second one returns ALSO
-1/-1. I don't understand this behavior. I thought that
after the variable "image" is instantiated (in the
constructor, the width/height data must already be
available.
What measure must be taken in order to get the correct
data before the frame becomes visible? Thanks in advance
you have to load image first.
width/height -1 means that image is not yet loaded.

use ImageIO or put your image into ImageIcon.
 
M

Marco Schmidt

fup2 comp.lang.java.gui

Please do not crosspost unless you have a really good reason! If you
must do it, set a followup-to header that points to the group where
the discussion is to follow.

King W.Wang:
in the following program for displaying image I try to
get the width/height of the image. The first attempt
(marked with first) before setVisible() returns -1/-1,
only the second attempt returns the correct values. But
if I remove the first one, the second one returns ALSO
-1/-1. I don't understand this behavior. I thought that
after the variable "image" is instantiated (in the
constructor, the width/height data must already be
available.

No. The image is loaded in the background, and correct width and
height are available only after a short while.
What measure must be taken in order to get the correct
data before the frame becomes visible? Thanks in advance

One approach is to load the image first, using a MediaTracker. See
<http://www.geocities.com/marcoschmidt.geo/java-load-image-toolkit.html>.

You could also retrieve the resolution first with ImageInfo, which is
quick because it never touches the image data:
<http://www.geocities.com/marcoschmidt.geo/image-info.html>.

Or just load the image and adjust the frame size once resolution
information is available.

Regards,
Marco
 
D

David Cano

How to use Media Tracker:

image = Toolkit.getDefaultToolkit().getImage(urlPhoto);

MediaTracker mediaTracker = new MediaTracker(this);

mediaTracker.addImage(image, 0);
 
A

ak

How to use Media Tracker:
image = Toolkit.getDefaultToolkit().getImage(urlPhoto);

MediaTracker mediaTracker = new MediaTracker(this);

mediaTracker.addImage(image, 0);
a) you forgot mediaTracker.waitForID / mediaTracke.waitForAll
b) just create ImageIcon and you have the same thing but with less code
(ImageIcon loads image)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top