4

Possible Duplicate:
Java Swing : Obtain Image of JFrame

How do you save what a Java GUI Component displays to an image file?

E.g. rendering a JPanel as a PNG.

Community
  • 1
  • 1
Matthew Piziak
  • 3,262
  • 4
  • 33
  • 47

2 Answers2

5
JPanel panel = ...
...
...
File yourFileHere = ...
...
...
BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
panel.paint(g);
g.dispose();

try{
    ImageIO.write(img, "png", yourFileHere);
}catch(IOException e){
    e.printStackTrace();
}
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
Jeffrey
  • 43,506
  • 7
  • 88
  • 140
1

that would be descibed and with excelent examples here by @Andrew Thompson, but you can learn more than that by reading 2D Graphics and examples for that here

Community
  • 1
  • 1
mKorbel
  • 109,107
  • 18
  • 130
  • 305