OOP Animated Circles By John Keston

// Simple OOP example "Spot"
int numSpots = 200;
float rd;
Spot[] spots = new Spot[numSpots]; // Declare the object

void setup() {
  size(800,200);
  smooth();
  noStroke();
  for ( int i=0; i < spots.length; ++i ) {
    rd = random(10,50); // random diameter
    spots[i] = new Spot(random(width),random(rd/2,height-rd/2),rd,random(9)); // Construct the object
  }
}
void draw() {  
  fill(0,15);
  rect(0,0,width,height);
  fill(255,128);
  for ( int i=0; i < spots.length; ++i ) {
    spots[i].move();
    spots[i].display();
  }
}
class Spot { // Define the class
  float x,y,d,s; // x position, y position, diameter, speed
  int dir = 1;
  Spot(float xpos, float ypos, float diameter, float speed) { // Constructor for the object 
    x = xpos;
    y = ypos;
    d = diameter;
    s = speed;
  } 
  void move() {
    y += (s * dir);
    if ( (y > (height-d/2)) || (y < d/2) ) {
      dir *= -1;
    }
    // println("y: "+y+" s: "+s+" dir: "+dir);
  }
  void display() { // Method (function) to display the object
    ellipse(x,y,d,d);
  }
}
[top]