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,430
  • 4
  • 35
  • 49

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
  • 168,117
  • 40
  • 217
  • 433
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
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,525
  • 20
  • 134
  • 319