Primary Squares By John Keston

// Simple OOP example "Primary Cards"
int numSquares = 200;
float rd;
Square[] squares = new Square[numSquares]; // Declare the object

void setup() {
	background(255);
  size(800,600);
  smooth();
  noStroke();
  for ( int i=0; i < squares.length; ++i ) {
    rd = random(10,50); // random swidth
    squares[i] = new Square(random(width),random(rd/2,height-rd/2),rd,random(9),floor(random(3))); // Construct the object
  }
}
void draw() {  
  fill(255,5);
  rect(0,0,width,height);
  for ( int i=0; i < squares.length; ++i ) {
    squares[i].travel();
    squares[i].move();
    squares[i].display();
  }
}
class Square { // Define the class
  float x,y,w,s; // x position, y position, swidth, speed
  int c;
  int dir = 1;
  Square(float xpos,float ypos,float swidth,float speed,int co) { // Constructor for the object 
    x = xpos;
    y = ypos;
    w = swidth;
    s = speed;
    c = co;
  } 
  void move() {
    y += (s * dir);
    if ( (y > (height-w/2)) || (y < w/2) ) {
      dir *= -1;
    }
    // println("y: "+y+" s: "+s+" dir: "+dir);
  }
  void display() { // Method (function) to display the object
    switch(c) {
      case 0:
        fill(255,0,0,128);
        break;
      case 1:
        fill(0,0,255,128);
        break;
      case 2:
        fill(255,255,0,128);
        break;
    }
    rect(x,y,w,w);
  }
  void travel() {
    translate(width/2, height/2);
    rotate(PI/(3*(mouseX/800)));
    x += (mouseX - x)/(s*40);
  }

}
[top]