什么是最容易和最简单的方法来保持fill()
相同点击后(那是当它改变),然后unclicking,并离开悬停?如何在mousePressed后填充()中的颜色相同?
在这个项目中,我只是做了一个网格。当鼠标悬停在特定矩形(在x
,y
上)时,它会根据所处状态改变颜色。fill(50)
是默认设置,fill(75)
是鼠标悬停的时候,fill(100)
是鼠标单击的时候。但是,当鼠标点击时,它会返回到悬停填充状态,直到鼠标离开矩形。谢谢。
int cols, rows;
int scl = 20;
void setup() {
size(400, 400);
int w = 400;
int h = 400;
cols = w/scl;
rows = h/scl;
}
void draw() {
background(255);
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
int xpos = x*scl;
int ypos = y*scl;
stroke(55);
if((mouseX >= xpos && mouseX <= xpos+scl) &&
(mouseY >= ypos && mouseY <= ypos+scl)){
fill(75);
if (mousePressed == true){
println("Clicked at: " + xpos + " and " + ypos);
fill(100);
//here is the desired location for the fill to remain constant even
//after unclicking and leaving hover
}
println("Mouse at: " + xpos + " and " + ypos);
}else{
fill(50);
}
rect(xpos, ypos, scl, scl);
}
}
}
你能否修改你的代码的格式?你有两个'setup()'函数。你也可以更具体地说明你想要做什么,哪一行代码的行为与你期望的不同? –
对不起,我没有看到!如果现在运行代码,您会注意到当您将鼠标悬停在矩形上时,它会更改颜色,并且当您单击它时会再次更改颜色。 “mousePressed”后面使用的填充颜色是我想用来保持单击的方块其点击的颜色。 – Icy4614