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.

430 <strong>Learning</strong> <strong>Processing</strong><br />

void draw() {<br />

particles.add(new Particle());<br />

background(255);<br />

// Iterate through our ArrayList and get each Particle<br />

for (int i = 0; i < particles.size(); i + + ) {<br />

Particle p = (Particle) particles.get(i);<br />

p.run();<br />

p.gravity();<br />

p.display();<br />

}<br />

// Remove the first particle when the list gets over 100.<br />

if (particles.size() > 100) {<br />

particles.remove(0);<br />

If the ArrayList has more than 100 elements<br />

}<br />

in it, we delete the fi rst element, using<br />

}<br />

remove().<br />

// A simple Particle class<br />

class Particle {<br />

float x;<br />

float y;<br />

float xspeed;<br />

float yspeed;<br />

Particle() {<br />

x = mouseX;<br />

y = mouseY;<br />

xspeed = random(–1,1);<br />

yspeed = random(–2,0); }<br />

void run() {<br />

x = x + xspeed;<br />

y = y + yspeed;<br />

}<br />

void gravity() {<br />

yspeed + = 0.1;<br />

}<br />

void display() {<br />

stroke(0);<br />

fill(0,75);<br />

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

}<br />

}<br />

A new Particle object is added <strong>to</strong> the<br />

ArrayList every cycle through draw().<br />

The ArrayList keeps track of how many<br />

elements it is s<strong>to</strong>ring and we iterate<br />

through them all.

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

Saved successfully!

Ooh no, something went wrong!