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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

140<br />

able to h<strong>and</strong>le billions of operations per second, but each process is still distinct. The illusion<br />

that a single computer processor is h<strong>and</strong>ling simultaneous events is a function of our<br />

relatively slow eyes <strong>and</strong> their inability to see the tiny delays.<br />

Event h<strong>and</strong>ling<br />

Returning to <strong>Processing</strong> <strong>and</strong> Java’s event mechanism, if we don’t need to create our own<br />

loop to detect events, how is it done? Java’s runtime environment, the Java Virtual<br />

Machine (JVM), takes care of it for us. In addition, there is an Event class that encapsulates<br />

event properties <strong>and</strong> methods. When an event occurs, the Event class detects it <strong>and</strong> in a<br />

sense broadcasts it throughout the program. How we receive the broadcast <strong>and</strong> what we<br />

do with it is mostly what we have to worry about. This efficient system makes event h<strong>and</strong>ling<br />

relatively easy in Java <strong>and</strong>, as you might suspect, even easier in <strong>Processing</strong>. Here’s a<br />

simple interactive example in which you can click the screen to r<strong>and</strong>omize the sketch. The<br />

event detection code in the example happens within the if statement in the draw() function,<br />

checking the mousePressed condition. When the mouse is pressed, the condition<br />

evaluates to true <strong>and</strong> the setR<strong>and</strong>omStyle() function is called.<br />

// click to r<strong>and</strong>omize<br />

float x, y, squareSize;<br />

color bgColor, strokeColor, fillColor;<br />

float strokeWt;<br />

void setup(){<br />

size(400, 400);<br />

rectMode(CENTER);<br />

x = width/2;<br />

y = height/2;<br />

setR<strong>and</strong>omStyle();<br />

frameRate(30);<br />

}<br />

void draw(){<br />

background(bgColor);<br />

strokeWeight(strokeWt);<br />

stroke(strokeColor);<br />

fill(fillColor);<br />

rect(x, y, squareSize, squareSize);<br />

if (mousePressed){<br />

setR<strong>and</strong>omStyle();<br />

}<br />

}<br />

void setR<strong>and</strong>omStyle(){<br />

bgColor = color(r<strong>and</strong>om(255), r<strong>and</strong>om(255), r<strong>and</strong>om(255));<br />

strokeColor = color(r<strong>and</strong>om(255), r<strong>and</strong>om(255), r<strong>and</strong>om(255));<br />

fillColor = color(r<strong>and</strong>om(255), r<strong>and</strong>om(255), r<strong>and</strong>om(255));<br />

strokeWt = r<strong>and</strong>om(5, 100);<br />

squareSize = r<strong>and</strong>om(10, 300);<br />

}

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

Saved successfully!

Ooh no, something went wrong!