处理填充问题的清单
Checklist in processing fill problems
所以我有这个清单代码正在处理中,我希望能够单击列表中的一个按钮,并让该按钮自己填充黑色,但每次都是。我让那个部分为单个按钮工作,但每当我在 class 中实现更多按钮并单击一个按钮时,它就会填充每个按钮。我试过使用 pushMatrix/popMatrix 和 pushStyle/popStyle,但每次我这样做时,按钮在我单击时都不会改变颜色。如果有人能告诉我该怎么做,那将不胜感激。
这是按钮 class:
class Button {
// Other optional attributes could be r, g, and b values for examples
int xLoc;
int yLoc;
int wide;
int tall;
int fill;
boolean checked;
Button(int x, int y, int w, int h, int f) {
xLoc = x;
yLoc = y;
wide = w;
tall = h;
fill = f;
}
void display() {
//fill with blue green and create the rect
fill(fill);
if (mousePressed) {
if (button1.isClicked() == true) {
fill = 0;
}
}
ellipse(xLoc, yLoc, wide, tall);
}
// returns true if the mouse is over the button and false otherwise
boolean isClicked() {
if (mouseX > xLoc && mouseX < xLoc + wide) {
if (mouseY > yLoc && mouseY < yLoc + tall) {
return true;
}
}
return false;
}
boolean isChecked() {
if (fill == 0) {
return true;
} else {
return false;
}
}
}
这是主要代码:
Button button1 = new Button(50, 50, 20, 20, 255);
Button button2 = new Button(50, 75, 20, 20, 255);
Button button3 = new Button(50, 100, 20, 20, 255);
Button button4 = new Button(50, 125, 20, 20, 255);
void setup() {
size(500, 500);
}
void draw() {
background(50);
button1.display();
button2.display();
button3.display();
button4.display();
}
pushMatrix
和 popMatrix()
函数只处理平移和旋转。他们不处理填充颜色。
看看你 Button
class 中的这一行:
if (button1.isClicked() == true) {
每个按钮仅在单击 button1
时才进行检查。您可能希望检查当前按钮是否被单击:
if (isClicked() == true) {
可以缩短为:
if (button1.isClicked()) {
所以我有这个清单代码正在处理中,我希望能够单击列表中的一个按钮,并让该按钮自己填充黑色,但每次都是。我让那个部分为单个按钮工作,但每当我在 class 中实现更多按钮并单击一个按钮时,它就会填充每个按钮。我试过使用 pushMatrix/popMatrix 和 pushStyle/popStyle,但每次我这样做时,按钮在我单击时都不会改变颜色。如果有人能告诉我该怎么做,那将不胜感激。
这是按钮 class:
class Button {
// Other optional attributes could be r, g, and b values for examples
int xLoc;
int yLoc;
int wide;
int tall;
int fill;
boolean checked;
Button(int x, int y, int w, int h, int f) {
xLoc = x;
yLoc = y;
wide = w;
tall = h;
fill = f;
}
void display() {
//fill with blue green and create the rect
fill(fill);
if (mousePressed) {
if (button1.isClicked() == true) {
fill = 0;
}
}
ellipse(xLoc, yLoc, wide, tall);
}
// returns true if the mouse is over the button and false otherwise
boolean isClicked() {
if (mouseX > xLoc && mouseX < xLoc + wide) {
if (mouseY > yLoc && mouseY < yLoc + tall) {
return true;
}
}
return false;
}
boolean isChecked() {
if (fill == 0) {
return true;
} else {
return false;
}
}
}
这是主要代码:
Button button1 = new Button(50, 50, 20, 20, 255);
Button button2 = new Button(50, 75, 20, 20, 255);
Button button3 = new Button(50, 100, 20, 20, 255);
Button button4 = new Button(50, 125, 20, 20, 255);
void setup() {
size(500, 500);
}
void draw() {
background(50);
button1.display();
button2.display();
button3.display();
button4.display();
}
pushMatrix
和 popMatrix()
函数只处理平移和旋转。他们不处理填充颜色。
看看你 Button
class 中的这一行:
if (button1.isClicked() == true) {
每个按钮仅在单击 button1
时才进行检查。您可能希望检查当前按钮是否被单击:
if (isClicked() == true) {
可以缩短为:
if (button1.isClicked()) {