Here you can find the source of getPanelImage(JPanel p)
public static Image getPanelImage(JPanel p)
//package com.java2s; //License from project: Open Source License import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; public class Main { /**//from w w w .ja v a 2 s. c om * I think it's more expensive to layout 1000 charts than it is to layout 1000 * already rendered (fixed) images. */ public static Image getPanelImage(JPanel p) { JFrame frame; frame = new JFrame(); frame.setContentPane(p); frame.pack(); Dimension size = p.getPreferredSize(); BufferedImage image = new BufferedImage((int) size.width, (int) size.height, BufferedImage.TYPE_INT_RGB); p.paint(image.createGraphics()); return (image); } }