Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Processing 2.0] Create Transparent Window [Java, Frame]
/**
* Author: Tim Pulver
* Date: 2013
* Tested with Processing 2.0
* original code by jungalero (processing.org forum)
*/
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
PImage screenShot;
int W = 400; // width
int H = 400; // height
int X = 0; // x-position of the window
int Y = 0; // y-position of the window
int XOFF = 0; // x-offset = distance to the right screen boarder
int YOFF = 100; // y-offset = distance to the top screen boarder
void setup() {
// if the window should be fullscreen uncomment this
//W = displayWidth;
//H = displayHeight;
size(W, H);
screenShot = getScreen(X, Y, W, H);
initFrame();
}
/**
* Removes the windowboarder
*/
void initFrame(){
frame.removeNotify();
frame.setUndecorated(true);
frame.addNotify();
}
void draw () {
frame.setLocation(X, Y);
image(screenShot,0,0, width, height);
// draw something to see the window
fill(0);
ellipse(width/2, height/2, 200, 200);
}
/**
* Returns an image of the screen (full)
*/
PImage getFullscreen(){
return getScreen(0, 0, displayWidth, displayHeight);
}
/**
* Returns an area of the screen (screenshot)
* @param x Top-Left corner of the area to copy (x)
* @param y Top Left corner of the area to copy (y)
* @param w Width of the rectangle
* @param h Height of the rectangle
*/
PImage getScreen(int x, int y, int w, int h) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
DisplayMode mode = gs[0].getDisplayMode();
Rectangle bounds = new Rectangle(x, y, w, h);
BufferedImage desktop = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
try {
desktop = new Robot(gs[0]).createScreenCapture(bounds);
}
catch(AWTException e) {
System.err.println("Screen capture failed.");
}
return (new PImage(desktop));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment