Here you can find the source of resizeImageIcon(ImageIcon icon, int width, int height)
Parameter | Description |
---|---|
icon | the input ImageIcon |
width | the new width |
height | the new height |
public final static ImageIcon resizeImageIcon(ImageIcon icon, int width, int height)
//package com.java2s; /**//w w w . jav a 2 s .c om * This file is part of evdev-java - Java implementation. * * evdev-java - Java implementation 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. * * evdev-java - Java implementation 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 evdev-java - Java implementation. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; public class Main { /** * Resize the input ImageIcon to a new size * * @param icon the input ImageIcon * @param width the new width * @param height the new height * @return the resized ImageIcon */ public final static ImageIcon resizeImageIcon(ImageIcon icon, int width, int height) { BufferedImage buffered = convert(icon); BufferedImage resized = resizeImage(buffered, width, height); ImageIcon result = new ImageIcon(resized); return result; } /** * Convert the Input {@link ImageIcon} into a {@link BufferedImage} with type TYPE_INT_ARGB * * @param icon the {@link ImageIcon} to be converted * @return the converted BufferedImaged */ public final static BufferedImage convert(ImageIcon icon) { if (icon == null) { throw new IllegalArgumentException("ImageIcon is null"); } BufferedImage buffered = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = buffered.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); // paint the Icon to the BufferedImage. icon.paintIcon(null, g, 0, 0); g.dispose(); return buffered; } /** * Resize the input BufferedImage to the specified width and height * * @param img the BufferedImage to be resized * @param width the new width of the resized BufferedImage * @param height the new height of the resized BufferedImage * @return */ public final static BufferedImage resizeImage(BufferedImage img, int width, int height) { if (img == null) { throw new IllegalArgumentException("Img parameter could not be null"); } BufferedImage resized = new BufferedImage(width, height, img.getType()); Graphics2D g = resized.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null); // g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null); g.dispose(); return resized; } }