Java 按下鼠标后,如何保持填充中的颜色不变?

Java 按下鼠标后,如何保持填充中的颜色不变?,java,processing,fill,rect,Java,Processing,Fill,Rect,单击(即更改时)然后松开并保持悬停后,保持fill()不变的最简单方法是什么 在这个项目中,我只是做了一个网格。当鼠标悬停在特定的矩形上时(在x,y),它会根据所处的状态更改颜色fill(50)是默认值,fill(75)是鼠标悬停时的值,fill(100)是鼠标单击时的值。但在这里,当鼠标松开时,它将返回悬停填充,直到鼠标离开矩形。谢谢 int cols, rows; int scl = 20; void setup() { size(400, 400); int w = 400; int h

单击(即更改时)然后松开并保持悬停后,保持
fill()
不变的最简单方法是什么

在这个项目中,我只是做了一个网格。当鼠标悬停在特定的矩形上时(在
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);
    }
  }
}
int列、行;
int scl=20;
无效设置(){
尺寸(400400);
int w=400;
int h=400;
cols=w/scl;
rows=h/scl;
}
作废提款(){
背景(255);
对于(int x=0;x如果((mouseX>=xpos&&mouseX=ypos&&mouseY堆栈溢出实际上不是为一般的“我该怎么做”类型问题而设计的。它是为特定的“我尝试了X,预期了Y,但得到了Z”类型问题而设计的。但我将尝试在一般意义上提供帮助:

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


您可以使用2D数组来实现这一点,数组中的每个单元格表示网格中的一个单元格。您可以直接存储单元格的状态或颜色。

正如Kevin所说,您应该将应用程序的状态保存在矩阵中

boolean[][] matrix = new boolean[21][21];
当你点击一个
单元格时,切换它

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);
        }
    }
}
void draw(){
背景(255);
对于(int x=0;x如果((mouseX>=xpos&&mouseX=ypos&&mouseY)您可以修复代码的格式吗?您有两个
setup()
函数在其中。另外,请您更具体地说明您正在尝试执行的操作,以及哪一行代码的行为与您预期的不同?很抱歉,我没有看到这一点!如果您现在运行代码,您会注意到,当您将鼠标悬停在矩形上时,它会改变颜色,当您再次单击它时,它会改变颜色。fill co鼠标按下后使用的lor是我想用来保持点击方块的点击颜色的lor。我会尝试类似的方法,通过处理可以在控制台中看到打印的数组,但它总是显示类似于
[[I@4e59b084编辑:我还不知道如何为这个2D数组建立一个骨架?@ ICY4614,因为默认行为是打印数组的哈希代码。你可以考虑编写自己的函数,打印出数组中的值,或者在java API中查找函数。可能会有帮助。哇!这正是我需要的。谢谢!