Advertisement
Chiddix

Capture

Aug 11th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package org.rabrg.util;
  2.  
  3. import java.awt.AWTException;
  4. import java.awt.Rectangle;
  5. import java.awt.Robot;
  6. import java.awt.Toolkit;
  7. import java.awt.image.BufferedImage;
  8.  
  9. /**
  10.  * A utility class which uses {@link Robot} to capture the user's screen image into a {@link BufferedImage}.
  11.  * @author Ryan Greene
  12.  *
  13.  */
  14. public final class Capture {
  15.  
  16.     /**
  17.      * The {@link Robot} instance.
  18.      */
  19.     private static Robot robot;
  20.  
  21.     /**
  22.      * The screen's size represented as a {@link Rectangle}.
  23.      */
  24.     private static Rectangle screen;
  25.  
  26.     /**
  27.      * Initiates the capture utility.
  28.      * @throws AWTException If an exception is thrown.
  29.      */
  30.     public static void initiate() throws AWTException {
  31.         robot = new Robot();
  32.         screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  33.     }
  34.  
  35.     /**
  36.      * Sets the delay between {@link Robot} events being registered and being executed.
  37.      * @param delay The delay.
  38.      */
  39.     public static void setDelay(final int delay) {
  40.         robot.setAutoDelay(delay);
  41.     }
  42.  
  43.     /**
  44.      * Captures the current screen's image, excluding the mouse cursor.
  45.      * @return The captured screen image.
  46.      */
  47.     public static BufferedImage capture() {
  48.         return robot.createScreenCapture(screen);
  49.     }
  50.  
  51.     /**
  52.      * Captures the current screen's image, excluding the mouse cursor.
  53.      * @param screen The dimensions of the screen being captured.
  54.      * @return The captured screen image.
  55.      */
  56.     public static BufferedImage capture(Rectangle screen) {
  57.         return robot.createScreenCapture(screen);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement