Java Code Examples for java.awt.Robot#createScreenCapture()

The following examples show how to use java.awt.Robot#createScreenCapture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BDVRenderer.java    From 3Dscript with BSD 2-Clause "Simplified" License 8 votes vote down vote up
public ImagePlus takeSnapshot() {
	ViewerFrame win = viewer.getViewerFrame();
	win.toFront();
	IJ.wait(500);
	Rectangle bounds = win.getBounds();
	Rectangle r = bounds;
	ImagePlus imp = null;
	Image img = null;
	try {
		Robot robot = new Robot();
		img = robot.createScreenCapture(r);
	} catch(Exception e) { }
	if (img != null) {
		imp = new ImagePlus("", img);
	}
	return imp;
}
 
Example 2
Source File: JInternalFrameDraggingTest.java    From jdk8u_jdk with GNU General Public License v2.0 7 votes vote down vote up
public static void main(String[] args) throws Exception {

        Robot robot = new Robot();
        robot.setAutoDelay(20);
        SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);
        robot.waitForIdle();

        final int translate = FRAME_SIZE / 4;
        moveFrame(robot, translate, translate / 2, translate / 2);
        robot.waitForIdle();

        Point p = getDesktopPaneLocation();
        int size = translate / 2;
        Rectangle rect = new Rectangle(p.x, p.y, size, size);
        BufferedImage img = robot.createScreenCapture(rect);

        int testRGB = BACKGROUND_COLOR.getRGB();
        for (int i = 0; i < size; i++) {
            int rgbCW = img.getRGB(i, size / 2);
            int rgbCH = img.getRGB(size / 2, i);
            if (rgbCW != testRGB || rgbCH != testRGB) {
                throw new RuntimeException("Background color is wrong!");
            }
        }
    }
 
Example 3
Source File: ScreenShot.java    From cloudExplorer with GNU General Public License v3.0 6 votes vote down vote up
public void run() {
    try {
        File screenshot_file = new File(mainFrame.Home + File.separator + "screenshot.s3");
        Random rand = new Random(System.currentTimeMillis());
        int random = rand.nextInt(256);
        String random_name = "Screenshot-" + random;
        long t1 = System.currentTimeMillis();
        long diff = 0;
        while (diff < 5000) {
            long t2 = System.currentTimeMillis();
            diff = t2 - t1;
        }
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(screenshot_file.getAbsolutePath()));
        mainFrame.jTextArea1.append("\nCreated Screen Shot.");
        put = new Put(screenshot_file.getAbsolutePath(), mainFrame.cred.getAccess_key(), mainFrame.cred.getSecret_key(), mainFrame.cred.getBucket(), mainFrame.cred.getEndpoint(), random_name, false, false, false);
        put.startc(screenshot_file.getAbsolutePath(), mainFrame.cred.getAccess_key(), mainFrame.cred.getSecret_key(), mainFrame.cred.getBucket(), mainFrame.cred.getEndpoint(), random_name, false, false, false);
    } catch (Exception screenShot) {

    }
}
 
Example 4
Source File: NSTexturedJFrame.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void showFrame() throws Exception {
    final Robot robot = new Robot();
    robot.setAutoDelay(50);
    createUI();
    images[step] = robot.createScreenCapture(bounds);
    SwingUtilities.invokeAndWait(frame::dispose);
    sleep();
}
 
Example 5
Source File: NSTexturedJFrame.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void showFrame() throws Exception {
    final Robot robot = new Robot();
    robot.setAutoDelay(50);
    createUI();
    images[step] = robot.createScreenCapture(bounds);
    SwingUtilities.invokeAndWait(frame::dispose);
    sleep();
}
 
Example 6
Source File: TestsWithFiles.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
@Test(description = "Adding screenshot to the report")
public void testAddScreenshot1() throws IOException, AWTException {
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Rectangle screenRectangle = new Rectangle(screenSize);
	Robot robot = new Robot();
	BufferedImage image = robot.createScreenCapture(screenRectangle);
	File imgFile = File.createTempFile("screenshot_file", "png");
	ImageIO.write(image, "png", imgFile);
	report.addImage(imgFile, "My screenshot file");
	imgFile.delete();

}
 
Example 7
Source File: ScreenFram.java    From myqq with MIT License 5 votes vote down vote up
public void snapshot()
{
	try
	{
		Robot robot = new Robot();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		image = robot.createScreenCapture(new Rectangle(0, 0, d.width,
				d.height));
	}
	catch (AWTException e)
	{

	}
}
 
Example 8
Source File: Screenshot.java    From sAINT with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void TakeScreenshot(String filePath, String fileName) {
    try {
        Robot robot = new Robot();
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
        ImageIO.write(screenFullImage, "jpg", new File(filePath + fileName + ".jpg"));
    } catch (AWTException | IOException ex) {
        System.out.println(ex.getMessage());
    }
}
 
Example 9
Source File: CaptureController.java    From logbook-kai with MIT License 5 votes vote down vote up
/**
 * 座標取得アクション
 */
private void detectAction() {
    try {
        GraphicsConfiguration gc = this.currentGraphicsConfiguration();
        Robot robot = new Robot(gc.getDevice());
        BufferedImage image = robot.createScreenCapture(gc.getBounds());
        Rectangle relative = ScreenCapture.detectGameScreen(image);
        Rectangle screenBounds = gc.getBounds();
        this.setBounds(robot, relative, screenBounds);
    } catch (Exception e) {
        LoggerHolder.get().error("座標取得に失敗しました", e);
    }
}
 
Example 10
Source File: TestsWithFiles.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
@Test(description = "Adding screenshot to the report")
public void testAddScreenshot0() throws IOException, AWTException {
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Rectangle screenRectangle = new Rectangle(screenSize);
	Robot robot = new Robot();
	BufferedImage image = robot.createScreenCapture(screenRectangle);
	File imgFile = File.createTempFile("screenshot_file", "png");
	ImageIO.write(image, "png", imgFile);
	report.addImage(imgFile, "My screenshot file");
	imgFile.delete();

}
 
Example 11
Source File: DrawImageBilinear.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: DrawImageBilinear.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: TestsWithFiles.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
@Test
@TestProperties(name = "Adding screenshot to the report")
public void testAddScreenshot() throws Exception {
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Rectangle screenRectangle = new Rectangle(screenSize);
	Robot robot = new Robot();
	BufferedImage image = robot.createScreenCapture(screenRectangle);
	File imgFile = File.createTempFile("screenshot_file", "png");
	ImageIO.write(image, "png", imgFile);
	ReporterHelper.copyFileToReporterAndAddLink(report, imgFile, "My screenshot file");
	imgFile.delete();

}
 
Example 14
Source File: DrawImageBilinear.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: NSTexturedJFrame.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void showFrame() throws Exception {
    final Robot robot = new Robot();
    robot.setAutoDelay(50);
    createUI();
    images[step] = robot.createScreenCapture(bounds);
    SwingUtilities.invokeAndWait(frame::dispose);
    sleep();
}
 
Example 16
Source File: DrawImageBilinear.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 17
Source File: MultiScreenLocationTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws AWTException
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gds = ge.getScreenDevices();
    if (gds.length < 2) {
        System.out.println("It's a multiscreen test... skipping!");
        return;
    }

    for (int i = 0; i < gds.length; ++i) {
        GraphicsDevice gd = gds[i];
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screen = gc.getBounds();
        Robot robot = new Robot(gd);

        // check Robot.mouseMove()
        robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
        Point mouse = MouseInfo.getPointerInfo().getLocation();
        Point point = screen.getLocation();
        point.translate(mouseOffset.x, mouseOffset.y);
        if (!point.equals(mouse)) {
            throw new RuntimeException(getErrorText("Robot.mouseMove", i));
        }

        // check Robot.getPixelColor()
        Frame frame = new Frame(gc);
        frame.setUndecorated(true);
        frame.setSize(100, 100);
        frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
        frame.setBackground(color);
        frame.setVisible(true);
        robot.waitForIdle();
        Rectangle bounds = frame.getBounds();
        if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
            throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
        }

        // check Robot.createScreenCapture()
        BufferedImage image = robot.createScreenCapture(bounds);
        int rgb = color.getRGB();
        if (image.getRGB(0, 0) != rgb
            || image.getRGB(image.getWidth() - 1, 0) != rgb
            || image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
            || image.getRGB(0, image.getHeight() - 1) != rgb) {
                throw new RuntimeException(
                        getErrorText("Robot.createScreenCapture", i));
        }
        frame.dispose();
    }

    System.out.println("Test PASSED!");
}
 
Example 18
Source File: MultiScreenLocationTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws AWTException
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gds = ge.getScreenDevices();
    if (gds.length < 2) {
        System.out.println("It's a multiscreen test... skipping!");
        return;
    }

    for (int i = 0; i < gds.length; ++i) {
        GraphicsDevice gd = gds[i];
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screen = gc.getBounds();
        Robot robot = new Robot(gd);

        // check Robot.mouseMove()
        robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
        Point mouse = MouseInfo.getPointerInfo().getLocation();
        Point point = screen.getLocation();
        point.translate(mouseOffset.x, mouseOffset.y);
        if (!point.equals(mouse)) {
            throw new RuntimeException(getErrorText("Robot.mouseMove", i));
        }

        // check Robot.getPixelColor()
        Frame frame = new Frame(gc);
        frame.setUndecorated(true);
        frame.setSize(100, 100);
        frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
        frame.setBackground(color);
        frame.setVisible(true);
        robot.waitForIdle();
        Rectangle bounds = frame.getBounds();
        if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
            throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
        }

        // check Robot.createScreenCapture()
        BufferedImage image = robot.createScreenCapture(bounds);
        int rgb = color.getRGB();
        if (image.getRGB(0, 0) != rgb
            || image.getRGB(image.getWidth() - 1, 0) != rgb
            || image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
            || image.getRGB(0, image.getHeight() - 1) != rgb) {
                throw new RuntimeException(
                        getErrorText("Robot.createScreenCapture", i));
        }
        frame.dispose();
    }

    System.out.println("Test PASSED!");
}
 
Example 19
Source File: MultiScreenLocationTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws AWTException
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gds = ge.getScreenDevices();
    if (gds.length < 2) {
        System.out.println("It's a multiscreen test... skipping!");
        return;
    }

    for (int i = 0; i < gds.length; ++i) {
        GraphicsDevice gd = gds[i];
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screen = gc.getBounds();
        Robot robot = new Robot(gd);

        // check Robot.mouseMove()
        robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
        Point mouse = MouseInfo.getPointerInfo().getLocation();
        Point point = screen.getLocation();
        point.translate(mouseOffset.x, mouseOffset.y);
        if (!point.equals(mouse)) {
            throw new RuntimeException(getErrorText("Robot.mouseMove", i));
        }

        // check Robot.getPixelColor()
        Frame frame = new Frame(gc);
        frame.setUndecorated(true);
        frame.setSize(100, 100);
        frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
        frame.setBackground(color);
        frame.setVisible(true);
        robot.waitForIdle();
        Rectangle bounds = frame.getBounds();
        if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
            throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
        }

        // check Robot.createScreenCapture()
        BufferedImage image = robot.createScreenCapture(bounds);
        int rgb = color.getRGB();
        if (image.getRGB(0, 0) != rgb
            || image.getRGB(image.getWidth() - 1, 0) != rgb
            || image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
            || image.getRGB(0, image.getHeight() - 1) != rgb) {
                throw new RuntimeException(
                        getErrorText("Robot.createScreenCapture", i));
        }
        frame.dispose();
    }

    System.out.println("Test PASSED!");
}
 
Example 20
Source File: AwtUtils.java    From logbook with MIT License 3 votes vote down vote up
/**
 * <p>
 * スクリーンショットをrectangleで指定した領域で取得します<br>
 * 失敗した場合nullを返します
 * </p>
 * 
 * @param rectangle
 * @return スクリーンショット
 */
public static BufferedImage getCapture(Rectangle rectangle) {
    try {
        Robot robot = new Robot();
        return robot.createScreenCapture(rectangle);
    } catch (Exception e) {
        return null;
    }
}