I've got a chess board (JPanel) with pieces (JLabel) on it that are dragged around thanks to a JLayeredPane. Problem is that I can drag the pieces outside of the chess board, and I'd like to avoid that.
This is my mouseDragged(MouseEvent me) method:
board_mml = new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent me) {
if (pieceMoving == null) {
return ;
}
pieceMoving.setLocation(me.getX() + offsetX, me.getY() + offsetY);
}
I've tried to add controls like this:
if (pieceMoving.getParent().contains(me.getPoint())) {
pieceMoving.setLocation(me.getX() + offsetX, me.getY() + offsetY);
}
and like this:
if (pieceMoving.getParent().contains(pieceMoving.getLocation())) {
pieceMoving.setLocation(me.getX() + offsetX, me.getY() + offsetY);
}
but neither It's working.