• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Resize ImageIcon

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know if there is a way to resize an ImageIcon when placing it on a Button so that I don't have to physically rezize the image file?? I am writing an Application, not an applet, so the getImage method won't work.
Thanks for any help!!
 
Ranch Hand
Posts: 83
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here's one way of doing it.
You load your image straight into the ImageIcon using the constructor that takes a file name as an argument like:
ImageIcon icon = new ImageIcon("whatever.jpg");
Make sure the reference you create is an ImageIcon reference. Then use getImage() to grab the image from the ImageIcon:
Image img = icon.getImage();
Now create a buffered image the same size as the image:
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Then blit the icon image to the buffered image, and resize it as you do so:
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
(The code above may be incorrect - check the docs)
Now recreate the IconImage with the new buffered image:
IconImage newIcon = new IconImage(bi);
Hope that helps.
- Daniel
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh man. You are a saint. Thank you so much!!!
The code was correct by the way too.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another problem now. I resized the image, and set it to the new ImageIcon, but I have to actually position the image on the button.

the 140, 199 is positioning the Image on the button. If I set it to 0, 0, the image is off the button somewhere and can't be seen.
Does that make since? I just don't know why it doesn't center the image with the ImageIcon like normally.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a better way to do this:

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lavric Daniel,
Your post was moved to a new topic.
Split from "Resize ImageIcon"
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Searson wrote:Ok, here's one way of doing it.
You load your image straight into the ImageIcon using the constructor that takes a file name as an argument like:
ImageIcon icon = new ImageIcon("whatever.jpg");
Make sure the reference you create is an ImageIcon reference. Then use getImage() to grab the image from the ImageIcon:
Image img = icon.getImage();
Now create a buffered image the same size as the image:
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Then blit the icon image to the buffered image, and resize it as you do so:
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
(The code above may be incorrect - check the docs)
Now recreate the IconImage with the new buffered image:
IconImage newIcon = new IconImage(bi);
Hope that helps.
- Daniel




I like this option. All of the posts I have read elsewhere recommend using the BufferedImage/Graphics.drawImage approach.

In this situation, it looks like you do actually need a resized image. In the project I am working on, I have been using a modified JLabel to use a resized icon. You may not need the whole IconLabel class, but it does implement the resize technique mentioned above. For added bonus (and for satisfying my OCD), I have pieced together some logic that also scales the image to within a certain size, maintaining its aspect ratio (i.e., I used http://stackoverflow.com/questions/10245220/java-image-resize-maintain-aspect-ratio). That way, you can pre-determine how big of a space you want your image to take without compromising its aspect ratio. The getScaledDimension is taken from that URL, and it does a better job of explaining how it resizes an image and maintains its aspect ratio.

In this JLabel-extending class, the real tricks are to override getPreferredSize and the paintComponent methods, where I do the image resizing.

 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use the Stretch Icon which will scale to fix the entire space or scale to keep the aspect ratio.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sir you solved my issue :
Heres a more precise way :

ImageIcon icon =new ImageIcon("c:\\whatever");
Image newimg = icon.getImage().getScaledInstance(40,40, java.awt.Image.SCALE_SMOOTH);
icon=new ImageIcon(newimg);
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also replace the last line with icon.setImage(newimg).
reply
    Bookmark Topic Watch Topic
  • New Topic