0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
IDKs
  • 3
  • 2
  • 1
    See: https://stackoverflow.com/questions/22343047/asking-for-some-clarification-in-java-about-jlabel-and-parent/22357889#22357889 for one approach. – camickr Mar 22 '18 at 15:29
  • Thank you very much, it did solve my problem however I'm still trying to grasp the solution. – IDKs Mar 22 '18 at 15:45

0 Answers0