Java tutorial
//package com.java2s; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; public class Main { /** * * @param image * @return */ public static BufferedImage toBufferedImage(ImageIcon image) { return toBufferedImage(image.getImage()); } /** * Converts a {@link Image} into a {@link BufferedImage} using * {@link BufferedImage#TYPE_INT_ARGB_PRE} type. If the image is already a * {@link BufferedImage} then the same instance is returned. * * @param image the image to convert to a buffered image. * @return the converted image or the same instance if already a * {@link BufferedImage}. */ public static BufferedImage toBufferedImage(Image imageIn) { return toBufferedImage(imageIn, BufferedImage.TYPE_INT_ARGB_PRE); } /** * Converts a {@link Image} into a {@link BufferedImage}. If the image * is already a {@link BufferedImage} then the same instance is returned. * * @param image the image to convert to a buffered image. * @param imageType the image type to use. * @return the converted image or the same instance if already a * {@link BufferedImage}. */ public static BufferedImage toBufferedImage(Image image, int imageType) { if (image instanceof BufferedImage) { return (BufferedImage) image; } final BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType); final Graphics2D gfx = buffImage.createGraphics(); gfx.drawImage(image, 0, 0, null); return buffImage; } }