Convert JPanel to png or other image file

I have a JPanel that consists of many JLabels. So there is no paint at all, just JLabels with different background colors, etc. JPanel is visible.

How to convert this JPanel to image file? The image file will be a JPanel image just like on the screen.

(Ideally, the program will create .png and save it in the same folder in which the program is located, or even in another folder selected by the user.)

Any advice would be appreciated. Thank you

---- This is Cherie again, unregistered, so I could not return to my account to select stas as the answer. That is why I do not answer ... anyway, thanks for your help. Cherie

+3
source share
3 answers

.

BufferedImage bi = ScreenImage.createImage(panel);
ScreenImage.writeImage(bi, "panel.png");
+4

:

BufferedImage image = new Robot().createScreenCapture(new Rectangle(panel.getLocationOnScreen().x, panel.getLocationOnScreen().y, panel.getWidth(), panel.getHeight()));
ImageIO.write(image, "png", file);

:

File file = new File("fileName.png");
if (!file.exists())file.createNewFile();

, , :)

+3

In java.awt.Robot classes, you can take a screen dump, which can then be placed where you need it.

+2
source

All Articles