Raindrop code

This example is taken from here.

It has been modified to use an ArrayList (resizable array). Array lists are very similar to arrays, they are data structures that are lists however unlike an array they can be resized. This means they can have spots added to them and spots subtracted as the code runs. In the code below the array list to hold the drops is declared first with this line:

ArrayList<Drop> drops = new ArrayList<Drop>();      // An array of drop objects

Then we put a drop into it in void setup here:

  drops.add(new Drop());    // Add a drop to the arrayList

And then we run through the arrayList and call the methods of each object in it using the following:

// Move and display all drops
for(int i=0;i<drops.size();i++){
    //create a temporary object for i
    Drop dr = drops.get(i); //we set up a temporary local variable called dr to call the methods of each index number of the array
    //call its methods
    dr.move();
    dr.display();
 
 
    if (catcher.intersect(dr)) {
      dr.caught(i);
    }
  }

Completed code is here:

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
 
// Example 10-10: The raindrop catching game
 
Catcher catcher;    // One catcher object
Timer timer;        // One timer object
ArrayList<Drop> drops = new ArrayList<Drop>();      // An array of drop objects
int totalDrops = 0; // totalDrops
 
void setup() {
  size(400,400);
  smooth();
  catcher = new Catcher(32); // Create the catcher with a radius of 32
  drops.add(new Drop());    // Add a drop to the arrayList
  timer = new Timer(300);   // Create a timer that goes off every 2 seconds
  timer.start();             // Starting the timer
}
 
void draw() {
  background(255);
 
  // Set catcher location
  catcher.setLocation(mouseX,mouseY); 
  // Display the catcher
  catcher.display(); 
 
  // Check the timer
  if (timer.isFinished()) {
    // Deal with raindrops
    // Initialize one drop
    drops.add(new Drop());   //  Adds a new Drop to the arrayList
    // Increment totalDrops
    totalDrops ++ ;
    timer.start();
  }
 
// Move and display all drops
for(int i=0;i<drops.size();i++){
    //create a temporary object for i
    Drop dr = drops.get(i);
    //call its methods
    dr.move();
    dr.display();
 
 
    if (catcher.intersect(dr)) {
      dr.caught(i);
    }
  }
}
 
 
 
class Catcher {
  float r;   // radius
  color col; // color
  float x,y; // location
 
  Catcher(float tempR) {
    r = tempR;
    col = color(50,10,10,150);
    x = 0;
    y = 0;
  }
 
  void setLocation(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  }
 
  void display() {
    stroke(0);
    fill(col);
    ellipse(x,y,r*2,r*2);
  }
 
  // A function that returns true or false based on
  // if the catcher intersects a raindrop
  boolean intersect(Drop d) {
    // Calculate distance
    float distance = dist(x,y,d.x,d.y); 
 
    // Compare distance to sum of radii
    if (distance < r + d.r) { 
      return true;
    } else {
      return false;
    }
  }
}
 
 
 
class Drop {
  float x,y;   // Variables for location of raindrop
  float speed; // Speed of raindrop
  color c;
  float r;     // Radius of raindrop
 
  Drop() {
    r = 8;                 // All raindrops are the same size
    x = random(width);     // Start with a random x location
    y = -r*4;              // Start a little above the window
    speed = random(1,5);   // Pick a random speed
    c = color(50,100,150); // Color
  }
 
  // Move the raindrop down
  void move() {
    // Increment by speed
    y += speed; 
  }
 
  // Check if it hits the bottom
  boolean reachedBottom() {
    // If we go a little beyond the bottom
    if (y > height + r*4) { 
      return true;
    } else {
      return false;
    }
  }
 
  // Display the raindrop
  void display() {
    // Display the drop
    fill(c);
    noStroke();
    for (int i = 2; i < r; i++ ) {
      ellipse(x,y + i*4,i*2,i*2);
    }
  }
 
  // If the drop is caught, pass it the index number of the array list.
  void caught(int i) {
 
    Drop dr = drops.get(i);
 
      drops.remove(i);
  }
}
 
 
 
class Timer {
 
  int savedTime; // When Timer started
  int totalTime; // How long Timer should last
 
  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }
 
  // Starting the timer
  void start() {
    // When the timer starts it stores the current time in milliseconds.
    savedTime = millis(); 
  }
 
  // The function isFinished() returns true if 5,000 ms have passed. 
  // The work of the timer is farmed out to this method.
  boolean isFinished() { 
    // Check how much time has passed
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}