Added dynamic color swap button text
This commit is contained in:
parent
b3710a878f
commit
91962c01e0
@ -8,6 +8,7 @@ import javax.swing.JButton;
|
|||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.game.Game;
|
import dev.kske.chess.game.Game;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,6 +22,7 @@ public class MainWindow {
|
|||||||
private JFrame mframe;
|
private JFrame mframe;
|
||||||
private BoardPane boardPane;
|
private BoardPane boardPane;
|
||||||
private Game game;
|
private Game game;
|
||||||
|
private Color activeColor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launch the application.
|
* Launch the application.
|
||||||
@ -66,10 +68,19 @@ public class MainWindow {
|
|||||||
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
|
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
|
||||||
|
|
||||||
JButton btnRestart = new JButton("Restart");
|
JButton btnRestart = new JButton("Restart");
|
||||||
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
btnRestart.addActionListener((evt) -> {
|
||||||
|
if (game != null)
|
||||||
|
game.reset();
|
||||||
|
game.start();
|
||||||
|
});
|
||||||
|
|
||||||
JButton btnSwapColors = new JButton("Swap Colors");
|
activeColor = Color.WHITE;
|
||||||
btnSwapColors.addActionListener((evt) -> game.swapColors());
|
JButton btnSwapColors = new JButton("Play as black");
|
||||||
|
btnSwapColors.addActionListener((evt) -> {
|
||||||
|
game.swapColors();
|
||||||
|
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
|
||||||
|
activeColor = activeColor.opposite();
|
||||||
|
});
|
||||||
|
|
||||||
toolPanel.add(btnRestart);
|
toolPanel.add(btnRestart);
|
||||||
toolPanel.add(btnSwapColors);
|
toolPanel.add(btnSwapColors);
|
||||||
@ -78,12 +89,17 @@ public class MainWindow {
|
|||||||
mframe.setLocationRelativeTo(null);
|
mframe.setLocationRelativeTo(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BoardPane getBoardPane() { return boardPane; }
|
public BoardPane getBoardPane() {
|
||||||
|
return boardPane;
|
||||||
|
}
|
||||||
|
|
||||||
public Game getGame() { return game; }
|
public Game getGame() {
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
public void setGame(Game game) {
|
public void setGame(Game game) {
|
||||||
if (this.game != null) this.game.disconnect();
|
if (this.game != null)
|
||||||
|
this.game.disconnect();
|
||||||
this.game = game;
|
this.game = game;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user