Here is the Program for Capturing the Screen via using the Robot Class of java.awt package
import com.sun.awt.AWTUtilities;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
class InUI extends JFrame {
int wWin;
int hWin;
int xWin;
int yWin;
InUI() {
wWin = Toolkit.getDefaultToolkit().getScreenSize().width;
hWin = Toolkit.getDefaultToolkit().getScreenSize().height;
setSize(wWin, hWin);
Point loc = MouseInfo.getPointerInfo().getLocation();
setLocation(0, 0);
setBackground(Color.BLACK);
addKeyListener(new KeyDetector());
addMouseMotionListener(new MouseMove());
setUndecorated(true);
AWTUtilities.setWindowOpacity(this, 0.4f);
Container cont = getContentPane();
cont.setLayout(new BorderLayout());
JLabel lbl = new JLabel("Click and drag this capture area.
Press Enter to capture the screen and ESC for Exit.");
lbl.setHorizontalAlignment(JLabel.CENTER);
cont.add(lbl, BorderLayout.CENTER);
setVisible(true);
}
class KeyDetector extends KeyAdapter {
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
int code = e.getKeyCode();
if (code == 27) //exit the program when the Esc key is pressed.
System.exit(0);
else if (code == 45) //minimize the capture
minimize();
else if (code == 91) //decrease capture area
decreaseSize();
else if (code == 93) //increase capture area
increaseSize();
else if (code == 10) { //capture the screen when Enter key is pressed
minimize();
capture();
}
else if((code==KeyEvent.VK_X) && (e.getModifiers() & KeyEvent.ALT_MASK)!=0 )
System.exit(0);
}
}
class MouseMove extends MouseMotionAdapter {@Override
public void mouseDragged(MouseEvent e) {
//change location of the capture area when mouse is dragged
setWinLocation(e.getXOnScreen(), e.getYOnScreen());
}
}
public void increaseSize() {
Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
if (wWin < ds.getWidth()) {
wWin += 5;
this.setSize(wWin, hWin);
}
if (hWin < ds.getHeight()) {
hWin += 5;
this.setSize(wWin, hWin);
}
}
public void decreaseSize() {
if (wWin > 5) {
wWin -= 5;
this.setSize(wWin, hWin);
}
if (hWin > 5) {
hWin -= 5;
this.setSize(wWin, hWin);
}
}
public void setWinLocation(int x, int y) {
xWin = x - wWin / 2;
yWin = y - wWin / 2;
this.setLocation(new Point(xWin, yWin));
}
public void minimize() {
this.setExtendedState(JFrame.ICONIFIED);
}
public void capture() {
try {
Robot rb = new Robot();
//capture the target part of the screen
BufferedImage bi = rb.createScreenCapture(new Rectangle(xWin, yWin, wWin, hWin));
//save the image
System.out.println(System.getProperty("user.dir"));
ImageIO.write(bi, "png", new File(System.getProperty("user.dir") + File.separator + "image" + System.currentTimeMillis() + ".png"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class RobotDemo {
public static void main(String[] args) throws AWTException {
new InUI();
}
}