Click in the applet window to control the particle system.

// Learning Processing
// Daniel Shiffman

// Example 23-2: Simple particle system with ArrayList

ArrayList particles;

void setup() {
  particles = new ArrayList();

void draw() {
  // A new Particle object is added to the ArrayList every cycle through draw().
  particles.add(new Particle()); 

  // Iterate through our ArrayList and get each Particle
  // The ArrayList keeps track of the total number of particles.
  for (int i = 0; i < particles.size(); i++ ) { 
    Particle p = (Particle) particles.get(i);;

  // If the ArrayList has more than 100 elements in it, we delete the first element, using remove().
  if (particles.size() > 100) {
// Learning Processing
// Daniel Shiffman

// Example 23-2: Simple particle system with ArrayList

// A simple Particle class
class Particle {
  float x;
  float y;
  float xspeed;
  float yspeed;
  Particle() {
    x = mouseX;
    y = mouseY;
    xspeed = random(-1,1);
    yspeed = random(-2,0);
  void run() {
    x = x + xspeed;
    y = y + yspeed;
  void gravity() {
    yspeed += 0.1;
  void display() {
  • JBG724

    Good example. Thank you. The Processing reference is not clear on how to call methods w/in classes when using ArrayList .

  • gdfgdf

    How would I reference the x value of a particle in that arraylist?

  • shiffman

    p.x assuming p is a particle from the ArrayList i.e. Particle p = particles.get(someindex);

  • bruce

    What does it mean on line…Particle p = (Particle) particles.get(i);
    Specifically, why the syntax (Particle) ? Is there a reference I can look this up? This is different from what is shown in, where they don’t have (Particle)

  • shiffman

    this is an old way of using ArrayLists without “generics”. It’s explained in the LP book. You have to tell the ArrayList what kind of thing is coming out of it (even though it seems redundant). I’ll update the example soon.

  • yoni

    could you give a particle example with a pshape, say a star?

  • shiffman

    take a look at the “create shapes” examples in the Processing IDE. There is one that is much like a particle system with a star!

  • yoni

    thanks a lot, your attention is appreciated. going there.

  • jordan

    i love this example! could you please tell me how to reduce the number of circles falling? thank you :)

  • shiffman

    a new particle is added each time in draw(). i would start by adding them less frequently.

  • MISG

    Hi. I´m running this code in eclipse, but there is a problem whut the ArrayList, do you know if I have to include a library? Thanks in advance

  • Jade

    Hello Daniel, I’m getting into coding and i’m doing a project that i’m showcasing in May for my end of year degree, What i’d love to do is particles with webcam, so i can basically turn pixels into particles with a kinect or a webcam, Would you be able to help me with this, or point in in the right direction? this sketch is a start but i still need to get the webcam thing going and then i can also work on the keypressed function so that it can go from one colour of particles to another or another shape or something, I have pressure mats for that.

    Thank you!

  • shiffman

    I would suggest posting some questions at Send me a link to your post so I can follow / contribute!