package com.haxademic.core.draw.image;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;

import com.haxademic.core.system.SystemUtil;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;

public class ScreenUtil {
	
	public static String saveScreenshot( PApplet p, String outputDir ) {
		String filename = outputDir + SystemUtil.getTimestampFine( p ) + ".png";
		p.saveFrame( filename );
		return filename;
	}
	
	public static void screenshotHiRes( PApplet p, int scaleFactor, String p5Renderer, String outputDir ) {
		// from: http://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/
		PGraphics hires = p.createGraphics(p.width*scaleFactor, p.height*scaleFactor, p5Renderer );
		hires.beginDraw();
		p.beginRecord(hires);
		hires.scale(scaleFactor);
		p.smooth();
		p.draw();
		p.endRecord();
		hires.endDraw();
		hires.save( outputDir + SystemUtil.getTimestamp(p) + "-hires.png" );
		p.noSmooth();
	}

	public static PImage getScreenAsPImage( PApplet p ) {
		return p.get();
	}
	
	public static PImage getScreenShotAsPImage( PApplet p ) {
		try {
			Robot robot = new Robot();
			PImage screenshot = new PImage(robot.createScreenCapture(new Rectangle(0,0,p.width,p.height)));
			return screenshot;
		} catch (AWTException e) { }
		return null;
	}

}


@@ -83,40 +83,182 @@
 awt.
-Rectangle;%0Aimport java.awt.Robot
+GraphicsConfiguration;%0Aimport java.awt.GraphicsDevice;%0Aimport java.awt.GraphicsEnvironment;%0Aimport java.awt.Rectangle;%0Aimport java.awt.Robot;%0Aimport java.awt.geom.Rectangle2D
 ;%0A%0Ai
@@ -426,17 +426,16 @@
 nUtil %7B%0A
-%09
 %0A%09public
@@ -617,25 +617,24 @@
 ilename;%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -1138,86 +1138,8 @@
 %09%7D%0A%0A
-%09public static PImage getScreenAsPImage( PApplet p ) %7B%0A%09%09return p.get();%0A%09%7D%0A%09%0A
 %09pub
@@ -1402,11 +1402,850 @@
 ll;%0A%09%7D%0A%0A
+%09public static PImage getScreenShotAllMonitors() %7B%0A%09%09return getScreenShotAllMonitors(0,0);%0A%09%7D%0A%09%0A%09public static PImage getScreenShotAllMonitors(int x, int y) %7B%0A%09%09// NOTE! main monitor must be top left, otherwise, override x+y position  %0A%09%09try %7B%0A%09%09%09Rectangle2D result = new Rectangle2D.Double();%0A%09%09%09GraphicsEnvironment localGE = GraphicsEnvironment.getLocalGraphicsEnvironment();%0A%09%09%09for (GraphicsDevice gd : localGE.getScreenDevices()) %7B%0A%09%09%09%09for (GraphicsConfiguration graphicsConfiguration : gd.getConfigurations()) %7B%0A%09%09%09%09%09Rectangle2D.union(result, graphicsConfiguration.getBounds(), result);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09Robot robot = new Robot();%0A%09%09%09PImage screenshot = new PImage(robot.createScreenCapture(new Rectangle(x, y,(int) result.getWidth(), (int) result.getHeight())));%0A%09%09%09return screenshot;%0A%09%09%7D catch (AWTException e) %7B %7D%0A%09%09return null;%0A%09%7D%0A%0A
 %7D%0A%0A

Add a screenshot method for multiple monitors