Java ImageIcon Create toImageIcon(Icon icon)

Here you can find the source of toImageIcon(Icon icon)

Description

Returns an ImageIcon based on a given Icon object.

License

LGPL

Parameter

Parameter Description
icon input icon

Return

image icon

Declaration

public static ImageIcon toImageIcon(Icon icon) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Main {
    private static Component dummyComponent_;

    /**/*w  w w . jav  a2s.  c  om*/
     * Returns an ImageIcon based on a given Icon object.  If the supplied
     * <code>icon</code> is already an ImageIcon, it is returned.  Otherwise,
     * it is painted to an Image and an ImageIcon is constructed from that.
     * The reason this is useful is that some Swing components will only
     * grey out disabled icons if they are ImageIcon subclasses (which is
     * naughty).
     *
     * @param  icon  input icon
     * @return   image icon
     */
    public static ImageIcon toImageIcon(Icon icon) {
        if (icon instanceof ImageIcon) {
            return (ImageIcon) icon;
        } else {
            return new ImageIcon(createImage(icon));
        }
    }

    /**
     * Returns an image got by drawing an Icon.
     *
     * @param  icon
     * @return  image
     */
    private static BufferedImage createImage(Icon icon) {
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();

        /* Create an image to draw on. */
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();

        /* This ought to clear it to transparent white (00ffffff).
         * But in fact it seems to remain transparent black (00000000).
         * That shouldn't make a difference, but in some cases PNGs
         * written with a transparent background can end up displaying
         * with black backgrounds when they end up in PDFs (probably
         * a bug in Fop?).  I have wasted a vast amount of time trying
         * to get this right and so far failed.  But if you just want
         * to copy an existing image you can use AlphaComposite.SRC
         * for the actual painting. */
        Color color = g2.getColor();
        Composite compos = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
        g2.setColor(new Color(1f, 1f, 1f, 0f));
        g2.fillRect(0, 0, w, h);
        g2.setColor(color);
        g2.setComposite(compos);

        /* Paint the icon. */
        icon.paintIcon(getDummyComponent(), g2, 0, 0);

        /* Tidy up and return the image. */
        g2.dispose();
        return image;
    }

    /**
     * Provides an empty component.
     *
     * @return   lazily constructed component
     */
    private static Component getDummyComponent() {
        if (dummyComponent_ == null) {
            dummyComponent_ = new JPanel();
        }
        return dummyComponent_;
    }
}

Related

  1. createImageIconJLabel(ClassLoader classloader, String path, String description, String text)
  2. createImageIcons(java.awt.Image... images)
  3. getIconImage(Icon icon)
  4. getIconImage(String path)
  5. toImageIcon(File file, int width, int height)
  6. toImageIcon(URL resource, int width, int height)