04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

these elements is the creation of an off-screen image, also sometimes referred to as an<br />

image buffer. Beyond drawing to the display window, you can also use the<br />

createGraphics() function to generate a PGraphics object that you can draw directly<br />

into. You can then output this off-screen image to the screen, or even save it to a file.<br />

The next example uses these two structures to generate an off-screen image that will<br />

eventually be turned into a tiled background (see Figure A-18). The sketch allows a user to<br />

draw within the display window. The drawing is simultaneously output to the screen (using<br />

<strong>Processing</strong>’s built-in graphics context) <strong>and</strong> written to an off-screen buffer (using a<br />

PGraphics object created with the createGraphics() function). When the user releases<br />

the mouse, the off-screen image is scaled, positioned, <strong>and</strong> drawn to the screen as a tiled<br />

fill pattern. Notice in the sketch code that the two PGraphics methods beginDraw() <strong>and</strong><br />

endDraw() are used, in a sense, as entry <strong>and</strong> exit portals for drawing to the off-screen<br />

PGraphics object.<br />

/* Tile Designer<br />

Ira Greenberg, October 11, 2006<br />

draw to off-screen buffer<br />

to create a tiled background */<br />

PGraphics p;<br />

int tiles = 10;<br />

float strokeWt = 2.75;<br />

float scaleFactor;<br />

boolean isRecordable = false;<br />

void setup() {<br />

size(400, 400);<br />

background(0);<br />

smooth();<br />

float tileWidth = width/tiles;<br />

scaleFactor = tileWidth/width;<br />

}<br />

void draw() {<br />

// write to off-screen buffer<br />

if (isRecordable){<br />

p.line(pmouseX, pmouseY, mouseX, mouseY);<br />

}<br />

// preview drawing of tile<br />

if (mousePressed){<br />

stroke(255);<br />

line(pmouseX, pmouseY, mouseX, mouseY);<br />

}<br />

}<br />

void mousePressed(){<br />

p = createGraphics(width, height, JAVA2D);<br />

noStroke();<br />

PROCESSING LANGUAGE API<br />

735<br />

A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!