기본적으로 체스 조각을 이동하기 위해 기본적으로 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);
}
}
}
더 나은 도움을 받으려면 문제를 설명하는 유효한 [mcve]를 게시하십시오. 코드 스 니펫/전체 코드가 아닌 코드 형식의 텍스트 – Frakcool
MVC (또는 최신 버전)를 사용해보십시오. Model-View-Controller의 약자입니다. 모델에는 그림 좌표 등의 모든 게임 관련 데이터가 있으므로 쿼리 할 수 있습니다. – mike
1) 더 이상 고칠 수없는 문제에 대해서는 [코드 블록의 걸쇠 닫기]에 대한 [감지/수정] (http://meta.stackexchange.com/q/251795/155831)을 참조하십시오. 2) [강력하고 크기 조정이 가능한 스윙 체스 GUI 만들기] (http://stackoverflow.com/q/21142686/418556)를 참조하십시오. –