繁体   English   中英

如何使用机器人截取 JPanel 的屏幕截图?

[英]How to take a screenshot of a JPanel with robot?

这就是我如何制作整个 JFrame 的屏幕截图

Rectangle screenRect = cap.getBounds();
File docfile = new File("image");
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", file);

cap是 JFrame。 但是,当我将 JPanel 用于cap ,我会得到我桌面的部分屏幕截图,不仅是 JPanel,而且是我真正想要的。

它可能有帮助:

/**
 * 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM