wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Resizing the image and displaying it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resizing the image and displaying it" Watch "Resizing the image and displaying it" New topic
Author

Resizing the image and displaying it

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi all,
I have to disply an image in 200 * 200 space on a JPanel. Some images or too big for that space some images are too small for that space, I don't have a clue about how to display them? can anyone give me some pointers?
Thanks,
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

How are you painting these images? Just using ImageIcon or painting them in an overridden paintComponent() method? What do you want to do with images that aren't the correct size? Resize them up or down to match that size? Crop large images so only the upper right corner shows, or allow them to be scrolled? Center smaller images and add space around them?

If you just want to resize them all to 200x200 you can just call the getScaledInstance( int width, int height, int hints ) method of the Image class. Hints are defined as static final ints in the Image class also - SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH, SCALE_REPLICATE, and SCALE_AREA_AVERAGING.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your reply.
Right now, I am trying to use imageicon to display the images. If the images are small for 200 * 200 space then I would like to enlarge them if they are big then I would like to resize them up or down depending on the image height and width.
I will try to use the method you suggested and see how it works.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
I am trying to use getScaledInstance method in a small prg.
this is what I have in my paint method.

The image2's width is 200, but height is always -1 why? Original image's height is 640, widht is 480.
Can you anyone tell me how to use the getScaledInstance method?
thanks,
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You are using getScaledInstance() correctly, but you shouldn't create a scaled image every time through the paint method. Create your scaled image one time and keep a reference to it. (In fact, if you're using ImageIcon, you shouldn't have to use the paint method at all... ) I'm assuming that you're getting a -1 for height because you have a reference to the image, but it hasn't been loaded yet. The same kind of thing happens when you load an image from a file... the JVM creates a reference but waits until it actually needs to show the image before loading the actual image data. You can use the MediaTracker class to force an image to be loaded before it is visible. Then you can get correct sizes from the image.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks a lot Nathan for your help.
I have used another panel to display the image using getScaledinstance() thanks again,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resizing the image and displaying it
 
Similar Threads
Tough Question on Images and Tiles
Hiding images bound to sliders
Height and Width proportional algorithm?
"Image Fetcher" Threads??
Image Quality Checking in Java