Tuesday, October 6, 2009

Drag and Drop of chess pieces.

Last time, in the alpha2 phase, we added the ability to create positions on the board by interpreting a fen string pasted by the user. In the alpha3 phase we add the ability to move the pieces using drag and drop. To do this we added variables pieceMoved, xDrag, yDrag, xOffset, yOffset, and a boolean isDrag. We also added bodies to three methods of the table class, as follows.

mousePressed( MouseEvent e ) - When you press on a piece, you pick it up. The position array index is calculated from the xy co-ordinates of the mouse event, the corresponding posn array element is cleared, and the xOffset and yOffset are calculated. These offsets represent the distance of the mouse click from the top left corner of the square being clicked. more on that later. Another key variable is isDrag, which flags the paintComponent method that a piece is beng moved.

mouseDragged( MouseEvent e ) - As the piece is moved, a MouseEvent is generated, the new position of the piece is claculated, and repaint is called to paint the piece in the new position.

mouseReleased( MouseEvent e ) - The piece is dropped on a new square. The appropriate index of the position array is calculated from the xy of the MouseEvent, the array is updated, and paintComponent is called. Later on, the mouseReleased event will be a trigger for all sorts of actions to update game state variabled, and ultimetely to tell the engine that itis it's turn to move. But we're not there yet. All in due time.

We will take a close look at a few of the variables that come into play here...

isDrag - Normally, the paintComponent method of the Board class gets information from the position array to determine where to draw the pieces. But when a piece is being dragged, it is not in the position array. The boolean isDrag tells the paintComponent method that there is a piece not in the array that needs to be painted.

xOffset, yOffset. When a piece is drawn, what is actually being drawn is a square BufferedImage with a transparent background. xOffset and yOffset are constants from the original mousePressed event that started the piece move. They are used to calulate xDrag & yDrag from the xy co-ordinated of the MouseDragged event. xDrag and yDrag become the new top left corner of the BufferedImage, this is what is passed to the graphics drawImage command.

One more thing. At the moment, the entire board and position is redrawn with each incremental movement of a piece being dragged. This is easy to implement, and does not seem to cause any resurce usage problems. At a later date we will look at a more efficient repaint which redrawns only the affected areas of the board.

OK that's it for now. Currently there are no restrictions on how the pieces can be moved. That will entail co-ordinated effort in a number of areas, including internal representation of a move, of the game state, which is more than just the position of the pieces, and also move legality according to the rules of chess. The next few posts will document the effort towards that goal.

As always, the updated alpha3 source code is on the website.


No comments:

Post a Comment