ArrayList systems; void setup() { size(800, 700); systems = new ArrayList(); smooth(); } void draw() { background(0); Iterator psi = systems.iterator(); while (psi.hasNext ()) { ParticleSystem ps = psi.next(); ps.run(); if (!ps.isDying()) { ps.addParticle(); } if (ps.dead()) { psi.remove(); } } if (systems.isEmpty()) { fill(255); textAlign(CENTER); text("click mouse to add particle systems", width/2, height/2); } } void mousePressed() { systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY))); } class ParticleSystem { ArrayList particles; // An arraylist for all the particles PVector origin; // An origin point for where particles are birthed float timeOfDeath; ParticleSystem(int num, PVector v) { timeOfDeath = millis() + 6000; particles = new ArrayList(); // Initialize the arraylist origin = v.get(); // Store the origin point for (int i = 0; i < 100; i++) { particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist } } void run() { // Cycle through the ArrayList using Iterator we are deleting Iterator it = particles.iterator(); while (it.hasNext ()) { Particle p = it.next(); p.run(); if (p.isDead()) { it.remove(); } } } void addParticle() { particles.add(new Particle(origin)); } // void addParticle(Particle p) { // particles.add(p); // } // A method to test if the particle system still has particles boolean dead() { if (particles.isEmpty()) { return true; } else { return false; } } boolean isDying() { if (millis() > timeOfDeath) { return true; } else { return false; } } } class Particle { PVector location; PVector velocity; PVector acceleration; float lifespan; Particle(PVector l) { velocity = new PVector(random(-2, 2), random(-2, 2)); velocity.normalize(); velocity.x *= 2.0; velocity.y *= 2.0; location = l.get(); lifespan = 125.0; } void run() { update(); display(); } // Method to update location void update() { location.add(velocity); lifespan -= 2.50; } // Method to display void display() { noStroke(); fill(255, lifespan); ellipse(location.x, location.y, 7, 7); ellipse(location.x, location.y, 16, 16); } // Is the particle still useful? boolean isDead() { if (lifespan < 0.0) { return true; } else { return false; } } }