Java ImageIcon Scale scaleIcon(final ImageIcon icon)

Here you can find the source of scaleIcon(final ImageIcon icon)

Description

Returns a scaled icon based on the reduced font size

License

Open Source License

Parameter

Parameter Description
icon ImageIcon to scale

Return

the scaled Icon

Declaration

public static Icon scaleIcon(final ImageIcon icon) 

Method Source Code

//package com.java2s;
/*/*w  w w .j  a  va2 s .  c  o  m*/
 * jGnash, a personal finance application
 * Copyright (C) 2001-2015 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Image;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {
    private static float growthPercentage = 1f;

    /**
     * Returns a scaled icon based on the reduced font size
     * 
     * @param icon {@code ImageIcon} to scale
     * @return the scaled {@code Icon}
     */
    public static Icon scaleIcon(final ImageIcon icon) {

        int scaledWidth = (int) Math.floor(icon.getIconWidth() * growthPercentage);
        int scaledHeight = (int) Math.floor(icon.getIconHeight() * growthPercentage);

        Image scaledInstance = icon.getImage().getScaledInstance(scaledWidth, scaledHeight,
                java.awt.Image.SCALE_SMOOTH);

        return new ImageIcon(scaledInstance);
    }
}

Related

  1. rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
  2. rescaleImageIcon(ImageIcon image, int size)
  3. scale(ImageIcon icon, int maxWidth, int maxHeight)
  4. scale(ImageIcon icon, int newHeight, int newWidth)
  5. scale(ImageIcon original, int width, int height)
  6. scaleIcon(ImageIcon icon, int size)
  7. scaleIcon(int width, int height, ImageIcon img)
  8. scaleIconTo(ImageIcon icon, int width, int height)
  9. scaleImage(ImageIcon i, int x, int y)