Java Image Scale getScaledIcon(final Image image, final double scale)

Here you can find the source of getScaledIcon(final Image image, final double scale)

Description

get Scaled Icon

License

Open Source License

Declaration

public static ImageIcon getScaledIcon(final Image image, final double scale) 

Method Source Code

//package com.java2s;
/**/*from   w ww.ja  v a 2 s  . c  om*/
 *                    
 * @author greg (at) myrobotlab.org
 *  
 * This file is part of MyRobotLab (http://myrobotlab.org).
 *
 * MyRobotLab 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 2 of the License, or
 * (at your option) any later version (subject to the "Classpath" exception
 * as provided in the LICENSE.txt file that accompanied this code).
 *
 * MyRobotLab is distributed in the hope that it will be useful or fun,
 * 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.
 *
 * All libraries in thirdParty bundle are subject to their own license
 * requirements - please refer to http://myrobotlab.org/libraries for 
 * details.
 * 
 * Enjoy !
 * 
 * */

import java.awt.Component;
import java.awt.Graphics;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon getScaledIcon(final Image image, final double scale) {
        ImageIcon scaledIcon = new ImageIcon(image) {
            private static final long serialVersionUID = 1L;

            public int getIconWidth() {
                return (int) (image.getWidth(null) * scale);
            }

            public int getIconHeight() {
                return (int) (image.getHeight(null) * scale);
            }

            public void paintIcon(Component c, Graphics g, int x, int y) {
                g.drawImage(image, x, y, getIconWidth(), getIconHeight(), c);
            }
        };
        return scaledIcon;
    }
}

Related

  1. getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)
  2. getScaledImage(Image image, int maxWidth, int maxHeight)
  3. getScaledImage(Image imagen, double scaleW, double scaleH)
  4. getScaledImage(Image srcImg, int w, int h)