Greetings.
In my chess program my chessboard is a JPanel with a mouseListener on it. I have implemented drag and drop of the pieces as follows…
- mousePressed to pick up a piece.
- mouseDragged to move a piece, with appropriate calls to repaint()
- mouseReleased to place a piece on a new square.
- mouseExited to prevent a piece from being dragged off the board.
Everything works fine, but I am not totally satisfied with item 4.
As it works now, as soon as the mouse reaches the edge of the board, if a piece is being dragged it is automatically placed back on its original square, without the mouseButton being released. Also the repaint doesn’t remove the piece being moved from where it left the board, until a subsequent repaint, but that is a small thing that I am not concerned with right now.
Anyways, I have seen programs where you can pick up a piece and drag it off the board, and if you release the mouse button while the pointer is off the board, then the piece will return to the original square. I can’t figure out how to do that, as how can the application generate a mouseReleased event if the mouse is not over the application for which the mouseListener is defined? See What I mean?
Thanks.