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

static public Image iconToImage(Icon icon) 

Method Source Code


//package com.java2s;
// This copy of Ice is licensed to you under the terms described in the

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;

import java.awt.GraphicsEnvironment;

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

public class Main {
    static public Image iconToImage(Icon icon) {
        if (icon instanceof ImageIcon) {
            return ((ImageIcon) icon).getImage();
        } else {//w w  w.  j  a v a2 s.  c o m
            Graphics2D g = null;
            try {
                BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                        .getDefaultConfiguration().createCompatibleImage(icon.getIconWidth(), icon.getIconHeight());
                g = image.createGraphics();
                icon.paintIcon(null, g, 0, 0);
                return image;
            } finally {
                if (g != null) {
                    g.dispose();
                }
            }
        }
    }
}

Related

  1. iconImage(Icon icon)
  2. iconToImage(@Nonnull Component context, @Nullable final Icon icon)
  3. iconToImage(Icon icon)
  4. iconToImage(Icon icon)
  5. iconToImage(Icon icon)
  6. iconToImage(Icon icon)
  7. iconToImage(Icon icon)