Java Image Create createImage(Component comp, Icon icon)

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

Description

create Image

License

Open Source License

Declaration

public static BufferedImage createImage(Component comp, Icon icon) 

Method Source Code

//package com.java2s;
/*/*w w  w . j a v a2 s  .c  o m*/
 * 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.BufferedImage;

import javax.swing.Icon;

public class Main {
    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. createImage(byte[] data)
  2. createImage(Icon icon)
  3. createImage(JPanel panel)
  4. createImage(String str)
  5. createImage(URL url, String description, boolean checkPresence)