0

So basically, I want to save all the coordinates of all row numbers that are processed in the actionPerformed method in the ButtonListener class to my Board class in order to move pieces for chess. When I run the program, the inner class ButtonListener saves all the variables which is good. But the variables do not keep the saved values when they are called to my constructor in class Board. I want to keep the values in the variables from the inner class to the outer class.

Edit:Provided the entire class.

public class Board extends JPanel {
Piece movingPiece;
ImageIcon image;
private int row1;
private int col1;
private int row2;
private int col2;
private JButton btn[][];
private Square squares[][];


public Board(String gameType, int row, int col) {
    setLayout(new GridLayout(8, 8));
    squares = new Square[row][col];
    btn = new JButton[row][col];
    setUpSquares();
    setUpBtns();
    setUpChessPieces();
    findPieces();
    movePiece(row1, col1, row2, col2);// all 0's

}

public void setUpSquares() {
    for (int i = 0; i < ChessGame.EIGHT; i++) {
        for (int j = 0; j < ChessGame.EIGHT; j++) {
            squares[i][j] = new Square();
        }
    }
}

public void setUpBtns() {
    for (int i = 0; i < ChessGame.EIGHT; i++) {
        for (int j = 0; j < ChessGame.EIGHT; j++) {
            btn[i][j] = new JButton();
            add(btn[i][j]);
            btn[i][j].addActionListener(new ButtonListener());
            if ((i + j) % 2 == 0) {
                btn[i][j].setBackground(Color.WHITE);
                btn[i][j].setForeground(Color.WHITE);
            } else {
                btn[i][j].setBackground(Color.DARK_GRAY);
                btn[i][j].setForeground(Color.DARK_GRAY);
            }
        }
    }
}

public void movePiece(int row1, int col1, int row2, int col2) {
    squares[row1][col1].getPiece().setRow(row2);
    squares[row1][col1].getPiece().setCol(col2);
    movingPiece = squares[row1][col1].getPiece();
    squares[row2][col2].setPiece(movingPiece);
    btn[row2][col2].setIcon(new ImageIcon(squares[row2][col2].getPiece().getPieceColor()));
}

/**
 * Finds pieces and sets the piece icons to the button.
 */
public void findPieces() {
    for (int i = 0; i < ChessGame.EIGHT; i++) {
        for (int j = 0; j < ChessGame.EIGHT; j++) {
            if (squares[i][j].getPiece() != null) {
                btn[i][j].setIcon(new ImageIcon(squares[i][j].getPiece().getPieceColor()));
            } else {
                btn[i][j].setIcon(null);
            }

        }
    }
}

public void setUpChessPieces() {
    // white pieces
    for (int i = 0; i < ChessGame.EIGHT; i++) {
        Pawn pawn1 = new Pawn(1, i, 1, "white");
        squares[1][i].setPiece(pawn1);
    }

    for (int i = 0; i < ChessGame.EIGHT; i += 7) {
        Rook rook1 = new Rook(0, i, 1, "white");
        squares[0][i].setPiece(rook1);
    }

    for (int i = 1; i < ChessGame.EIGHT; i += 5) {
        Knight knight1 = new Knight(0, i, 1, "white");
        squares[0][i].setPiece(knight1);
    }

    for (int i = 2; i < ChessGame.EIGHT; i += 3) {
        Bishop bishop1 = new Bishop(0, i, 1, "white");
        squares[0][i].setPiece(bishop1);
    }

    King king1 = new King(0, 4, 1, "white");
    squares[0][4].setPiece(king1);

    Queen queen1 = new Queen(0, 3, 1, "white");
    squares[0][3].setPiece(queen1);

    // black pieces
    for (int i = 0; i < ChessGame.EIGHT; i++) {
        Pawn pawn2 = new Pawn(6, i, 2, "black");
        squares[6][i].setPiece(pawn2);
    }

    for (int i = 0; i < ChessGame.EIGHT; i += 7) {
        Rook rook2 = new Rook(7, i, 1, "black");
        squares[7][i].setPiece(rook2);
    }

    for (int i = 1; i < ChessGame.EIGHT; i += 5) {
        Knight knight2 = new Knight(7, i, 1, "black");
        squares[7][i].setPiece(knight2);
    }

    for (int i = 2; i < ChessGame.EIGHT; i += 3) {
        Bishop bishop2 = new Bishop(7, i, 1, "black");
        squares[7][i].setPiece(bishop2);
    }

    King king2 = new King(7, 4, 1, "black");
    squares[7][4].setPiece(king2);

    Queen queen2 = new Queen(7, 3, 1, "black");
    squares[7][3].setPiece(queen2);

}

public class ButtonListener implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        JButton src = (JButton) e.getSource();

        if (src.getBackground() != Color.DARK_GRAY && src.getBackground() != Color.WHITE) {
            src.setBackground(src.getForeground());
        } else {
            src.setBackground(Color.MAGENTA);
        }

        for (int i = 0; i < ChessGame.EIGHT; i++) {
            for (int j = 0; j < ChessGame.EIGHT; j++) {
                if (squares[i][j].getPiece() != null) {
                    if (btn[i][j] == src) {
                        row1 = i;
                        col1 = j;
                    }
                }
                if (squares[i][j].getPiece() == null) {
                    if (btn[i][j] == src) {
                        row2 = i;
                        col2 = j;
                    }
                }

            }
        }
        System.out.println("row1:" + row1 + " col1:" + col1);
        System.out.println("row2:" + row2 + " col2:" + col2);

    }

}

}

Elijah.S
  • 47
  • 6
  • 1
    For better help sooner post a valid [mcve] that demonstrates your issue. Not code snippets / whole code, as code-formatted text (as you did) – Frakcool Mar 08 '17 at 00:47
  • 2
    Try MVC (or any modern version of it). It stands for Model-View-Controller. The model has all the game related data like coordinates of figures etc., so you can just query it. – mike Mar 08 '17 at 00:47
  • 1) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 2) See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Mar 08 '17 at 04:19

1 Answers1

0

Your problem is that you are calling the movePiece(int, int, int, int) from the constructor, and 2 buttons have not necessarily been clicked. The listener has been added to each button, but you don't know that the listener has been activated at that point. So, row1, col1, row2, and col2 are all set to the value that ints take when they have not been initiated yet, 0. So, you really want to call the move method from the listener. Something like:

btn[i][j].addActionListener(e -> {
    JButton src = (JButton) e.getSource();
    //Process src to get row1, col1, row2, and col2 as you did
    movePiece(row1, col1, row2, col2);
});

I replaced your ButtonListener class with a lambda expression, but you can leave it as you did. The important part is that you are calling movePiece from the action listener because it ensures that row1, col1, row2, and col2 are all defined.

acn3
  • 338
  • 3
  • 16