Draw() inside a class

Hey Processing forum!
I’ve been looking the Ani library for animation, was scrolling through the examples for using it and saw this class:

class Cirlce {
  float x = random(0,width);
  float y = random(0,height);
  int diameter = 5;
  Ani diameterAni;
  color c = color(0);

  Cirlce() {
    // diameter animation
    diameterAni = new Ani(this, random(1,5), 1.5, "diameter", 50, Ani.EXPO_IN_OUT, "onEnd:randomize");
    // repeat yoyo style (go up and down)
    diameterAni.setPlayMode(Ani.YOYO);
    // repeat 3 times
    diameterAni.repeat(3);
  }

  void draw() {
    fill(c);
    ellipse(x,y,diameter,diameter);
    fill(0);
    text(diameterAni.getRepeatNumber()+" / "+diameterAni.getRepeatCount(), x, y+diameter);
  }

  void randomize(Ani _ani) {
    c = lerpColor(from, to, random(1));

    // new repeat count
    int newCount = 1+2*round(random(4));
    diameterAni.repeat(newCount);
    // restart
    diameterAni.start();

    // move to new position
    Ani.to(this, 1.5, "x", random(0,width), Ani.EXPO_IN_OUT);
    Ani.to(this, 1.5, "y", random(0,height), Ani.EXPO_IN_OUT);
  }
}

Its the first time i see a draw loop inside a class, how exactly this works? i get that it can be called through a for loop in the main draw() but what are the advantages of this? can i see more examples about this?

The function/ method is just named draw().
A better name would be “display()”.

Processing also highlights the word as if it were the same like our beloved standard draw() outside the class which it’s not.

It doesn’t run automatically and you still have to call it like any other method in a class.

4 Likes

You might find this post interesting as it gives an insight into using object oriented programming (i…e. classes and objects) for animation .