Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created February 28, 2014 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobjoaquin/9273597 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/9273597 to your computer and use it in GitHub Desktop.
PGraphicsStack Proof of concept. Use a stack for PGraphics like pushMatrix() and pushStyle() in Processing.
PGraphicsStack pgs = new PGraphicsStack();
class PGraphicsStack {
private ArrayList<PGraphics> pgList;
private ArrayList<PVector> dimensionsList;
PGraphicsStack() {
pgList = new ArrayList<PGraphics>();
dimensionsList = new ArrayList<PVector>();
}
PGraphics push() {
return push(createGraphics(width, height));
}
PGraphics push(int w, int h) {
return push(createGraphics(w, h));
}
PGraphics push(int w, int h, String renderer) {
PGraphics pg = createGraphics(w, h, renderer);
return push(pg);
}
PGraphics pushCopy() {
PGraphics pgCopy = createGraphics(width, height);
pgCopy.copy(g, 0, 0, width, height, 0, 0, width, height);
return push(pgCopy);
}
PGraphics push(PGraphics pg) {
pgList.add(g);
g = pg;
dimensionsList.add(new PVector(width, height));
width = g.width;
height = g.height;
g.beginDraw();
return g;
}
PGraphics pop() {
g.endDraw();
PGraphics pgReturn = g;
g = pgList.remove(pgList.size() - 1);
PVector d = dimensionsList.remove(dimensionsList.size() - 1);
width = (int) d.x;
height = (int) d.y;
return pgReturn;
}
}
void setup() {
size(500, 500);
}
void draw() {
stroke(0, 12);
noFill();
float d = random(width);
ellipse(width / 2, height / 2, d, d);
PGraphics pg = pgs.push(width / 2, height / 2);
stroke(0, 12);
line(random(width), random(height), random(width), random(height));
pgs.pop();
image(pg, 0, 0);
image(pg, 0, height / 2);
image(pg, width / 2, 0);
image(pg, width / 2, height / 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment