# Adding objects to the canvas fom within the mousePressed() method

**URL:** <https://discourse.processing.org/t/adding-objects-to-the-canvas-fom-within-the-mousepressed-method/24121>\
**Category:** Beginners\
**Created:** [September 25, 2020, 6:56pm UTC](https://discourse.processing.org/t/adding-objects-to-the-canvas-fom-within-the-mousepressed-method/24121 "2020-09-25T18:56:11Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Hyperion65](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hyperion65/32/10569_2.png) [@Hyperion65](https://discourse.processing.org/u/Hyperion65)\
**Post date:** [September 25, 2020, 9:27pm UTC](https://discourse.processing.org/t/adding-objects-to-the-canvas-fom-within-the-mousepressed-method/24121/4 "2020-09-25T21:27:19Z")

</div>

draw is you friend ! even if you do nothing, you need it.

right now you “setup” your display and make it black. after that nothing can happen, because draw does not run. even when you leave it empty and use the rest of your code “as is”, you will see it works.  
“real” programmers will probably stone me for saying this, but it does not matter (most of the time) where your code is written to. at least for your purpose, to get more intimate with processing 😉

saying:

```auto
void setup() {
  size(400, 400);
}
void draw() {
  background(0, 0, 0);
  ellipse(mouseX, mouseY, 10, 10);
}
void drawAnEllipse () {
}

```

is the same as:

```auto
void setup() {
  size(400, 400);
}
void draw() {
  background(0, 0, 0);  
  drawAnEllipse();
}
void drawAnEllipse() {
  ellipse(mouseX, mouseY, 10, 10);
}

```

think of the method as a roadsign where your code makes a little detour 😉

it is not inappropriate to use draw ! one of the good reasons to use a method is to keep your code understandable for… yourself. if you have really a lot of lines, it makes sense to break your code down into different parts and put them into methods which you then call from inside draw.

it is not so dissimilar to why people invented for-loops (somewhere back in the stoneage I think…).

instead of writing “ellipse (x,y,10,10);” 100 times, writing down different x,y values for different positions,  
it is (in part) better to write:

```auto
for (int i=0;i<100;i++) {
ellipse (x,y,10,10);
}

```

now you could even do uncanny stuff like having a formula for x, every round… or x and y.

```auto
for (int i=0;i<100;i++) {
ellipse (x,y,10,10);
x = random (0,100);
y = random (0,100);
}

```

mousePressed () is “special” because, similar to draw(), it runs all the time. (like mouseReleased() and others. mouseX and mouseY are also update every frame when your code runs.

yet draw() is the thing that… well, draws to the display, makes sure that the code is executed repeatedly.

---

_[View the full topic](https://discourse.processing.org/t/adding-objects-to-the-canvas-fom-within-the-mousepressed-method/24121)._
