Here you can find the source of resizeImageAspectRatio(int width, int height, ImageIcon orig_img)
Parameter | Description |
---|---|
width | the width |
height | the height |
orig_img | the orig_img |
public static Image resizeImageAspectRatio(int width, int height, ImageIcon orig_img)
//package com.java2s; //License from project: Open Source License import java.awt.Image; import javax.swing.ImageIcon; public class Main { /**//from w w w .ja v a 2 s . com * Resize image aspect ratio. * * @param width * the width * @param height * the height * @param orig_img * the orig_img * * @return the image */ public static Image resizeImageAspectRatio(int width, int height, ImageIcon orig_img) { Image img = null; float disp_aspect_ratio = (height * 1.0F) / width; float aspect_ratio = (orig_img.getIconHeight() * 1.0F) / orig_img.getIconWidth(); if (aspect_ratio == disp_aspect_ratio) { img = orig_img.getImage().getScaledInstance(width, height, 0); } else if (aspect_ratio < disp_aspect_ratio) { // landscape img = orig_img.getImage().getScaledInstance(width, (int) (height * aspect_ratio / disp_aspect_ratio), 0); } else { // potrait img = orig_img.getImage().getScaledInstance((int) (width * disp_aspect_ratio / aspect_ratio), height, 0); } return img; } }