Here you can find the source of getSize(int width, int height, Image image)
public static int[] getSize(int width, int height, Image image)
//package com.java2s; import java.awt.Image; public class Main { public static int[] getSize(int width, int height, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); double scaling = getScaling(targetWidth, targetHeight, width, height); long standardWidth = Math.round(targetWidth * scaling); long standardHeight = Math.round(targetHeight * scaling); return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) }; }//from w w w . j a v a 2 s . com public static int[] getSize(float scale, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); long standardWidth = Math.round(targetWidth * scale); long standardHeight = Math.round(targetHeight * scale); return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) }; } public static int[] getSize(int width, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); long height = Math.round((targetHeight * width) / (targetWidth * 1.00f)); return new int[] { width, Integer.parseInt(String.valueOf(height)) }; } public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) { double widthScaling = 0d; double heightScaling = 0d; if (targetWidth > standardWidth) { widthScaling = standardWidth / (targetWidth * 1.00d); } else { widthScaling = 1d; } if (targetHeight > standardHeight) { heightScaling = standardHeight / (targetHeight * 1.00d); } else { heightScaling = 1d; } return Math.min(widthScaling, heightScaling); } }