mousePressed后如何在fill()中保持颜色相同?

时间:2017-09-07 20:08:10

标签: java processing fill rect

点击(当它发生变化时)然后取消点击并留下悬停时,保持var LineKey = (string)element.Attribute("name"); 相同的最简单最简单的方法是什么?

在这个项目中,我只是制作了一个网格。当鼠标悬停在特定的矩形(fill()x)上时,它会根据其所处的状态更改颜色。y是默认值,fill(50)是鼠标悬停,鼠标单击时fill(75)。但是,当鼠标未被点击时,它返回到悬停填充,直到鼠标离开矩形。感谢。

fill(100)

2 个答案:

答案 0 :(得分:1)

Stack Overflow实际上并不是针对一般的“我该怎么做”这类问题而设计的。这是针对具体的“我试过X,期待Y,但得到Z而不是”类型的问题。但我会尝试在一般意义上提供帮助:

您需要将每个单元格的状态存储在数据结构中,然后使用该数据结构绘制场景。

您可以使用2D数组执行此操作,其中数组中的每个单元格代表网格中的单元格。您可以直接存储单元格的状态或颜色。

答案 1 :(得分:1)

正如凯文所说,你应该将你的申请状态保存在一个矩阵中。

boolean[][] matrix = new boolean[21][21];

点击cell时,切换

if(!matrix[xpos/scl][ypos/scl]) {
    matrix[xpos/scl][ypos/scl] = true;
} else {
    matrix[xpos/scl][ypos/scl] = false;
}

在此循环中,检查是否可以绘制当前位置

if(matrix[x][y]) {
    fill(204, 102, 0); // an orange color
    rect(xpos, ypos, scl, scl);
}

因此,您的draw()方法应如下所示

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);
                    if(!matrix[xpos/scl][ypos/scl]) {
                        matrix[xpos/scl][ypos/scl] = true;
                    } else {
                        matrix[xpos/scl][ypos/scl] = false;
                    }
                    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);
            }
            if(matrix[x][y]) {
                fill(204, 102, 0);
                rect(xpos, ypos, scl, scl);
            }
            rect(xpos, ypos, scl, scl);
        }
    }
}