每当点击方块时,我都会尝试从黑到白,从白到黑的方形更改颜色。我使用mouseClicked函数来执行此操作,但问题是mouseClicked未正确单击。当我点击一个方格的底部时,按下它下方的方形。
这是我的mouseClicked代码:
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
System.out.println(mouseX + ", " + mouseY + ", " + (screenW / squares));
colorFlip();
}
});
我的colorFlip代码:
private void colorFlip() {
for (int i = 0; i < cells.size(); i++) {
Cell cell = cells.get(i);
int x = cell.getX();
int y = cell.getY();
int side = cell.getSide();
if (mouseX >= x && mouseX < x + side && mouseY >= y && mouseY < y + side) {
if (cell.getR() == 0) {
cell.setRGB(255, 255, 255);
} else if (cell.getR() == 255) {
cell.setRGB(0, 0, 0);
}
}
}
mouseX = -1;
mouseY = -1;
}
我的单元格初始化代码:
Cell(double xVal, double yVal, double sideVal) {
x = xVal;
y = yVal;
side = sideVal;
}
谢谢, Pranav