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.

434 <strong>Learning</strong> <strong>Processing</strong><br />

23.6<br />

// Move<br />

void run() {<br />

x = x + xspeed;<br />

y = y + yspeed;<br />

}<br />

// Fall down<br />

void gravity() {<br />

yspeed + = 0.1;<br />

}<br />

// S<strong>to</strong>p moving<br />

void s<strong>to</strong>p() {<br />

xspeed = 0;<br />

yspeed = 0;<br />

}<br />

// Ready for deletion<br />

boolean finished() {<br />

life – = 2.0;<br />

if (life < 0) return true;<br />

else return false;<br />

}<br />

// Show<br />

void display() {<br />

stroke(0);<br />

fill(0,life);<br />

ellipse(x,y,10,10);<br />

}<br />

}<br />

Exercise 23-3: Write a But<strong>to</strong>n class that includes a Rectangle object <strong>to</strong> determine if the<br />

mouse has clicked inside the but<strong>to</strong>n’s area. (Th is is an extension of Exercise 9-8 .)<br />

Exception (Error) Handling<br />

In addition <strong>to</strong> a very large library of useful classes, the Java programming language includes some features<br />

beyond what we have covered while learning <strong>Processing</strong> . We will explore one of those features now:<br />

exception handling .<br />

<strong>Programming</strong> mistakes happen. We have all seen them.<br />

java.lang.ArrayIndexOutOfBoundsException<br />

java.io.IOException: openStream() could not open file.jpg<br />

java.lang.NullPointerException<br />

The Particle has a “life” variable which<br />

decreases. When it reaches 0 the Particle<br />

can be removed from the ArrayList.

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

Saved successfully!

Ooh no, something went wrong!