How can I convert an Icon to an Image
问题 I'm trying to convert an Icon ( javax.swing.Icon ) to an Image ( java.awt.Image ) using this code: private Image iconToImage(Icon icon) { if(icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); } else { BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); icon.paintIcon(null, image.getGraphics(), 0, 0); return image; } } The thing is, the paintIcon function throws a NullPointerException on the image.getGraphics() . For