我需要基于它是否是包含“宝藏”的随机选择的盒子之一来更改盒子颜色的代码。我已经编写了代码以随机化宝藏的位置,并且基于此,盒子会更改为正确的颜色,但是它只适用于一次单击,之后其他选定的盒子都不会改变颜色。
我认为只需在代码开头添加一个简单的for循环就可以解决此问题,但是我对此并不走运,并且不确定从何而来。
for(int i = 0; i <= 6; i ++){
row = getClickRow(panel);
col = getClickColumn(panel);
while ((row < 0 || col < 0) && !(pRow == row && pCol == row)) {
row = getClickRow(panel);
col = getClickColumn(panel);
}
if (pRow != row || pCol != col) {
pRow = row;
pCol = col;
}
ret = verifyPos(xPos, yPos, row, col); // should be converted to verify all treasures
if (row > -1 && col > -1) {
if (ret)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillRect(col * 100 + 51, row * 100 + 51, 99, 99);
}
}
我在顶部添加了for循环,希望它可以用于6个选定的框,但是在选择第一个框之后,框不会改变颜色。