Tuesday, October 19, 2021

Capture Screenshot in Java

Sometimes you just need to capture a screenshot in Java. Well look no further here's the code in question!

import java.awt.AWTException;import java.awt.HeadlessException;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class ScreenShot {  public static void main(String[] args) throws IOException {    BufferedImage image;    try {      image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));      ImageIO.write(image, "png", new File("screenshot.png"));    } catch (HeadlessException e) {        e.printStackTrace();    } catch (AWTException e) {        e.printStackTrace();    }		  }}

No comments:

Post a Comment

No Sleep

 This is the second day I've woken up early. Like way earlier than normal. Like four am early. I don't know what to do about it. I n...