获取 Processing 3 上点击点的坐标

get coordinate of clicked points on Processing 3

我想做的只是改变我点击的像素的颜色。所以有两种状态;

点击鼠标

没有点击鼠标

我正在使用下面的代码;

boolean myBol = false ;
void setup(){
  size(860,640);
  background(0);
}
void draw(){
  if (mousePressed) {

   if(myBol == true){myBol = false;} else {myBol = true;}

  } 

  if (myBol == true){
    stroke(255);
  point(mouseX,mouseY);
}}

当我 运行 第一次单击此代码时,它会一直在我移动鼠标的任何地方绘制,但我想要的是; "draw everytime I click on screen"。

您的检查条件不正确。

if (mousePressed) {
   if(myBol == true){
       myBol = false;
   } else {
       myBol = true;
   }
} 

或者基本上

if (mousePressed)
    myBol = !myBol

每次点击都会更改 myBol 值。

既然是点击绘制,就不需要这个值了。直接用mousePressed值就可以了

if (mousePressed) {
    stroke(255);
    point(mouseX,mouseY);
}

你有多余的逻辑,但没有多大意义。既然已经有了 mousePressed 变量,为什么还需要 myBol?你能不能把你的逻辑简化成这样:

void setup() {
  size(860, 640);
  background(0);
}

void draw() {
  if (mousePressed) {
    stroke(255);
    point(mouseX, mouseY);
  }
}

或者您可以使用鼠标事件函数,如下所示:

void setup() {
  size(860, 640);
  background(0);
}

void mousePressed() {
  stroke(255);
  point(mouseX, mouseY);
}

void mouseDragged() {
  stroke(255);
  point(mouseX, mouseY);
}

void draw() {
}

此外,boolean 值可以直接与运算符一起使用,因此:

boolean example;
if(example == true){example= false;} else {example= true;}

可以使用 not 运算符缩短为:

boolean example;
example = !example;

还有这个:

if(example == true)

可以缩短为:

if(example)