I’m working on a GUI for a chess game and I was wondering if there’s any way to check for a series of clicks, for example: user clicks on jPanel THEN user clicks on another jPanel that exists in valid move array. I know I could use a variable to store some kind of state like “isSquareClicked = true” or something but I’d rather not unless that’s the only way…
Share
I don’t see anything wrong with using JPanels. Here’s my implementation:
First a ChessSquare, this represents one cell on the board:
Now the main board panel:
We keep a JPanel matrix to represent the board, and ChessSquare array to represent the current move. In
isValidMove()we check to see if the current move is complete (both squares have been clicked, thus the move array already has one element). Once a move is complete, we reset the move and start again.