Convert JPanel into png or other image file
I have a JPanel that consists of a lot of JLabels. So there is no paint at all, just JLabels with different background colors and such. The JPanel is visible.
How can I convert this JPanel to an image file? The image file would be a picture of the JPanel e开发者_Go百科xactly as it appears on the screen.
(Ideally, the program would create a .png, and save it to the same folder that the program is in or even a different folder chosen by the user.)
Any advice would be appreciated. Thanks!
---- It's Cherie again, unregistered so I couldn't get back into my account to choose stas's answer as the answer. This is why I'm not replying... anyway, thank you for your help. Cherie
Screen Image will choose the best way to create the image.
BufferedImage bi = ScreenImage.createImage(panel);
ScreenImage.writeImage(bi, "panel.png");
like this:
BufferedImage image = new Robot().createScreenCapture(new Rectangle(panel.getLocationOnScreen().x, panel.getLocationOnScreen().y, panel.getWidth(), panel.getHeight()));
ImageIO.write(image, "png", file);
You create a file like this:
File file = new File("fileName.png");
if (!file.exists())file.createNewFile();
please select my answer as correct if i answered your question :)
The java.awt.Robot classes can take a screen dump which you can then put where you need it for later.
精彩评论