I need help coming up with a very simple little polymorphism animation

I need to make a very simple and small animation using polymorphism. I have looked at examples and watched video but most of them are too complicated or large, and I want something small. Any suggestions on what I could do and how to go about doing it? Thanks.

abstract class BaseObject {
  float x, y;
  BaseObject(){
    x = random(width);
    y = random(height);
  }
  abstract void draw();
}

class Sqa extends BaseObject {
  void draw(){
    fill(0,0,200);
    rect(x,y,20,20);
  }
}

class Tri extends BaseObject {
  void draw(){
    fill(0,200,0);
    triangle(x,y+20,x+10,y,x+20,y+20);
  }
}

class Dot extends BaseObject {
  void draw(){
    fill(200,0,0);
    ellipse(x+10,y+10,20,20);
  }
}

BaseObject[] shapes = new BaseObject[3];

void setup(){
  size(200,200);
  noStroke();
  shapes[0] = new Sqa();
  shapes[1] = new Tri();
  shapes[2] = new Dot();
  for( int i = 0; i < 3; i++){
    shapes[i].x = 40 + 50 *i;
    shapes[i].y = 90;
  }
}

void draw(){
  background(0);
  for( int i = 0; i < 3; i++){
    shapes[i].draw();
  }
}

Here we have a β€œsmall” example of polymorphism. While it may seem like our array only has three β€œBaseObject” objects in it, in fact the three shapes are only pretending to be BaseObjects so that they fit in the array - they are actually three very different types of objects that can pretend to be Basic Shapes because they extend on its definition!

1 Like

An interesting exercise along these lines would would be to apply TfGuy44s example to the RegularPolygon demo sketch, changing the function into classes. Simple, shared assumptions are easy to turn into a base.

https://processing.org/examples/regularpolygon.html