0

The code simply captures your working window as jpeg image and stores it in the same directory as where your java class after compiling by javac is being stored

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
 
import javax.imageio.ImageIO;
 
public class screen2image
{
	public static void main(String[] args) throws Exception
	{
		Robot robot = new Robot();
 
		BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
	}
}
13
Contributors
12
Replies
106
Views
9 Years
Discussion Span
Last Post by Ashan_1
0
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class screen2image
{
    public static void main(String[] args) throws Exception
    {
        Robot robot = new Robot();

        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    }
}

Edited by mike_2000_17: Fixed formatting

0
import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File; import javax.imageio.ImageIO; public class screen2image{   public static void main(String[] args) throws Exception {       Robot robot = new Robot();      BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));       ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));   }}import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class screen2image
{
    public static void main(String[] args) throws Exception
    {
        Robot robot = new Robot();

        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    }
}

Edited by mike_2000_17: Fixed formatting

0

hi all, is there any way of getting mouse location on screen even it is out of frame? normally e.getLocationOnScreen() method works but out of frame.
can i use Rectangle to do that?

0

how does this thing suppose to work? cos i have netbeans as development environment on my vista os,i have inputed the codes and it indicted its with no error on my template,i even run it and it run fine, but how it works is what i don't understand so how do i know how it captures my screen and where does it store it?

0

Capturing a screenshot may be achieved in 2 ways -
1. using "robot.createScreenCapture()", as mentioned above.
2. using "robot.keyPress()" to simulate hitting PrtScr key (for most systems).
method 2 may be something like this:

    r.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);

you might want to combine these methods, testing the success of one, and trying the other if the 1st one failed.

Capturing part of the screen:
1.
using 1st method: "robot.createScreenCapture(bounds)", where bounds might be a window's rectangle, for example:

    Rectangle bounds = frame.getBounds()

2.
using the 2nd method to hit ALT+PrtScr, to capture only the currently focused window:

    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_ALT);

Edited by Obfuscator: minor indentation change

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.