As seen in this answer, You can create a BufferedImage
of the JPanel
contents. The example has PanelOne
(which is a JPanel
) being passed to a method of another JPanel
(PanelTwo
), and this is the method in PanelTwo
that creates a BufferedImage
from the PanelOne
passed to it.
BufferedImage bi;
....
private void setImage(PanelOne panel) {
Dimension d = panel.getPreferredSize();
int w = (int)d.getWidth();
int h =(int)d.getHeight();
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
panel.paint(g);
g.dispose();
}
....
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, 250, 250, 0, 0, 250, 250, this);
}
Here's the result. PanelOne
is the left and passes itself to PanelTwo
on the right