Here you can find the source of iconToImage(Icon icon)
Parameter | Description |
---|---|
icon | the icon to convert. |
public static Image iconToImage(Icon icon)
//package com.java2s; 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 { /**// www . j a va 2 s.co m * Converts an Icon to an Image. * * @param icon * the icon to convert. * @return image the converted icon. */ public static Image iconToImage(Icon icon) { if (icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); } 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; } }