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

582<br />

btnBackground = btnOverState;<br />

} else {<br />

cursor(ARROW);<br />

btnBackground = btnUpState;<br />

}<br />

}<br />

The sketch makes a little more sense visually now. <strong>Processing</strong>’s cursor(cursor icon)<br />

function makes it really easy to change the cursor icon. The different built-in icons are<br />

ARROW, CROSS, HAND, MOVE, TEXT, <strong>and</strong> WAIT. Please note that once you change the cursor<br />

icon, the new icon will remain until you call the cursor(cursor icon) function again.<br />

The final version of this sketch (shown in Figure 12-2) will make it even more user-friendly,<br />

<strong>and</strong> will also add a few more interface elements, such as buttons with labels.<br />

Unfortunately, making the sketch simpler for the user can mean more (potentially a lot<br />

more) work for the coder. Creating ergonomically friendly interfaces requires a lot of<br />

code, especially when the language (like <strong>Processing</strong>) doesn’t rely on a set of prebuilt widgets<br />

(buttons, sliders, spinners, etc.).<br />

Please note that the following Final Interactive Button Example sketch requires that<br />

the <strong>Processing</strong> font Verdana-10.vlw be installed in the sketch’s data directory. You<br />

can create the font yourself by using <strong>Processing</strong>’s Create Font comm<strong>and</strong>, found under<br />

the Tools menu, or you can download the font from the Download section on the<br />

friends of ED web site (www.friendsofed.com/downloads.html).<br />

// Final Interactive Button Example<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 />

// moving square<br />

float x = 25;<br />

float w = 10;<br />

float h = w;<br />

// r<strong>and</strong>omly place square y position on screen<br />

float y = r<strong>and</strong>om(h/2, 70);

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

Saved successfully!

Ooh no, something went wrong!