[Java] Resizing ImageIcon in Java

Solved
Calo -  
 Mustang -
Hello everyone, I have a question regarding ImageIcons in Java, and more specifically their resizing.

My application automatically creates jpg images, which can be of any size. I want to put the created image into a JLabel that has a specific size. Let's say for example 400x400 (just as an example).
How can I ensure that my image appears entirely in the JLabel (so that I don't just see a small portion of my image)?

I have heard about the setImageAutoSize method for TrayIcons; is there an equivalent for ImageIcons...?

Configuration: Mac OS X / Firefox 3.6.3

2 answers

chpeps
 
ImageIcon icon = new ImageIcon(new ImageIcon("tonImage.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
37
kami
 
You're welcome.
0
fred
 
Nickel
0
Spounchy
 
Thank you, bro.
0
Mustang
 
"Image.SCALE_DEFAULT" is used for default scaling of images, but if you believe that replacing it with a specific number like 20 yields better results, then you should feel free to use that value for your needs.
0
marc
 
ImageIcon icon = new ImageIcon("tonImage.jpg");
Image img = icon.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
IconImage newIcon = new IconImage(bi);

It's not the best way, but it's one :)
6
Calo
 
Thank you, thank you!!! You are "saving" my life!
0
swardfish
 
Thank you very much for the code! It saves my life too :)
0
Mr zlem Posted messages 29 Status Member 1
 
not clear at all
0
Ano-niles
 
Thank you!
0