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.

X = �50<br />

fi g. 17.4<br />

float w = textWidth(headline) ;<br />

if (x < -w) {<br />

x = width;<br />

}<br />

Text 313<br />

Example 17-3 is a full example that displays a diff erent headline each time the previous headline leaves<br />

the screen. Th e headlines are s<strong>to</strong>red in a String array .<br />

Example 17-3: Scrolling headlines<br />

// An array of news headlines<br />

String[] headlines = {<br />

" <strong>Processing</strong> downloads break downloading record. ",<br />

" New study shows computer programming lowers cholesterol. ",<br />

} ;<br />

PFont f; // Global font variable<br />

float x; // Horizontal location<br />

int index = 0;<br />

void setup() {<br />

size(400,200);<br />

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

// Initialize headline offscreen<br />

x = width;<br />

}<br />

void draw() {<br />

background(255);<br />

fill (0);<br />

// Display headline at x location<br />

textFont(f,16);<br />

textAlign (LEFT );<br />

text(headlines[index],x,180);<br />

// Decrement x<br />

x = x – 3;<br />

X = 0<br />

Some text<br />

still on screen!<br />

Some text<br />

width = 100 pixels<br />

fi g. 17.5<br />

// If x is less than the negative width,<br />

// then it is off the screen<br />

float w = textWidth(headlines[index]);<br />

if (x < -w) {<br />

x = width;<br />

index = (index + 1) % headlines.length;<br />

}<br />

}<br />

X = �100<br />

Some text<br />

all the way off screen!<br />

If x is less than the negative width, then it is<br />

completely off the screen<br />

Multiple Strings are<br />

s<strong>to</strong>red in an array.<br />

A specifi c String from the array is<br />

displayed according <strong>to</strong> the value of the<br />

“index” variable.<br />

textWidth() is used <strong>to</strong> calculate the width<br />

of the current String.<br />

“index“ is incremented when the current<br />

String has left the screen in order <strong>to</strong><br />

display a new String.

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

Saved successfully!

Ooh no, something went wrong!