Java Screen Shot Code

From Simson Garfinkel
Revision as of 10:33, 17 July 2009 by Simson (talk | contribs) (Created page with 'Make a Screen Shot with Java <pre> import java.awt.image.*; import java.awt.*; import javax.swing.*; import javax.imageio.*; import java.io.*; public class ScreenShot { pub…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Make a Screen Shot with Java

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;

public class ScreenShot
{
    public static void main(String[] args)
    {
	try
	    {
		// Get the screen size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(rectangle);
		File file;

		// Save the screenshot as a png
		file = new File("screen.png");
		ImageIO.write(image, "png", file);

	    } catch (Exception e) {
		System.out.println(e.getMessage());
	    }
    }
}