List shapes = new ArrayList();
void setup()
{
size(800, 800);
smooth();
for(int i = 0; i < 100; i++)
if(i % 2 == 0)
shapes.add(new Square());
else
shapes.add(new Circle());
}
void draw()
{
background(255, 255, 255);
Iterator i = shapes.iterator();
while(i.hasNext())
{
((Shape)i.next()).drawAndStep();
}
}
abstract class Shape
{
float x, y, r, xdir, ydir;
color f, s;
Shape()
{
x = random(width/4, width/2);
y = random(height/4, height/2);
r = random(20, 40);
xdir = random(-10, 10);
ydir = random(-10, 10);
color rc = color((int)random(255), (int)random(255), (int)random(255));
f = color(rc, 100);
s = color(rc, 200);
}
void drawAndStep()
{
fill(f);
stroke(s);
strokeWeight(2);
draw();
x+=xdir;
y+=ydir;
if(x + r/2 >= width || x - r/2 <= 0)
xdir *= -1;
if(y + r/2 >= height || y - r/2 <= 0)
ydir *= -1;
}
abstract void draw();
}
class Square extends Shape
{
void draw()
{
rectMode(CENTER);
rect(x, y, r, r);
}
}
class Circle extends Shape
{
void draw()
{
ellipse(x, y, r, r);
}
}