Java ImageIcon Scale rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)

Here you can find the source of rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)

Description

Rescales an icon.

License

Apache License

Parameter

Parameter Description
src the original icon.
newMinSize the minimum size of the new icon. The width and height of the returned icon will be at least the width and height respectively of newMinSize.
an ImageObserver.

Return

the rescaled icon.

Declaration

public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012-2014 Esri//from w  w  w  . j a  va  2 s. com
 * 
 *  Licensed 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.Dimension;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Rescales an icon.
     * @param src the original icon.
     * @param newMinSize the minimum size of the new icon. The width and height of
     *                   the returned icon will be at least the width and height
     *                   respectively of newMinSize.
     * @param an ImageObserver.
     * @return the rescaled icon.
     */
    public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer) {
        double widthRatio = newMinSize.getWidth() / (double) src.getIconWidth();
        double heightRatio = newMinSize.getHeight() / (double) src.getIconHeight();
        double scale = widthRatio > heightRatio ? widthRatio : heightRatio;

        int w = (int) Math.round(scale * src.getIconWidth());
        int h = (int) Math.round(scale * src.getIconHeight());
        BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = dst.createGraphics();
        g2.drawImage(src.getImage(), 0, 0, w, h, observer);
        g2.dispose();
        return new ImageIcon(dst);
    }
}

Related

  1. getScaledImageIcon(final String location, final int width, final int height)
  2. getScaledImageIconHeight(ImageIcon ii, int h, boolean incr)
  3. rescaleImageIcon(ImageIcon image, int size)
  4. scale(ImageIcon icon, int maxWidth, int maxHeight)
  5. scale(ImageIcon icon, int newHeight, int newWidth)
  6. scale(ImageIcon original, int width, int height)