From 0e75aae42134bc927fd6273312c6203f8fccc033 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 23 Jul 2019 09:59:22 +0200 Subject: [PATCH] Added dynamic color swap button text --- src/dev/kske/chess/ui/MainWindow.java | 40 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index 351ef45..ea84de0 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -8,6 +8,7 @@ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; +import dev.kske.chess.board.Piece.Color; import dev.kske.chess.game.Game; /** @@ -18,9 +19,10 @@ import dev.kske.chess.game.Game; */ public class MainWindow { - private JFrame mframe; - private BoardPane boardPane; - private Game game; + private JFrame mframe; + private BoardPane boardPane; + private Game game; + private Color activeColor; /** * Launch the application. @@ -66,24 +68,38 @@ public class MainWindow { mframe.getContentPane().add(toolPanel, BorderLayout.NORTH); JButton btnRestart = new JButton("Restart"); - btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); - - JButton btnSwapColors = new JButton("Swap Colors"); - btnSwapColors.addActionListener((evt) -> game.swapColors()); - + btnRestart.addActionListener((evt) -> { + if (game != null) + game.reset(); + game.start(); + }); + + activeColor = Color.WHITE; + 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(btnSwapColors); - + mframe.pack(); 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) { - if (this.game != null) this.game.disconnect(); + if (this.game != null) + this.game.disconnect(); this.game = game; } }