I have some .jpg's that I'm displaying in a panel. Unfortunately they're all about 1500x1125 pixels, which is way too big for what I'm going for. Is there a programmatic way to change the resolution of these .jpg's?
You can scale an image using Graphics2D
methods (from java.awt
). This tutorial at mkyong.com explains it in depth.
Load it as an ImageIcon and this'll do the trick:
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
public static ImageIcon resizeImageIcon( ImageIcon imageIcon , Integer width , Integer height )
{
BufferedImage bufferedImage = new BufferedImage( width , height , BufferedImage.TRANSLUCENT );
Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.drawImage( imageIcon.getImage() , 0 , 0 , width , height , null );
graphics2D.dispose();
return new ImageIcon( bufferedImage , imageIcon.getDescription() );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With