Java ImageIcon Create createImageIcon(final Icon icon)

Here you can find the source of createImageIcon(final Icon icon)

Description

Creates an ImageIcon from any Icon.

License

Open Source License

Parameter

Parameter Description
icon Icon with complex painting operation

Return

ImageIcon based on a BufferedImage

Declaration

public static ImageIcon createImageIcon(final Icon icon) 

Method Source Code


//package com.java2s;
/*// w  ww .j  av  a  2  s. c o m
 * jGnash, a personal finance application
 * Copyright (C) 2001-2015 Craig Cavanaugh
 *
 * This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Main {
    /**
     * Creates an {@code ImageIcon} from any Icon. Useful for caching complex icon paints into a buffered Icon
     * 
     * @param icon Icon with complex painting operation
     * @return {@code ImageIcon} based on a {@code BufferedImage}
     */
    public static ImageIcon createImageIcon(final Icon icon) {

        BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = bufferedImage.createGraphics();

        // paint the icon into the BufferedImage
        // pass a JLabel; the Metal based icons want a GraphicsConfiguration from the passed component
        icon.paintIcon(new JLabel(), g, 0, 0);

        g.dispose();

        return new ImageIcon(bufferedImage);
    }
}

Related

  1. createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY)
  2. createImageIcon(Class theClass, String path, String description)
  3. createImageIcon(Class bezugsklasse, String path)
  4. createImageIcon(ClassLoader classloader, String path, String description)
  5. createImageIcon(File f, String description)
  6. createImageIcon(final String src)
  7. createImageIcon(java.net.URL imgURL)
  8. createImageIcon(String iconFile)
  9. createImageIcon(String imgFile)