Java ImageIcon convert(ImageIcon icon)

Here you can find the source of convert(ImageIcon icon)

Description

Convert the Input ImageIcon into a BufferedImage with type TYPE_INT_ARGB

License

Open Source License

Parameter

Parameter Description
icon the ImageIcon to be converted

Return

the converted BufferedImaged

Declaration

public final static BufferedImage convert(ImageIcon icon) 

Method Source Code

//package com.java2s;
/**//from  ww  w .  j  ava2s  . 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 {
    /**
     * 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;
    }
}

Related

  1. absoluteDifferenceImage(ImageIcon icon1, ImageIcon icon2, double scale)
  2. componentToImageIcon(Component c, String description, boolean paintBorder)
  3. createBase64StringFromImage(ImageIcon image)
  4. createColorImageIcon(int w, int h, int c)
  5. createCursor(ImageIcon cursorIcon, Point hotSpot, String name)
  6. createCursor(ImageIcon icon, Point hotspot)