Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resizing the image and displaying it

 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Nathan for your help.
I have used another panel to display the image using getScaledinstance() thanks again,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic