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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » RobotScreenshots 
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
 */
public class RobotTest {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void 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
   */
  public static void 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(20050);
    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(00400300));

    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);
  }

  public static final int DEFAULT_WIDTH = 450;

  public static final int 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.
   */
  private class ColorAction implements ActionListener {
    public ColorAction(Color c) {
      backgroundColor = c;
    }

    public void 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);
  }

  public static final int DEFAULT_WIDTH = 300;

  public static final int 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.