我有这个代码来创建一个网格,并在鼠标放在网格上时填充网格框:
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);
}
}
}
我想为每个盒子分配一个像A2,B7等的iD,然后在控制台中打印出鼠标所在盒子的iD。
有人可以帮我这么做吗?我不知道如何定义一个精确的区域并给它一个ID
答案 0 :(得分:1)
使用ASCII将整数转换为char(表格为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]);
答案 1 :(得分:0)
您已在i
和j
中拥有鼠标的方框坐标。只需使用以下内容将这些转换为ID即可:
String id = Character.toString((char)('A' + i)) + (1 + j);
您还可以使用数组查找ID的第一部分。