Using Robot to capture a screen shapshot : Robot « Development Class « Java






Using Robot to capture a screen shapshot

/* This program is a part of the companion code for Core Java 8th ed. (http://horstmann.com/corejava) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */import java.awt.AWTException; import java.awt.Color; import java.awt.EventQueue; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * @version 1.03 2007-06-12 * @author Cay Horstmann */publicclass RobotTest { publicstaticvoid main(String[] args) { EventQueue.invokeLater(new Runnable() { publicvoid run() { // make frame with a button panel  ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // attach a robot to the screen device  GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice screen = environment.getDefaultScreenDevice(); try { Robot robot = new Robot(screen); runTest(robot); } catch (AWTException e) { e.printStackTrace(); } } }); } /** * Runs a sample test procedure * * @param robot * the robot attached to the screen device */publicstaticvoid runTest(Robot robot) { // simulate a space bar press  robot.keyPress(' '); robot.keyRelease(' '); // simulate a tab key followed by a space  robot.delay(2000); robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyPress(' '); robot.keyRelease(' '); // simulate a mouse click over the rightmost button  robot.delay(2000); robot.mouseMove(200, 50); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); // capture the screen and show the resulting image  robot.delay(2000); BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300)); ImageFrame frame = new ImageFrame(image); frame.setVisible(true); } } /** * A frame to display a captured image */class ImageFrame extends JFrame { /** * @param image * the image to display */public ImageFrame(Image image) { setTitle("Capture"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); JLabel label = new JLabel(new ImageIcon(image)); add(label); } publicstaticfinalint DEFAULT_WIDTH = 450; publicstaticfinalint DEFAULT_HEIGHT = 350; } /* * This program is a part of the companion code for Core Java 8th ed. * (http://horstmann.com/corejava) * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. *//** * A panel with three buttons * * @version 1.32 2004-05-11 * @author Cay Horstmann */class ButtonPanel extends JPanel { public ButtonPanel() { // create buttons  JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); // add buttons to panel  add(yellowButton); add(blueButton); add(redButton); // create button actions  ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons  yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } /** * An action listener that sets the panel's background color. */privateclass ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } publicvoid actionPerformed(ActionEvent event) { setBackground(backgroundColor); } private Color backgroundColor; } } /* * This program is a part of the companion code for Core Java 8th ed. * (http://horstmann.com/corejava) * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. *//** * A frame with a button panel * * @version 1.31 2004-05-11 * @author Cay Horstmann */class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame  ButtonPanel panel = new ButtonPanel(); add(panel); } publicstaticfinalint DEFAULT_WIDTH = 300; publicstaticfinalint DEFAULT_HEIGHT = 200; } 








Related examples in the same category

1.Moving the Cursor on the Screen
2.Simulate a key press
3.Simulate a mouse click
4.Create mouse event using Robot class
5.Capture a screenshot
6.Get the colour of a screen pixel
7.Capturing a Screen Shot
8.Capturing Screen in an image using Robot class
9.Create key press event using Robot class?
10.Use Robot to do mouse press
11.Use Robot to send combo key event
close