Disruptr
Disruptr

Reputation: 55

Processing Draw Program

I was making a basic paint/draw program in processing where the Left click would draw a point and a Right click would draw a background colored rectangle to "erase". The thing is that when I start erasing it wont let me go back to drawing. Also i would like to make the dots draw faster so it looks more like a line rather than a dotted line. Thanks! Here is the code:

void setup() {
  size(600, 600);
  background(#C4C4C4);
}

void draw() {
  frameRate(60);
  if (mouseButton == LEFT) {
    fill(#030303);
    point(mouseX, mouseY);
  }

  else if(mouseButton == RIGHT){
    fill(#C4C4C4);
    noStroke();
    rect(mouseX-15, mouseY-15, 30, 30);
  }
}

Upvotes: 0

Views: 789

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

Your main problem is that you call noStroke() when the user presses the right button, but you never set the stroke back when the user presses the left button. The point() function uses the stroke color, not the fill color, so you have to reset it. This works:

void setup() {
  size(400, 400);
  background(#C4C4C4);
}

void draw() {

  if (mouseButton == LEFT) {
    stroke(#030303);
    point(mouseX, mouseY);
  }

  else if(mouseButton == RIGHT){
    fill(#C4C4C4);
    noStroke();
    rect(mouseX-15, mouseY-15, 30, 30);
  }
}

As for drawing lines instead of a point, you can use the pmouseX and pmouseY variables (which hold the previous position of the mouse) to draw a line. Specifically, change this:

point(mouseX, mouseY);

To this:

line(pmouseX, pmouseY, mouseX, mouseY);

Upvotes: 0

Related Questions