4

I am developing chess game in Java using the Netbeans IDE. Instead of rotating the whole board at each move, I thought of changing position of the pieces themselves, while the board remains same. All my pieces and squares are JLabels and are in a JLayeredPane, with pieces being in top layer. All the squares are part of a JLabel array named labels and same for white and black pieces.

WinitialLocation and BinitialLocation are Point Arrays for initial positions of white and black pieces.

But running the following code in mouseReleased() event of the pieces, only the piece which we moved changes place, the rest remain at their position.

Can anybody help me, Please?

void reLocate() {
     for(int i = 0; i < 6; i++ )
     {
        for(int j=0, n=labels.length; j<n;j++)
        {
            if(labels[j].getLocation().equals(WinitialLocation[i]))
               wPieces[i].setLocation(labels[63-j].getLocation());

            if(labels[j].getLocation().equals(BinitialLocation[i]))
               bPieces[i].setLocation(labels[63-j].getLocation());
        }

        layer.moveToFront(wPieces[i]);
        layer.moveToFront(bPieces[i]);
    }
} 

0 Answers0