// Box containing moving Ball objects
class Box {
// Attributes
float x, y; // location of top-left corner of box
float w, h; // width and height of box
int nBalls; // # of balls in box
Ball[] balls; // all ball objects (nBalls) in box
// Constructors
Box(float _x, float _y, float _w, float _h, int n) {
x = _x;
y = _y;
w = _w;
h = _h;
nBalls = n;
// Create the balls array to contains nBalls objects
balls = new Ball[nBalls];
// Create nBalls Ball objects
for (int i=0; i < nBalls; i++) {
balls[i] = new Ball(random(w), random(h), 2, color(0), this);
}
} // Box()
// accessors
float getWidth() {
return this.w;
}
float getHeight() {
return this.h;
}
// Behaviors
void display() {
pushMatrix();
translate(x, y);
// Draw the box at <x, y> of width w, heighht, h
stroke(0);
fill(255);
rect(0, 0, w, h);
// Draw all the balls in the box
for (int i=0; i < balls.length; i++) {
balls[i].display();
}
popMatrix();
} // display()
void update() {
for (int i=0; i < balls.length; i++) {
balls[i].move();
}
} // update()
} // class Box