Beginner's Bad Luck

Hello,

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

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

1 Like

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:

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.


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

no, looks like you even need a start state:

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.

1 Like

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.

1 Like

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

Cheers

Steve