AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with getting width/height of image

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
King W.Wang
Guest





PostPosted: Tue Mar 02, 2004 3:45 pm    Post subject: Problem with getting width/height of image Reply with quote



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);
}
}
Back to top
ak
Guest





PostPosted: Tue Mar 02, 2004 4:25 pm    Post subject: Re: Problem with getting width/height of image Reply with quote



Quote:
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.

--

____________

http://reader.imagero.com the best java image reader.



Back to top
Marco Schmidt
Guest





PostPosted: Tue Mar 02, 2004 4:42 pm    Post subject: Re: Problem with getting width/height of image Reply with quote



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:

Quote:
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.

Quote:
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
--
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

Back to top
David Cano
Guest





PostPosted: Tue Mar 02, 2004 9:45 pm    Post subject: Re: Problem with getting width/height of image Reply with quote

How to use Media Tracker:

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

MediaTracker mediaTracker = new MediaTracker(this);

mediaTracker.addImage(image, 0);





"King W.Wang" <king_wwang (AT) yahoo (DOT) de> escribió en el mensaje
news:51d6351d.0403020745.2a6f97a (AT) posting (DOT) google.com...
Quote:
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);
}
}



Back to top
ak
Guest





PostPosted: Wed Mar 03, 2004 1:56 am    Post subject: Re: Problem with getting width/height of image Reply with quote

Quote:
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)

--

____________

http://reader.imagero.com the best java image reader.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.