1

How can I capture a specific component in my Java application as opposed to my full primary monitor?

Current code example of how I collect the image:

    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    BufferedImage image = null;
    try {//take the screenshot and store in buf img
        image = new Robot().createScreenCapture(screenRect);
    } catch (AWTException e2) {
        e2.printStackTrace();
    }

Example of components I want to screen shot would be JFrame, Applet, and JPanels. An example with any of those would be satisfactory in answering this question.

EDIT: This code solves the issue of allowing you to run and take screenshots while minimized or in background. Thanks to Dando18.

public static BufferedImage createImage(JComponent comp) {
    int w = comp.getWidth();
    int h = comp.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    comp.paint(g);
    return bi;
}

Remaining issues:

  • When I take a screenshot by using getComponent or getContentPane on my JFrame and pass it over to the createImage function it shows every control/component and their content is shown on the Main JFrame but the applet will not appear even though it is showing me my game it is sending back black screen shots of that portion.

How it is loaded in my application:

The following conditions are for my applet.

  • Applet is on Main JFrame
  • Stub of Applet implements AppletContext, AppletStub

Main JFrame -> Calls class named Loader and passes parameters to it -> Parameters are passed to my class called Stub from Loader -> Applet is set to the Stub -> Applet is added to Main JFrame centered.

I don't understand why this Component cannot be captured along with the others on the Main JFrame. I know the Applet is a Component of awt not JComponent of swing and I have tried just a screenshot of Applet by returning it from the Loader class and passing just Component to createImage overriding the signature, it seemed to only capture a small window which was different than capturing the Main JFrame of my application but nonetheless it was a black image.

7
  • Do you have to use Robot? Commented Jul 4, 2016 at 18:07
  • IIRC JFrame has a getRectangle() method, in which case I could imagine using that as opposed to screenRect would do Commented Jul 4, 2016 at 18:08
  • Robot is not required at all. Suggestions are open and welcome. Commented Jul 4, 2016 at 18:10
  • 1
    1) See ComponentImageCapture in this answer. 2) Tip: Add @Dando18 (or whoever, the @ is important) to notify the person of a new comment. Commented Jul 5, 2016 at 0:17
  • Thanks @AndrewThompson I will test now. Commented Jul 5, 2016 at 0:28

1 Answer 1

3

If you don't have to use Robot this should return a BufferedImage of a component.

public static BufferedImage createImage(JComponent comp) {
    int w = comp.getWidth();
    int h = comp.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    comp.paint(g);
    return bi;
}

And simply call:

BufferedImage component_screenshot = createImage(myComponent);

Or using Robot:

try {
    BufferedImage img = new Robot().createScreenCapture(new Rectangle(
                                      frame.getLocationOnScreen().x,
                                      frame.getLocationOnScreen().y,
                                      frame.getWidth(),
                                      frame.getHeight()));
} catch (AWTException ex) {
}
Sign up to request clarification or add additional context in comments.

4 Comments

This approach doesn't work if the component's area's rendering depends on other component that is not its child, e.g. the component is transparent.
@HeseinBurg the second approach won't work if the window is closed. You can try the first approach understanding what Coderino said.
The first part i am getting just my main application now but unfortunately does not render my Java Applet in the screen shots as Coderino suggested. I add my Applet to my main Frame (the one i am screenshotting) is there anyway to fix this?
"..is there anyway to fix this?" For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example of your attempt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.