[Java] Resizing ImageIcon in Java
Solved
Calo
-
Mustang -
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
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
ImageIcon icon = new ImageIcon(new ImageIcon("tonImage.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
kami
You're welcome.
fred
Nickel
Spounchy
Thank you, bro.
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.
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 :)
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 :)