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);
}
}
ak - 02 Mar 2004 16:25 GMT
> in the following program for displaying image I try to
> get the width/height of the image. The first attempt
[quoted text clipped - 7 lines]
> 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.
--
____________
http://reader.imagero.com the best java image reader.
Marco Schmidt - 02 Mar 2004 16:42 GMT
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
[quoted text clipped - 5 lines]
>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

Signature
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
David Cano - 02 Mar 2004 21:45 GMT
How to use Media Tracker:
image = Toolkit.getDefaultToolkit().getImage(urlPhoto);
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
> Hi all gurus:
> in the following program for displaying image I try to
[quoted text clipped - 47 lines]
> }
> }
ak - 03 Mar 2004 01:56 GMT
> How to use Media Tracker:
>
[quoted text clipped - 3 lines]
>
> 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)
--
____________
http://reader.imagero.com the best java image reader.