Created
February 28, 2014 15:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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