将项目上的鼠标单击存储到数组中

时间:2019-05-01 02:50:27

标签: java arrays swing jpanel mouselistener

我有一个项目,用户单击5x5网格尝试查找宝藏,但是我想知道如何实现一个代码,该代码将用户单击的位置存储到数组中并保存。这样就可以进行多次尝试,并且不会让用户两次选择相同的位置。

row = getClickRow(panel);
col = getClickColumn(panel);

while ((row < 0 || col < 0) && !(pRow == row && pCol == row))  // Out of bounds error check
{
    row = getClickRow(panel);  // asks user to click once again
    col = getClickColumn(panel);
}

if (pRow != row || pCol != col) {
    pRow = row;
    pCol = col;

    ret = verifyPos(treasureX, treasureY, row, col);

    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);
    }
}

0 个答案:

没有答案
相关问题