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);
}
}
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);
}
}