在处理中点击时改变单元格的填充颜色

时间:2021-10-18 17:58:37

I've managed to print the co-ordinates of a cell when it's clicked.

当单元格被单击时,我成功地打印了它的坐标。

Now I'm looking for help and advice to use the this data in order to change the cell colour to red - unless the cell selected is a base cell, I want those to remain as a constant. Here's the code I'm working with:

现在,我正在寻求帮助和建议,以便使用这些数据,以便将单元格的颜色更改为红色——除非所选的单元格是一个基本单元格,否则我希望这些数据保持不变。下面是我使用的代码:

Tile[][] tileArray = new Tile[10][10];


int rows = 10;
int cols = 10;
int[] base = { 4,4, 4,5, 4,6, 4,7 };

void setup() {
  size(500, 500);

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      tileArray[i][j] = new Tile(i*50, j*50);
    }
  }
}

void draw() {
  for (int i=0; i<base.length; i+=2) {
    fill(0, 0, 255);
    rect(base[i]*50, base[i+1]*50, 50, 50);
  }
}

void mousePressed() {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      println (mouseX/50 +"," + mouseY/50);
    }
  }
}

class Tile {
  Tile(int x, int y) {
    fill(255);
    stroke(0);
    rect(x, y, 50, 50);
  }
}

EDIT: I've added the functionality which I am looking for, but I assume that I have not stored my base cell data correctly. As you can still click on the base cell and an active cell is drawing underneath. Here is the new code:

编辑:我添加了我正在寻找的功能,但是我假设我没有正确地存储我的基本单元格数据。因为您仍然可以单击基单元格,而一个活动单元格正在下面绘图。下面是新的代码:

Tile[][] tiles;
int gridSize = 10;
int tileSize = 50;


void setup() {
  size (450, 400);
  generateGrid();
}

void draw() {
  background (255);
  display();
}

public void generateGrid() {
  tiles = new Tile[gridSize][gridSize];
  for (int i = 0; i < gridSize; i++) {
    for (int j = 0; j < gridSize; j++) {
      tiles[i][j] = new Tile((i*50), (j*50), tileSize, tileSize);
    }
  }
}


public void display() {
  for (int i = 0; i < gridSize; i++) {
    for (int j = 0; j < gridSize; j++) {
      tiles[i][j].display();
    }
  }
}

void mousePressed() {
  for (int i = 0; i < gridSize; i++) {
    for (int j = 0; j < gridSize; j++) {
      println (mouseX/50 +"," + mouseY/50);
    }
  }

  int mx = mouseX/50;
  int my = mouseY/50;
  tiles[mx][my].active = !tiles[mx][my].active;
  //      tiles[mx][my].base = !tiles[mx][my].base;      
  println(tiles[mx][my].active);
}

class Tile {
  int tx, ty, tw, th;
  int[] baseCell = { 4, 2, 4, 3, 4, 4, 4, 5 };

  color col_default = color(255);
  color col_base = color(0, 0, 255);
  color col_active  = color(255, 100, 50);

  boolean active = false;
  boolean base = false;


  Tile (int itx, int ity, int itw, int ith) {
    tx = itx;
    ty = ity;
    tw = itw;
    th = ith;
  }

  void display() {  
    stroke(0);   
    fill(col_default);

    if ( active )      fill (col_active); 

    rect(tx, ty, tw, th);

    for (int i=0; i<baseCell.length; i+=2) {
      fill(col_base);
      rect(baseCell[i]*50, baseCell[i+1]*50, 50, 50);
    }
  }
}

1 个解决方案

#1


0  

Your Tile class doesn't really make sense right now. It doesn't really make sense to draw to the screen from a constructor. Instead, move that logic into a draw() function inside the Tile class.

你的瓷砖类现在没有意义。从构造函数中绘制到屏幕上并没有什么意义。相反,将该逻辑移动到Tile类中的draw()函数中。

Your Tile class also has to keep track of its state: whether it's a base cell or not, or any other kind of cell it can be. You might do this with an enum, or an int, or a set of booleans. That part is completely up to you.

您的Tile类还必须跟踪它的状态:无论它是基单元还是其他类型的单元。您可以使用enum、int或一组布尔值。那部分完全由你决定。

Then in your Tile.draw() function, you should check that state to decide what color to draw. From the sketch's draw() function, simply loop over each Tile and tell it to draw itself.

然后在您的Tile.draw()函数中,您应该检查该状态,以确定要绘制什么颜色。从示意图的draw()函数中,只需对每个块进行循环,并告诉它自己绘制。

Finally, when you click, simply set the state of the Tile that you clicked on.

最后,当您单击时,只需设置单击的Tile的状态。

#1


0  

Your Tile class doesn't really make sense right now. It doesn't really make sense to draw to the screen from a constructor. Instead, move that logic into a draw() function inside the Tile class.

你的瓷砖类现在没有意义。从构造函数中绘制到屏幕上并没有什么意义。相反,将该逻辑移动到Tile类中的draw()函数中。

Your Tile class also has to keep track of its state: whether it's a base cell or not, or any other kind of cell it can be. You might do this with an enum, or an int, or a set of booleans. That part is completely up to you.

您的Tile类还必须跟踪它的状态:无论它是基单元还是其他类型的单元。您可以使用enum、int或一组布尔值。那部分完全由你决定。

Then in your Tile.draw() function, you should check that state to decide what color to draw. From the sketch's draw() function, simply loop over each Tile and tell it to draw itself.

然后在您的Tile.draw()函数中,您应该检查该状态,以确定要绘制什么颜色。从示意图的draw()函数中,只需对每个块进行循环,并告诉它自己绘制。

Finally, when you click, simply set the state of the Tile that you clicked on.

最后,当您单击时,只需设置单击的Tile的状态。