This is how I can make a screenshot of a whole JFrame
Rectangle screenRect = cap.getBounds();
File docfile = new File("image");
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", file);
with cap
being a JFrame. But when I use a JPanel for cap
, I get a partly screenshot of my desktop, not only of the JPanel, what I actually want.
It may help:
/**
* Perform a screen capture of a JPanel to a Buffered Image
*
* @param panel - Panel to screen copy
* @return BufferedImage
*
* @throws AWTException - if the platform configuration does not allow low-level
* input control. This exception is always thrown when
* GraphicsEnvironment.isHeadless() returns true
*/
public static BufferedImage printScreen(JPanel panel) throws AWTException {
Point p = panel.getLocationOnScreen();
Dimension dim = panel.getSize();
Rectangle rect = new Rectangle(p, dim);
Robot robot = new Robot();
return robot.createScreenCapture(rect);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.