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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

318 <strong>Learning</strong> <strong>Processing</strong><br />

17.7<br />

Display text character by character.<br />

In certain graphics applications, displaying text with each character rendered individually is required. For<br />

example, if each character needs <strong>to</strong> move independently, then simply saying text( “ a bunch of letters ” ,0,0)<br />

will not do.<br />

Th e solution is <strong>to</strong> loop through a String , displaying each character one at a time.<br />

Let’s start by looking at an example that displays the text all at once. See Figure 17.8 .<br />

PFont f;<br />

String message = " Each character is not<br />

written individually." ;<br />

void setup() {<br />

size(400,200);<br />

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

}<br />

void draw() {<br />

background(255);<br />

fill(0);<br />

textFont(f);<br />

text(message,10,height/2);<br />

}<br />

We rewrite the code <strong>to</strong> display each character in a loop, using the charAt( ) function. See Figure 17.9 .<br />

int x = 10;<br />

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

text(message.charAt(i) ,x, height/2);<br />

x + = 10;<br />

}<br />

All characters are<br />

spaced 10 pixels<br />

apart.<br />

The fi rst character is at pixel 10.<br />

Each character is displayed<br />

one at a time with the<br />

charAt() function.<br />

Calling the text( ) function for each character will allow us more fl exibility in future examples (for<br />

coloring, sizing, and placing characters within one String individually). Th is example has a pretty<br />

major fl aw, however. Here, the x location is increased by 10 pixels for each character. Although this<br />

is approximately correct, because each character is not exactly 10 pixels wide, the spacing is off . Th e<br />

proper spacing can be achieved using the textWidth( ) function as demonstrated in the following code.<br />

Note how this example achieves the proper spacing even with each character being a random size!<br />

See Figure 17.10 .<br />

int x = 10;<br />

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

textSize(random(12,36));<br />

text(message.charAt(i) ,x, height/2);<br />

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

}<br />

textWidth() spaces the characters out properly.<br />

Each character is not written individually.<br />

fi g. 17.8<br />

Displaying a block of text all at once using text().<br />

fi g. 17.9 (Note how the spacing is incorrect.)<br />

fi g. 17.10 (Note how the spacing is correct even<br />

with differently sized characters!)

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

Saved successfully!

Ooh no, something went wrong!