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.

set initial button background colors<br />

color pencilBg = buttonSelected;<br />

color brushBg = buttonUp;<br />

color eraserBg = buttonUp;<br />

// button booleans<br />

boolean isPencilSelected = true;<br />

boolean isBrushSelected = false;<br />

boolean isEraserSelected = false;<br />

As in the last example, I created up, over, <strong>and</strong> selected states for each of the buttons, as<br />

well as Boolean values to hold the specific button states. For example, I initialized the<br />

sketch by making the pencil active <strong>and</strong> the other tools inactive. Thus, I set<br />

isPencilSelected to true <strong>and</strong> isBrushSelected <strong>and</strong> isEraserSelected to false.<br />

// slider<br />

float[] sliderBar, sliderH<strong>and</strong>le;<br />

boolean isSliderMovable = false;<br />

color sliderH<strong>and</strong>leUp = color(255, 127, 0);<br />

color sliderH<strong>and</strong>leOver = color(255, 200, 0);<br />

color sliderH<strong>and</strong>leBg = sliderH<strong>and</strong>leUp;<br />

float sliderValue = 0;<br />

//clear button<br />

float[]clearBtn;<br />

color clearBtnOver = color(50, 50, 150);<br />

color clearBtnUp = color(0, 0);<br />

color clearBtnBg = clearBtnUp;<br />

The slider <strong>and</strong> the clear button were h<strong>and</strong>led very similarly to the tool buttons. I could<br />

have encapsulated this more <strong>and</strong> removed some of the redundant structures, but the code<br />

would have become denser <strong>and</strong> more inscrutable. I could have also used an objectoriented<br />

structure—building a base widget class <strong>and</strong> having each specific interactive<br />

element extend (inherit) it <strong>and</strong> then add its own specific properties/methods. This OOP<br />

approach would be an excellent exercise to try on your own.<br />

After declaring all the global variables, I then initialized many of them in the setup()<br />

function.<br />

void setup(){<br />

size(600, 400);<br />

background(canvasBg);<br />

frameRate(30);<br />

// create fonts<br />

titleFont = createFont("Arial", 11);<br />

clearFont = createFont("Arial", 10);<br />

INTERACTIVITY<br />

593<br />

12

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

Saved successfully!

Ooh no, something went wrong!