Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me in calculating image data

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was trying to find out the image width and height and tried following program but it is not giving the results. What is wrong?
Moreover what does following mean:-
1 What is ImageObserver?
2. Can it be Container?
3. What is the purpose of it here?
Code:--
******
import javax.swing.*;
import java.awt.*;
public class makePanel extends JPanel
{
JLabel background_image;
public makePanel()
{
super();
setLayout(null);
setBackground(new Color(140,199,222));
Image i= Toolkit.getDefaultToolkit().getImage("main_image.jpg");
int xx = i.getWidth(this);
int yy = i.getHeight(this);
System.out.println("xx: "+xx);
System.out.println("yy: "+yy);
}
public static void main(String[] args)
{
new makePanel();
}
}
Thanks
Arun
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,
An ImageObserver is a class that has knowledge about a requested image. The class implements the interface ImageObserver which means it contains a method imageUpdate.
In your case, you have created the image without an ImageObserver. Therefore, you can't make one up when you want to know some information about the image. You must tell the getWidth and getHeight routines that you have no ImageObserver associated with the image. Do it like this:
int xx = i.getWidth( null );
int yy = i.getHeight( null );
Regards,
Manfred.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with this also but same result. it is giving wrong image information.
can you modify the code if I am not putting you on extra work?
regards,
Arun
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,
I think I know your problem. You need to wait until the image is actually loaded before you request any information from it. You need to get used to using the following code whenever you are loading an image:

Then once the try block is complete we can get the width and height.
Using your code:

Regards,
Manfred.
[ February 19, 2002: Message edited by: Manfred Leonhardt ]
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Manfred. It works fine with this.
regards,
Arun
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!