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.

Create and make an asynchronous request<br />

htmlRequest = new HTMLRequest(this, " http://www.yahoo.com " );<br />

htmlRequest.makeRequest();<br />

timer.start();<br />

background(0);<br />

}<br />

void draw() {<br />

background(back);<br />

// Every 5 seconds, make a new request<br />

if (timer.isFinished()) {<br />

htmlRequest.makeRequest();<br />

println( "Making request! ");<br />

timer.start();<br />

}<br />

// Draw some lines with colors based on characters from data retrieved<br />

for (int i = 0; i < width; i + + ) {<br />

if (i < html.length()) {<br />

int c = html.charAt(i);<br />

stroke(c,150);<br />

line(i,0,i,height);<br />

}<br />

}<br />

// Animate rectangle and dim rectangle<br />

fill(255);<br />

noStroke();<br />

rect(counter,0,10,height);<br />

counter = (counter + 1) % width;<br />

back = constrain(back – 1,0,255);<br />

}<br />

// When a request is finished<br />

void netEvent(HTMLRequest ml) {<br />

html = ml.readRawSource(); // Read the raw data<br />

back = 255; // Reset background<br />

println( "Request completed! "); // Print message<br />

}<br />

// Timer Class from Chapter 10<br />

class Timer {<br />

int savedTime;<br />

boolean running = false;<br />

int <strong>to</strong>talTime;<br />

Timer(int tempTotalTime) {<br />

<strong>to</strong>talTime = tempTotalTime;<br />

}<br />

void start() {<br />

running = true;<br />

savedTime = millis();<br />

}<br />

boolean isFinished() {<br />

int passedTime = millis() – savedTime;<br />

if (running & & passedTime > <strong>to</strong>talTime) {<br />

running = false;<br />

An HTML Request object <strong>to</strong> request<br />

the source from a URL.<br />

fi g. 18.11<br />

Data Input 343<br />

A request is made every 5 s. The data is not<br />

received here, however, this is only the request.<br />

The data is received in the netEvent() function<br />

which is au<strong>to</strong>matically called whenever data<br />

is ready.

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

Saved successfully!

Ooh no, something went wrong!