Java Icon to Image iconToImage(Icon icon)

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

Description

icon To Image

License

Open Source License

Declaration

public static Image iconToImage(Icon icon) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

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 iconToImage(Icon icon) {
        if (icon instanceof ImageIcon) {
            return ((ImageIcon) icon).getImage();
        } else {//from  w  ww  . ja v  a 2 s .  co  m
            int w = icon.getIconWidth();
            int h = icon.getIconHeight();
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage image = gc.createCompatibleImage(w, h);
            Graphics2D g = image.createGraphics();
            icon.paintIcon(null, g, 0, 0);
            g.dispose();
            return image;
        }
    }
}

Related

  1. iconToImage(Icon icon)
  2. iconToImage(Icon icon)
  3. iconToImage(Icon icon)
  4. iconToImage(Icon icon)
  5. iconToImage(Icon icon)
  6. iconToImage(Icon icon, Component comp)
  7. iconToImage(Icon icon, int width, int height)