Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: Obstacles

Particles

//
// Particle.java
//


import processing.core.*;


// The Particle class has all the code to display and move a single
// particle.  A particle is "dead" if it has fallen off screen.


public class Particle
{
    public Particle(PApplet p,
                    PVector position, PVector velocity, 
                    float radius, int color)
    {
        this.p = p;
        this.position = position.copy();
        this.velocity = velocity.copy();
        this.acceleration = new PVector(0, .1f); // gravity
        this.radius = radius;
        this.color = color;
    }

    public void display()
    {
        p.fill(color);
        p.noStroke();
        p.ellipse(position.x, position.y, 2*radius, 2*radius);

        position.add(velocity);
        velocity.add(acceleration);
    }

    public boolean isDead()
    {
        return position.y > p.height + 100;
    }

    private PApplet p;
    private PVector position;
    private PVector velocity;
    private PVector acceleration;
    private float radius;
    private int color;
}
//
// ParticleGenerator.java
//


import processing.core.*;
import java.util.*;


// The ParticleGenerator class has an ArrayList of Particles.  The
// display() function displays all the particles in the list,
// creates new particles based on the specified rate and direction,
// and removes old particles from the list.


public class ParticleGenerator
{
    public ParticleGenerator(PApplet p, 
                             PVector position, 
                             PVector direction, 
                             float rate)
    {
        this.p = p;
        this.position = position.copy();
        this.direction = direction.copy();
        this.rate = rate;

        this.particles = new ArrayList<Particle>();
    }

    public void display()
    {
        // display all particles in the list

        for (Particle b : particles)
            b.display();

        // create new particles, remove old particles

        createParticles();

        killDeadParticles();

        // draw box

        p.fill(128);
        p.rectMode(PApplet.CENTER);
        p.rect(position.x, position.y, 50, 50);

        // draw particle count

        p.fill(255);
        p.textSize(20);
        p.textAlign(PApplet.CENTER, PApplet.CENTER);
        p.text(particles.size(), position.x, position.y);
    }

    public void setPosition(float x, float y)
    {
        position.x = x;
        position.y = y;
    }

    private void killDeadParticles()
    {
        // remove any dead particles from the list

        for (int i=particles.size()-1; i>=0; i--)
            if (particles.get(i).isDead())
                particles.remove(i);
    }

    private void createParticles()
    {
        // add new particles to the list, based on the rate

        if (rate < 1)
        {
            if (p.random(0,1)<rate)
                createParticle();
        }
        else
        {
            for (int i=0; i<rate; i++)
                createParticle();
        }
    }

    private void createParticle()
    {
        // create a single particle, moving in the specified
        // direction (with a little random bump), with random size
        // and color, and add it to the list

        PVector velocity = direction.copy();
        PVector dv = PVector.random2D();
        velocity.add(dv);

        float radius = p.random(5, 10);
        int color = p.color(0, p.random(256), p.random(256), p.random(256));
        
        Particle b = new Particle(this.p,
                                  this.position,
                                  velocity,
                                  radius,
                                  color);
        particles.add(b);
    }

    private PApplet p;
    private PVector position;
    private PVector direction;
    private float rate;

    private ArrayList<Particle> particles;
}
//
// ParticleDemo.java
//


import processing.core.*;
import java.util.*;


// ParticleDemo is a PApplet that has two ParticleGenerator
// objects, a fountain and a rocket.  The fountain and rocket
// differ in the rate and direction of the particles they
// generate.


public class ParticleDemo extends PApplet
{
    public void settings()
    {
        size(800, 800);
    }

    public void setup()
    {
        fountain = new ParticleGenerator(this,
                          new PVector(width/4, height*.8f),
                          new PVector(0, -10), // direction: up
                          2f);

        rocket = new ParticleGenerator(this,
                          new PVector(mouseX, mouseY),
                          new PVector(-5, 0), // direction: left
                          .5f);
    }

    public void draw()
    {
        background(0);
        noCursor();

        fountain.display();

        rocket.display();
        rocket.setPosition(mouseX, mouseY); // move with mouse
    }

    private ParticleGenerator fountain;
    private ParticleGenerator rocket;

    public static void main(String[] args)
    {
        PApplet.main("ParticleDemo");
    }
}

Next: