簡體   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