Java ImageIcon Load getScaledInstance(Image img, int targetWidth, int targetHeight)

Here you can find the source of getScaledInstance(Image img, int targetWidth, int targetHeight)

Description

Redimensionne une image.

License

Apache License

Parameter

Parameter Description
img Image
targetWidth int
targetHeight int

Return

Image

Declaration

public static Image getScaledInstance(Image img, int targetWidth,
        int targetHeight) 

Method Source Code

//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;
    }
}

Related

  1. getImageIcon(String path, String description)
  2. getImageIcon(String res)
  3. getImageIcon(String resource)
  4. getImageIcon(URL u, int w)
  5. getImageIcon(URL url)
  6. loadImageIcon(byte bytes[], String descr)
  7. loadImageIcon(Class clas, String iconPath)
  8. loadImageIcon(String imageName)
  9. loadImageIcon(String url)