如何为网格的每个框分配一个 iD
How to assign each box of a grid an iD
我有这段代码可以创建网格并在鼠标位于网格上时填充网格框:
int cols = 10, rows = 10;
boolean[][] states = new boolean[cols][rows];
int videoScale = 50;
void setup(){
size(500,500);
}
void draw(){
// Begin loop for columns
for (int i = 0; i < cols; i++) {
// Begin loop for rows
for (int j = 0; j < rows; j++) {
// Scaling up to draw a rectangle at (x,y)
int x = i*videoScale;
int y = j*videoScale;
fill(255);
stroke(0);
//check if coordinates are within a box (these are mouse x,y but could be fiducial x,y)
//simply look for bounds (left,right,top,bottom)
if( (mouseX >= x && mouseX <= x + videoScale) && //check horzontal
(mouseY >= y && mouseY <= y + videoScale)){
//coordinates are within a box, do something about it
fill(0);
stroke(255);
//you can keep track of the boxes states (contains x,y or not)
states[i][j] = true;
if(mousePressed) println(i+"/"+j);
}else{
states[i][j] = false;
}
rect(x,y,videoScale,videoScale);
}
}
}
我想为每个盒子分配一个 ID,例如 A2、B7 等,然后在控制台中打印鼠标所在盒子的 ID。
有人可以帮我做这个吗?我不知道如何定义一个精确的区域并给它一个 ID
您在i
和j
中已经有了鼠标的方框坐标。只需使用类似以下内容将它们转换为 ID:
String id = Character.toString((char)('A' + i)) + (1 + j);
您还可以对 ID 的第一部分使用数组查找。
使用 ASCII 将整数转换为字符(table 此处:http://www.asciitable.com/)。
String[][] coordinates = new String[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase();
}
}
鼠标悬停时:
System.out.println(coordinates[i][j]);
我有这段代码可以创建网格并在鼠标位于网格上时填充网格框:
int cols = 10, rows = 10;
boolean[][] states = new boolean[cols][rows];
int videoScale = 50;
void setup(){
size(500,500);
}
void draw(){
// Begin loop for columns
for (int i = 0; i < cols; i++) {
// Begin loop for rows
for (int j = 0; j < rows; j++) {
// Scaling up to draw a rectangle at (x,y)
int x = i*videoScale;
int y = j*videoScale;
fill(255);
stroke(0);
//check if coordinates are within a box (these are mouse x,y but could be fiducial x,y)
//simply look for bounds (left,right,top,bottom)
if( (mouseX >= x && mouseX <= x + videoScale) && //check horzontal
(mouseY >= y && mouseY <= y + videoScale)){
//coordinates are within a box, do something about it
fill(0);
stroke(255);
//you can keep track of the boxes states (contains x,y or not)
states[i][j] = true;
if(mousePressed) println(i+"/"+j);
}else{
states[i][j] = false;
}
rect(x,y,videoScale,videoScale);
}
}
}
我想为每个盒子分配一个 ID,例如 A2、B7 等,然后在控制台中打印鼠标所在盒子的 ID。
有人可以帮我做这个吗?我不知道如何定义一个精确的区域并给它一个 ID
您在i
和j
中已经有了鼠标的方框坐标。只需使用类似以下内容将它们转换为 ID:
String id = Character.toString((char)('A' + i)) + (1 + j);
您还可以对 ID 的第一部分使用数组查找。
使用 ASCII 将整数转换为字符(table 此处:http://www.asciitable.com/)。
String[][] coordinates = new String[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase();
}
}
鼠标悬停时:
System.out.println(coordinates[i][j]);