| Author |
Resize ImageIcon
|
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
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!!
|
 |
Daniel Searson
Ranch Hand
Joined: Dec 03, 2000
Posts: 83
|
|
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
|
- Daniel
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
Oh man. You are a saint. Thank you so much!!! The code was correct by the way too.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
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
Joined: Jul 11, 2001
Posts: 15230
|
|
I found a better way to do this:
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4205
|
|
Lavric Daniel, Your post was moved to a new topic. Split from "Resize ImageIcon"
|
luck, db
There are no new questions, but there may be new answers.
|
 |
qaman walala
Greenhorn
Joined: Sep 05, 2012
Posts: 2
|
|
|
|
 |
 |
|
|
subject: Resize ImageIcon
|
|
|