2

I have a small issue that I don’t really know how to solve, basically I want to take a screenshot (using the "Robot" class) of my desktop excluding the actual program GUI components. Originally I believed it can be done by temporary hiding the components but every time a new screenshot is made the components are included in the image.

This is the block included in the actionPerformed method for the button that takes the screenshot:

if (command.equals("zoom")) {
    setComponentVisability(false);//try to hide the components from the robot
    zt.screenShoot();//take the screenshot
    setComponentVisability(true);//show the components
} 

"zt.screenShoot" takes the screenshot and returns it in a new JFrame (for debugging),in my main frame I am using the

com.sun.awt.AWTUtilities.setWindowOpaque(systemWindow, false);

method to make the background transparent; not sure if that has anything to do with this issue.

any help would be great, thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

5

Use a short delay between hiding the component and taking the screenshot.

E.G. TestScreenshot.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class TestScreenshot {

    static JLabel screenshot;

    public static void main(String[] args) throws Exception {
        final Robot robot = new Robot();
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                final JFrame f = new JFrame("Screenshot");

                JPanel gui = new JPanel(new BorderLayout(3,3));

                gui.setBorder(new EmptyBorder(5,5,5,5));

                f.setContentPane(gui);

                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                screenshot = new JLabel();
                JScrollPane scroll = new JScrollPane(screenshot);
                scroll.setPreferredSize(new Dimension(800,600));
                gui.add(scroll, BorderLayout.CENTER);

                final ActionListener al = new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        Rectangle screenSize = new Rectangle(
                            Toolkit.getDefaultToolkit().getScreenSize());
                        Image image = robot.createScreenCapture(screenSize);
                        setImage(image);
                        f.setVisible(true);
                    }
                };

                JButton grabScreen = new JButton("Grab Screen");
                grabScreen.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        f.setVisible(false);
                        Timer timer = new Timer(400, al);
                        timer.setRepeats(false);
                        timer.start();
                    }
                } );
                gui.add(grabScreen, BorderLayout.SOUTH);

                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }

    public static void setImage(Image image) {
        screenshot.setIcon(new ImageIcon(image));
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • If this is executed on the EDT, then a `SwingUtils.invokeLater()` will be necessary as well. – Joachim Sauer May 05 '11 at 14:06
  • i had some problems with the modification of the code but managed to sort it out, i need to get some rep to give u a "tick", thanks again – Michael Ole May 05 '11 at 16:56
  • This isn't very good because the user can see a flicker between the transition. If you fix that then it's solid. –  Jan 16 '17 at 00:56