Java Icon toImage(Icon icon)

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

Description

to Image

License

Open Source License

Declaration


public static Image toImage(Icon icon) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;

import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {

    public static Image toImage(Icon icon) {
        if (icon instanceof ImageIcon)
            return ((ImageIcon) icon).getImage();

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage image = gc.createCompatibleImage(icon.getIconWidth(), icon.getIconHeight());

        Graphics2D g = image.createGraphics();
        icon.paintIcon(null, g, 0, 0);//from w w w.j  ava2s  . com
        g.dispose();

        return image;
    }
}

Related

  1. extractIconImage(Component component, Icon icon)
  2. genImageResource(Class cls, String icon)
  3. getChatIconImage()
  4. makeIcon(final Class baseClass, final Class rootClass, final String imageFile)
  5. scaleIcon(Image iconImage, int size)
  6. toImages(List icons)