import processing.core.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;

public class RandBoxes extends PApplet {
  int numBoxes = 1000;
  ArrayList boxes = new ArrayList();

  public void setup() {
    size(600, 500, JAVA2D);
    for (int i = 0; i<numBoxes; i++)
      boxes.add(new Box(random(width), random(height)));
  }
  public void draw() {
    background(128);
    for (int i = 0; i<boxes.size(); i++)
      ((Box) boxes.get(i)).step();
    if (frameCount%10==0)
      println("frameRate="+frameRate+" frameCount="+frameCount);
  }

  class Box {
    int c = color(255, 255, 0);
    float x, y;
    Box(float x, float y) {
      this.x = x;
      this.y = y;
    }
    public void step() {
      x = max(min(x+random(-1, 1), width), 0);
      y = max(min(y+random(-1, 1), height), 0);
      paint();
    }
    public void paint() {
      noStroke();
      fill(c);
      rect(x-2, y-2, 5, 5);
    }
  }

  /**
   * Test of Processing with a very simple model. Create 1000 instances of a
   * local Box class. Every frame have each box jiggle by a random amount.
   */
  static public void main(String args[]) {
    PApplet.main(new String[] { "RandBoxes" });
  }
}