Is there a way to save a JPanel's paintComponent contents into a .png file? Preferably with java.io or some other built-in library. Thanks.
Asked
Active
Viewed 6,960 times
1
-
Check out the [ImageIO class](http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html)'s methods, in particular the `write(...)` method. – Hovercraft Full Of Eels May 04 '13 at 02:33
1 Answers
6
- create a
java.awt.image.BufferedImage
- get a
java.awt.Graphics
byBufferedImage.createGraphics()
- Pass the
Graphics
toJPanel.paintComponent()
- Use
javax.imageio.ImageIO.write()
to save it

johnchen902
- 9,531
- 1
- 27
- 69
-
1+1 [Writing/saving an Image](http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html) for more details – MadProgrammer May 04 '13 at 02:57