Here you can find the source of getScaledInstance(Image img, int targetWidth, int targetHeight)
Parameter | Description |
---|---|
img | Image |
targetWidth | int |
targetHeight | int |
public static Image getScaledInstance(Image img, int targetWidth, int targetHeight)
//package com.java2s; /*/*from ww w . jav a 2s . co m*/ * Copyright 2008-2014 by Emeric Vernat * * This file is part of Java Melody. * * 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.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; public class Main { /** * Redimensionne une ImageIcon. * @param icon ImageIcon * @param targetWidth int * @param targetHeight int * @return ImageIcon */ public static ImageIcon getScaledInstance(ImageIcon icon, int targetWidth, int targetHeight) { return new ImageIcon(getScaledInstance(icon.getImage(), targetWidth, targetHeight)); } /** * Redimensionne une image. * @param img Image * @param targetWidth int * @param targetHeight int * @return Image */ public static Image getScaledInstance(Image img, int targetWidth, int targetHeight) { final int type = BufferedImage.TYPE_INT_ARGB; Image ret = img; final int width = ret.getWidth(null); final int height = ret.getHeight(null); if (width != targetWidth && height != targetHeight) { // a priori plus performant que Image.getScaledInstance final BufferedImage scratchImage = new BufferedImage( targetWidth, targetHeight, type); final Graphics2D g2 = scratchImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, targetWidth, targetHeight, 0, 0, width, height, null); g2.dispose(); ret = scratchImage; } return ret; } }