Java ImageIcon Resize getResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth, int iRequiredHeight)

Here you can find the source of getResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth, int iRequiredHeight)

Description

The method will scale down the sourceImageIcon in a way that it will fit into the rectangle with dimensions (iRequiredWidth, iRequiredHeight) with the original aspect ratio being preserved.

License

Apache License

Parameter

Parameter Description
iRequiredWidth Maximum desired width of the resized image.
iRequiredHeight Maximum desired height of the resized image.

Declaration

public static ImageIcon getResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth,
        int iRequiredHeight) 

Method Source Code

//package com.java2s;
/*//from  www  . j av a 2 s  .c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    /**
     * The method will scale down the <b>sourceImageIcon</b> in a way that it will
     * fit into the rectangle with dimensions <b>(iRequiredWidth,
     * iRequiredHeight)</b> with the original aspect ratio being preserved.
     * 
     * @param iRequiredWidth
     *          Maximum desired width of the resized image.
     * @param iRequiredHeight
     *          Maximum desired height of the resized image.
     */
    public static ImageIcon getResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth,
            int iRequiredHeight) {
        // *** calculate the desired width and height of the resized image ***
        int iWidth = sourceImageIcon.getIconWidth();
        int iHeight = sourceImageIcon.getIconHeight();

        float fWidthResizeRatio = iWidth / (float) iRequiredWidth;
        float fHeightResizeRatio = iHeight / (float) iRequiredHeight;

        // the chosen resize ratio will be the greatest of the two -
        // this way we will preserve aspect ratio of the sides of the
        // original image
        float fResizeRatio = Math.max(fWidthResizeRatio, fHeightResizeRatio);

        // obtain the width and height of the new image
        int iNewWidth = Math.round(iWidth / fResizeRatio);
        int iNewHeight = Math.round(iHeight / fResizeRatio);

        // *** make the actual resizing work ***
        BufferedImage resizedImage = new BufferedImage(iNewWidth, iNewHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(sourceImageIcon.getImage(), 0, 0, iNewWidth, iNewHeight, null);
        g2.dispose();
        return (new ImageIcon(resizedImage, ""));
    }
}

Related

  1. resize(ImageIcon icon, int extent)
  2. resize(ImageIcon image, int width, int height)
  3. resize(ImageIcon src, Dimension size)
  4. resize(ImageIcon src, int destWidth, int destHeight)