# Beginner's Bad Luck

**URL:** <https://discourse.processing.org/t/beginners-bad-luck/8542>\
**Category:** Beginners\
**Created:** [February 19, 2019, 4:53pm UTC](https://discourse.processing.org/t/beginners-bad-luck/8542 "2019-02-19T16:53:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenoneilsteven](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@stevenoneilsteven](https://discourse.processing.org/u/stevenoneilsteven)\
**Post date:** [February 19, 2019, 4:53pm UTC](https://discourse.processing.org/t/beginners-bad-luck/8542/1 "2019-02-19T16:53:51Z")

</div>

Hello,

I have been playing with Processing for the first time and have reached an obstacle.

```auto
void setup () {

  size(1920,1080);
  background(0);

}

void draw () {
  
  if (mousePressed) {
    fill(0);
    ellipse(mouseX, mouseY, 460, 460);
  } else {
    noStroke();
    fill(255);
    ellipse(mouseX, mouseY, 460, 460);
  }
  

}

```

When I open the window to test the code, there is always a circle locked in the top right unless I remove it. I’m sure it’s a simple fix.

Any help?

Steve

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 19, 2019, 9:18pm UTC](https://discourse.processing.org/t/beginners-bad-luck/8542/2 "2019-02-19T21:18:27Z")

</div>

mouse always starts top left,  
actually even if mouse is not over window  
( there are some more tricks to detect that )

but the first problem / question  
if you want paint the circles over each other its ok,

if you want only one circle ( following mouse ) use:

```auto
void setup () {
  size(500, 500);
  noStroke();
}
void draw () {
  background(200, 200, 0);
  if (mousePressed) fill(0,200,0);
  else fill(0,0,200);
  ellipse(mouseX, mouseY, width/2,width/2);
}

```

now i try some older tests about mouse in or not, combined with window focused or not,  
still there are situations where the top/left circle shows.

```auto

boolean paint = true;
public boolean mousePresent = false;

void setup () {
  size(500, 500);
  noStroke();
  if ( paint) background(0);
  println("use: key [p]");
}
void draw () {
  if ( !paint ) background(200, 200, 0);
  if ( mousePresent && focused ) {
    if (mousePressed) fill(0, 200, 0);
    else fill(0, 0, 200);
    ellipse(mouseX, mouseY, width/8, width/8);
  }
}

void keyPressed() {
  if ( key == 'p' ) paint = !paint;
}

public void mouseExited() {
  mousePresent = false;
}

public void mouseEntered() {
  mousePresent = true;
}

```

so

> [@stevenoneilsteven](#):
>
> I’m sure it’s a simple fix

no, looks like you even need a start state:

```auto
boolean paint = true, start = false, mousePresent = false;

void setup () {
  size(500, 500);
  noStroke();
  if ( paint) background(0);
  println("use: key [p] for toggle painting\nkey [s] for start");
}
void draw () {
  if ( !paint ) background(200, 200, 0);
  if ( mousePresent && focused && start) {
    if (mousePressed) fill(0, 200, 0);
    else fill(0, 0, 200);
    ellipse(mouseX, mouseY, width/8, width/8);
  }
}

void keyPressed() {
  if ( key == 'p' ) paint = !paint;
  if ( key == 's' ) start = true;
}

public void mouseExited() {
  mousePresent = false;
}

public void mouseEntered() {
  mousePresent = true;
}

```

now when you press key [s] the first circle appears at mouse position.

---

<div class="post-metadata">

**Author:** ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)\
**Post date:** [February 19, 2019, 11:26pm UTC](https://discourse.processing.org/t/beginners-bad-luck/8542/3 "2019-02-19T23:26:45Z")

</div>

If you have’t come to a clear resolution yet:

_mousePressed_ is a variable reflecting whether a mouse button, _any_ of the mouse buttons, are _currently_ pressed.

However you are continously drawing a new ellipse (circle is a special kind of ellipse) every time draw gets called (repeatedly from start of program til it stops) at the mouse position even if no buttons are pressed because of that if-else construct. And as the other guy has pointed out this will result in a canvas, so stop speak, covered in circles with the first occurring wherever the mouse cursor is at when the program starts (or the initial value of those variable before the actual position of the mouse is checked by the underlying code, most probably x=0, y=0 or (0,0)).

To avoid that you either need to clear/erase the screen so only the most recently drawn circle is visible or at least somehow wait a bit so that one is not drawn with a center point at (0,0). You might even wish to do both.

Also as an aside there is a mousePressed() function that gets called each time you pressed a mouse button if you only want a single circle and change where the center point where it is drawn to wherever the mouse was the last time you pressed a mouse button.

---

<div class="post-metadata">

**Author:** ![stevenoneilsteven](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@stevenoneilsteven](https://discourse.processing.org/u/stevenoneilsteven)\
**Post date:** [February 20, 2019, 9:06am UTC](https://discourse.processing.org/t/beginners-bad-luck/8542/4 "2019-02-20T09:06:55Z")

</div>

> [@kll](#):
>
> boolean paint = true, start = false, mousePresent = false; void setup () { size(500, 500); noStroke(); if ( paint) background(0); println(“use: key [p] for toggle painting\nkey [s] for start”); } void draw () { if ( !paint ) background(200, 200, 0); if ( mousePresent && focused && start) { if (mousePressed) fill(0, 200, 0); else fill(0, 0, 200); ellipse(mouseX, mouseY, width/8, width/8); } } void keyPressed() { if ( key == ‘p’ ) paint = !paint; if ( key == ‘s’ ) start = true; } public void mouseExited() { mousePresent = false; } public void mouseEntered() { mousePresent = true; }

Thanks very much! The second one works a treat but are all interesting. Thank you for simplifying the code too. Makes sense.

Cheers

Steve
