2017-03-08 34 views
0

所以基本上,我想保存在ButtonListener類的actionPerformed方法中處理的所有行號的所有座標到我的Board類,以移動棋子。當我運行程序時,內部類ButtonListener保存所有好的變量。但是,當這些變量被調用到類Board的構造函數中時,這些變量不會保留這些保存的值。我想保留從內部類到外部類的變量中的值。記憶從內部到外部類的國際象棋遊戲的座標

編輯:提供整個班級。

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); 

    } 

} 

}

+1

爲了得到更好的幫助,請儘快發佈證明您的問題的有效[mcve]。不是代碼片段/整個代碼,作爲代碼格式的文本(與您一樣) – Frakcool

+2

嘗試MVC(或其任何現代版本)。它代表模型 - 視圖 - 控制器。該模型具有所有遊戲相關的數據,如圖形座標等,因此您可以查詢它。 – mike

+0

1)請參閱[檢測/修復代碼塊的懸掛緊密支架](http://meta.stackexchange.com/q/251795/155831),以解決問題,我不再擔心修復問題。 2)另請參見[製作強大的可調整大小的Swing Chess GUI](http://stackoverflow.com/q/21142686/418556)。 –

回答

0

你的問題是,你正在呼籲從構造函數的movePiece(int, int, int, int),和2個按鈕不一定被點擊。監聽器已添加到每個按鈕,但您不知道該監聽器已在此時被激活。因此,row1,col1,row2和col2都設置爲ints尚未啓動時的值,因此,您真的想從監聽器調用move方法。喜歡的東西:

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); 
}); 

我換成你ButtonListener類lambda表達式,但你可以把它像你一樣。重要的部分是您從動作偵聽器調用movePiece,因爲它確保row1,col1,row2和col2都已定義。