*********Ball in colored box sketch************ // colored boxes are organized coloredBox[] boxes; // an array of colored boxes color[] cols = { // colors for the boxes color(255, 142, 142), color(142, 250, 255), color(142, 255, 205), color(255), color(127), color(69, 227, 2), color(227, 216, 2), color(255, 255, 0), color(227, 126, 2) }; void setup() { size(500, 500); smooth(); // create the boxes array boxes = new coloredBox[9]; // create individual boxes int i=0; float bw = width/3; float bh = height/3; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { boxes[i] = new coloredBox(c*bw, r*bh, bw, bh, int(random(10, 30)), cols[i]); i = i+1; } } } // setup() void draw() { background(255); // update & display all boxes for (int i=0; i < 9; i++) { boxes[i].update(); boxes[i].display(); } } // draw() ***************The basic ball class... // The basic ball class... class ball { // Attributes float radius; // size float x, y; // location color ballColor; float dx = 1; float dy; float speed = 5.0; float gravity = 0.1; float dampen = -0.9; // Constructor(s) ball() { radius = 25; x = random(width); y = random(height); ballColor = color(59, 90, 255); //dx = random(-3, 3); dy = random(-3, 3); } // ball() ball(float r, color c) { radius = r; x = random(width); y = random(height); ballColor = c; //dx = random(-3, 3); dy = random(-3, 3); } // ball() ball(float _x, float _y, float _r, color _c) { x = _x; y = _y; radius = _r; ballColor = _c; dy = random(-3, 3); } // ball() // Behaviors void display() { // display the ball object noStroke(); fill(ballColor); ellipse(x, y, 2*radius, 2*radius); } // display() // move the ball void move() { x += dx; y += speed; speed = speed + gravity; bounce(); // check bounces } // move() void bounce() { if (x < radius) { x = radius; dx = -dx; } if (x > width-radius) { x = width-radius; dx = -dx; } if (y < radius) { y = radius; dy = -dy; } if (y > height - radius) { y = height-radius; dy = -dy; speed = speed*dampen; } } // bounce() } // class ball ***************Ball in Box... // This class is a subclass of the ball class // Notice use of super() to call the superclass constructor class ballInBox extends ball { // Inherits everything from ball class // additional attributes box myBox; ballInBox(float _x, float _y, float _r, color _c, box _b) { super(_x, _y, _r, _c); myBox = _b; } // ballInBox() // override the behavior of inherited bounce() void bounce() { if (x < radius) { x = radius; dx = -dx; } if (x > myBox.w-radius) { x = myBox.w-radius; dx = -dx; } if (y < radius) { y = radius; dy = -dy; } if (y > myBox.h - radius) { y = myBox.h-radius; dy = -dy; speed = speed*dampen; } } // bounce() } // class ballInBox ***************Box class... // a box has its dimensions and a bunch of balls in it class box { // attributes float x, y; // top left coords of a box float w, h; // width & height of the box ballInBox[] balls; // Every box contains a number of balls int nBalls; // constructor(s) box() { this(random(width), random(height), 50, 50, 10); } // box() box(float _x, float _y, float _w, float _h, int n) { x = _x; y = _y; w = _w; h = _h; nBalls = n; // create n balls balls = new ballInBox[nBalls]; // initialize ball objects for (int i=0; i < nBalls; i++) { balls[i] = new ballInBox(random(w), random(height/4, h), 2, color(0), this); } } // box() // behavior(s) void display() { pushMatrix(); // draw the box translate(x, y); stroke(0); fill(255); rect(0, 0, w, h); // draw the balls in it for (int i=0; i < balls.length; i++) { balls[i].display(); } popMatrix(); } // display() void update() { // move every ball as it is supposed to move for (int i=0; i < balls.length; i++) { balls[i].move(); } } // update() } // class ***************The colored box is a subclass of box... // The colored box is a subclass of box... class coloredBox extends box { // new attribute color boxColor; // new constructor coloredBox(float _x, float _y, float _w, float _h, int _n, color _c) { super(_x, _y, _w, _h, _n); boxColor = _c; } // coloredBox void display() { pushMatrix(); // draw the box translate(x, y); stroke(0); fill(boxColor); rect(0, 0, w, h); // draw the balls in it for (int i=0; i < balls.length; i++) { balls[i].display(); } popMatrix(); } // display() } // class coloredBox