Java BufferedImage from JPanel getBufferedImageFromPanel(JPanel panel)

Here you can find the source of getBufferedImageFromPanel(JPanel panel)

Description

get Buffered Image From Panel

License

Open Source License

Declaration

public static BufferedImage getBufferedImageFromPanel(JPanel panel) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class Main {
    public static BufferedImage getBufferedImageFromPanel(JPanel panel) {
        BufferedImage image = null;
        image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        panel.paint(g);//from   ww w. j a  v  a2 s  . c  o  m
        g.dispose();

        return image;
    }
}