Here you can find the source of getImageFromSource(JPanel source, Dimension dim)
static BufferedImage getImageFromSource(JPanel source, Dimension dim)
//package com.java2s; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.image.BufferedImage; import javax.swing.JPanel; public class Main { static BufferedImage getImageFromSource(JPanel source, Dimension dim) { int width = dim.width; int height = dim.height; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = image.createGraphics(); Rectangle originalBounds = source.getBounds(); source.setBounds(0, 0, width, height); source.updateUI();/*from w w w.ja v a2 s.c om*/ source.paint(g); source.setBounds(originalBounds); g.dispose(); return image; } }