06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Exercise 17-9: Using textWidth( ) , redo Example 17-4 (the text “ mirror ” ) <strong>to</strong> use a nonfi<br />

xed-width font with proper character spacing. Th e following image uses Arial.<br />

Text 319<br />

Th is “ letter by letter ” methodology can also be applied <strong>to</strong> a sketch where characters from a String move<br />

independently of one another. Th e following example uses object-oriented design <strong>to</strong> make each character<br />

from the original String a Letter object, allowing it <strong>to</strong> both be displayed in its proper location as well as<br />

move about the screen individually.<br />

Example 17-6: Text breaking up<br />

PFont f;<br />

String message = " click mouse <strong>to</strong> shake it up ";<br />

// An array of Letter objects<br />

Letter[] letters;<br />

void setup() {<br />

size(260,200);<br />

// Load the font<br />

f = createFont( "Arial",20,true);<br />

textFont(f);<br />

// Create the array the same size as the String<br />

letters = new Letter[message.length()];<br />

// Initialize Letters at the correct x location<br />

int x = 16;<br />

for (int i = 0; i < message.length(); i + + ) {<br />

letters[i] = new Letter(x,100,message.charAt(i));<br />

x + = textwidth(message.charAt(i));<br />

}<br />

}<br />

fi g. 17.11<br />

Letter objects are initialized with<br />

their location within the String<br />

as well as what character they<br />

should display.

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

Saved successfully!

Ooh no, something went wrong!