Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_PRINTSCREEN);
        robot.delay(40);
        robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
        robot.delay(404);

        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        DataFlavor[] flavors = cb.getAvailableDataFlavors();
        for (DataFlavor flavor : flavors) {
            if (flavor.toString().indexOf("java.awt.Image") <= 0) {
                continue;
            }
            Image i = (Image) cb.getData(flavor);
            BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bi.createGraphics();
            g.drawImage(i, 0, 0, null);
            g.dispose();
            ImageIO.write(bi, "png", new File("c:/Java_Dev/test.png"));
        }
    }
}