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 />

586<br />

//button 3<br />

if (mouseX>btn3X-btn3W/2 && mouseX< btn3X+btn3W/2 && ➥<br />

mouseY>btn3Y-btn3H/2 && mouseY< btn3Y+btn3H/2){<br />

cursor(HAND);<br />

}<br />

else {<br />

cursor(ARROW);<br />

}<br />

}<br />

Figure 12-2. Final Interactive Button Example sketch<br />

The finished interactive buttons example is likely a little intimidating, mostly due to its<br />

length. One of the (annoying) difficulties of coding graphical interfaces is the sheer<br />

amount of required code. Although verbose, interface code is not overly complex. Much<br />

of the code is redundant. For example, the last sketch included three buttons (one of<br />

which operated as a check box) which all shared a lot of common functionality.<br />

// font used for button text<br />

PFont myFont;<br />

// buttons<br />

float btn1X, btn1Y, btn1W, btn1H;<br />

float btn2X, btn2Y, btn2W, btn2H;<br />

float btn3X, btn3Y, btn3W, btn3H;<br />

color btnUpState = color(200, 200, 200);<br />

color btnOverState = color(200, 200, 50);<br />

color btnDownState = color(255, 150, 20);<br />

color btn1Background = btnUpState;<br />

color btn2Background = btnUpState;<br />

All three buttons are defined by btn#x, btn#y, btn#w, <strong>and</strong> btn#h properties. (The # symbol<br />

just represents the different button numbers.) I could have made this example more efficient<br />

<strong>and</strong> elegant by creating a Button or even Widget class, since all the objects have the<br />

same four properties. This would be an excellent thing to try on your own. The rest of<br />

the example should be straightforward, <strong>and</strong> stuff you’ve seen before. For example, notice<br />

the following conditional statement within the draw() function:

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

Saved successfully!

Ooh no, something went wrong!