Search code examples
javaswingvideo-capture

How to capture the contents of graphics as an image


I have a transparent jframe that has scrolling text on it, and I need to capture just the jframe and not whats behind it. At first, java.awt.Robot looked promising, but it doesn't capture only the graphics context of the jframe. Maybe somebody knows a portion of the API that could help with this...


Solution

  • You can

    1. Create a BufferedImage with the JFrame's size
    2. Call the JFrame's print() method with the BufferedImage's graphics

    Code:

    BufferedImage image = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB);
    frame.print(image.getGraphics());