Java ImageIcon Scale scale(ImageIcon icon, int newHeight, int newWidth)

Here you can find the source of scale(ImageIcon icon, int newHeight, int newWidth)

Description

Returns a scaled down image if the height or width is smaller than the image size.

License

Open Source License

Parameter

Parameter Description
icon the image icon.
newHeight the preferred height.
newWidth the preferred width.

Return

the icon.

Declaration

public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) 

Method Source Code

//package com.java2s;
/**/*from   ww w .  j  a v a2s  . c o m*/
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 * 
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * 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.Image;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Returns a scaled down image if the height or width is smaller than the
     * image size.
     * 
     * @param icon
     *            the image icon.
     * @param newHeight
     *            the preferred height.
     * @param newWidth
     *            the preferred width.
     * @return the icon.
     */
    public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) {
        Image img = icon.getImage();
        int height = icon.getIconHeight();
        int width = icon.getIconWidth();
        boolean scaleHeight = height * newWidth > width * newHeight;
        if (height > newHeight) {
            // Too tall
            if (width <= newWidth || scaleHeight) {
                // Width is okay or height is limiting factor due to aspect
                // ratio
                height = newHeight;
                width = -1;
            } else {
                // Width is limiting factor due to aspect ratio
                height = -1;
                width = newWidth;
            }
        } else if (width > newWidth) {
            // Too wide and height is okay
            height = -1;
            width = newWidth;
        } else if (scaleHeight) {
            // Height is limiting factor due to aspect ratio
            height = newHeight;
            width = -1;
        } else {
            // Width is limiting factor due to aspect ratio
            height = -1;
            width = newWidth;
        }
        img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        return new ImageIcon(img);
    }
}

Related

  1. getScaledImageIcon(final String location, final int width, final int height)
  2. getScaledImageIconHeight(ImageIcon ii, int h, boolean incr)
  3. rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
  4. rescaleImageIcon(ImageIcon image, int size)
  5. scale(ImageIcon icon, int maxWidth, int maxHeight)
  6. scale(ImageIcon original, int width, int height)
  7. scaleIcon(final ImageIcon icon)
  8. scaleIcon(ImageIcon icon, int size)
  9. scaleIcon(int width, int height, ImageIcon img)