When using an image as an icon in Java, the image size and the size of the icon content are different! do not you think so?
This time I will introduce a method that automatically returns the converted image when you specify the icon and the size you want to resize. First of all, the whole code
PictureBuilder.java
public class PictureBuilder {
public static ImageIcon resizeIcon(ImageIcon icon, int w, int h){
Image CGresize = icon.getImage();
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(CGresize, 0, 0, w, h, null);
g2.dispose();
ImageIcon resized = new ImageIcon();
resized.setImage(resizedImg);
return resized;
}
}
Since it is a static function, it can be used even from an external class.
ImageIcon icon = new ImageIcon("image.jpg ");
Imageicon resized = PictureBuilder.resizeIcon(icon,100,200);
You can use it like this.
・ I want to resize easily for the time being. ・ I want to use an image for the icon, but the size is different and it is difficult to use ・ I want to use the same image with icons of various sizes
Recommended for such people.
Recommended Posts