Automatic photo resizing method in Java

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

Automatic photo resizing method in Java
java (method)
Java method
[Java] method
[Java] method
Call the super method in Java
Call Java method from JavaScript executed in Java
Concurrency Method in Java with basic example
What is the main method in Java?
Element operation method in appium TIPS (Java)
Partization in Java
Changes in Java 11
Java8 method reference
Rock-paper-scissors in Java
[Java] forEach method
java8 method reference
Pi in Java
[Java] Random method
[Java] split method
FizzBuzz in Java
[Java] Handling of JavaBeans in the method chain
Java method call from RPG (method call in own class)
Method name of method chain in Java Builder + α
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
JAVA DB connection method
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Java learning 2 (learning calculation method)
Comments in Java source
Java learning memo (method)
Include image in jar file with java static method
Azure functions in java
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Format XML in Java
Studying Java 8 (see method)
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Java programming (class method)
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8