# Draw() inside a class

**URL:** <https://discourse.processing.org/t/draw-inside-a-class/34207>\
**Category:** Libraries\
**Created:** [December 20, 2021, 11:48am UTC](https://discourse.processing.org/t/draw-inside-a-class/34207 "2021-12-20T11:48:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skycstls](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@Skycstls](https://discourse.processing.org/u/Skycstls)\
**Post date:** [December 20, 2021, 11:48am UTC](https://discourse.processing.org/t/draw-inside-a-class/34207/1 "2021-12-20T11:48:48Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 20, 2021, 1:06pm UTC](https://discourse.processing.org/t/draw-inside-a-class/34207/2 "2021-12-20T13:06:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 24, 2021, 2:39pm UTC](https://discourse.processing.org/t/draw-inside-a-class/34207/3 "2021-12-24T14:39:16Z")

</div>

You might find this [post](https://discourse.processing.org/t/multiple-transformations/34243/4) interesting as it gives an insight into using object oriented programming (i…e. classes and objects) for animation .
