added with some lines to give a random color to each ellipse. It works fine, but i want to draw every new ellipse
in front of the last one rather than
behind.
ArrayList balls;
int ballWidth = 48;
void setup() {
size(200, 200);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// Start by adding one element
balls.add(new Ball(width/2, 0, ballWidth));
}
void draw() {
background(255);
// With an array, we say balls.length, with an ArrayList, we say balls.size()
// The length of an ArrayList is dynamic
// Notice how we are looping through the ArrayList backwards
// This is because we are deleting elements from the list
for (int i = balls.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
void mouseDragged() {
// A new ball object is added to the ArrayList (by default to the end)
balls.add(new Ball(mouseX, mouseY, ballWidth));
}
// Simple bouncing ball class
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
float life = 255;
PVector col;
Ball(float tempX, float tempY, float tempW) {
x = tempX;
y = tempY;
w = tempW;
speed = 0;
gravity = 0.1;
col = new PVector(random(255),random(255),random(255));