Java Icon to BufferedImage getBufferedImage(Component comp, Icon icon)

Here you can find the source of getBufferedImage(Component comp, Icon icon)

Description

get Buffered Image

License

Open Source License

Declaration

public static BufferedImage getBufferedImage(Component comp, Icon icon) 

Method Source Code

//package com.java2s;
/*/*from   www . jav a 2s .c om*/
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.awt.Component;
import java.awt.Composite;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

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

public class Main {
    public static BufferedImage getBufferedImage(Component comp, Icon icon) {
        if (icon instanceof ImageIcon) {
            Image img = ((ImageIcon) icon).getImage();

            if (img instanceof BufferedImage) {
                return (BufferedImage) img;
            }
        }

        return createImage(comp, icon);
    }

    public static BufferedImage createImage(Component comp) {
        Dimension size = comp.getSize();

        if ((size.width < 1) || (size.height < 1)) {
            size.width = 1;
            size.height = 1;
        }

        BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TRANSLUCENT);
        Graphics2D g2 = image.createGraphics();

        try {
            comp.paint(g2);
        } finally {
            g2.dispose();
        }

        return image;
    }

    public static BufferedImage createImage(Component comp, Icon icon) {
        return createImage(comp, icon, null);
    }

    public static BufferedImage createImage(Component comp, Icon icon, Composite composite) {
        if (icon == null) {
            return null;
        }

        BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TRANSLUCENT);
        Graphics2D g2 = image.createGraphics();

        if (composite != null) {
            g2.setComposite(composite);
        }

        try {
            icon.paintIcon(comp, g2, 0, 0);
        } finally {
            g2.dispose();
        }

        return image;
    }
}

Related

  1. getBufferedImage(Icon icon)
  2. getBufferedImageFromIcon(Icon icon)
  3. iconToBufferedImage(Icon icon)
  4. toBufferedImage(Icon icon)