-2

I'm new to programming. I'm trying to write a simple drawing program using java with processing. I need help with making it take a screenshot and save the image.

Here's what I have so far.

void setup(){
size(displayWidth, displayHeight);

background(255,255,255);

}



void keyPressed(KeyEvent SPACE){

background(255,255,255);
}

void draw(){

}


void mouseDragged()
{
strokeWeight(3);
stroke(0,0,0);
line(pmouseX, pmouseY, mouseX, mouseY);

}
  • 2
    What do you need help with? Look into the Robot class. I'm surprised your googling didn't already point you to it. – Kevin Workman Feb 11 '15 at 15:59
  • 2
    format your code. it's an important habit to get into and never get out of. That said, "screenshot of what"? Your own program? Your desktop? something else? – Mike 'Pomax' Kamermans Feb 11 '15 at 21:23
  • Possible duplicate of [Is there a way to take a screenshot using Java and save it to some sort of image?](https://stackoverflow.com/questions/58305/is-there-a-way-to-take-a-screenshot-using-java-and-save-it-to-some-sort-of-image) – Adam Michalik Jul 28 '17 at 06:59

1 Answers1

3

I have no idea what you are trying to do with the code you provided with your question although here is how to make a screenshot and save it using Java.

try
{
    Rectangle screenResolution = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    BufferedImage screenshot = new Robot().createScreenCapture(screenResolution);
    ImageIO.write(screenshot, "PNG", new File("yourimageoutput.png"));
}

catch(IOException | AWTException error)
{
    error.printStackTrace();
}

Here you can see the result: Screenshot made with java

If you want to change it to for example a 'jpeg' or 'bmp' image you have to change the second parameter of ImageIO.write() and the file extension.

Trevi Awater
  • 2,387
  • 2
  • 31
  • 53