From 5ecbeb767742cb8b258e2ff4254a674a5a6ddfb8 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 1 Sep 2019 12:45:06 +0200 Subject: [PATCH] Added creation of new game tabs - Switched to GridBagLayout in GamePane - New games created in MenuBar are appended to the tabbed pane in MainWindow + LogPanel class for displaying the game log inside a GamePane - Removed LogFrame in favor of LogPanel --- src/dev/kske/chess/ui/GamePane.java | 64 +++++++++++++++---- .../chess/ui/{LogFrame.java => LogPanel.java} | 19 ++---- src/dev/kske/chess/ui/MainWindow.java | 13 +++- src/dev/kske/chess/ui/MenuBar.java | 13 ++-- 4 files changed, 73 insertions(+), 36 deletions(-) rename src/dev/kske/chess/ui/{LogFrame.java => LogPanel.java} (71%) diff --git a/src/dev/kske/chess/ui/GamePane.java b/src/dev/kske/chess/ui/GamePane.java index 2e83c07..3c1101f 100644 --- a/src/dev/kske/chess/ui/GamePane.java +++ b/src/dev/kske/chess/ui/GamePane.java @@ -1,6 +1,7 @@ package dev.kske.chess.ui; -import java.awt.BorderLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; @@ -28,16 +29,20 @@ public class GamePane extends JComponent { private Color activeColor; public GamePane() { - setLayout(new BorderLayout()); - boardPane = new BoardPane(); - add(boardPane, BorderLayout.CENTER); + activeColor = Color.WHITE; + + GridBagLayout gridBagLayout = new GridBagLayout(); + gridBagLayout.columnWidths = new int[] { 450, 1, 0 }; + gridBagLayout.rowHeights = new int[] { 33, 267, 1, 0 }; + gridBagLayout.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE }; + gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE }; + setLayout(gridBagLayout); JPanel toolPanel = new JPanel(); btnRestart = new JButton("Restart"); btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); - activeColor = Color.WHITE; - btnSwapColors = new JButton("Play as black"); + btnSwapColors = new JButton("Play as black"); btnSwapColors.addActionListener((evt) -> { game.swapColors(); btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase()); @@ -46,26 +51,57 @@ public class GamePane extends JComponent { toolPanel.add(btnRestart); toolPanel.add(btnSwapColors); - add(toolPanel, BorderLayout.NORTH); - JPanel letterPanel = new JPanel(new GridLayout(1, 8)); + GridBagConstraints gbc_toolPanel = new GridBagConstraints(); + gbc_toolPanel.anchor = GridBagConstraints.NORTH; + gbc_toolPanel.fill = GridBagConstraints.HORIZONTAL; + gbc_toolPanel.gridx = 0; + gbc_toolPanel.gridy = 0; + add(toolPanel, gbc_toolPanel); + boardPane = new BoardPane(); + GridBagConstraints gbc_boardPane = new GridBagConstraints(); + gbc_boardPane.fill = GridBagConstraints.BOTH; + gbc_boardPane.gridx = 0; + gbc_boardPane.gridy = 1; + add(boardPane, gbc_boardPane); + + JPanel numberPanel = new JPanel(new GridLayout(8, 1)); + GridBagConstraints gbc_numberPanel = new GridBagConstraints(); + gbc_numberPanel.anchor = GridBagConstraints.WEST; + gbc_numberPanel.fill = GridBagConstraints.VERTICAL; + gbc_numberPanel.gridx = 1; + gbc_numberPanel.gridy = 1; + add(numberPanel, gbc_numberPanel); + + JPanel letterPanel = new JPanel(new GridLayout(1, 8)); + GridBagConstraints gbc_letterPanel = new GridBagConstraints(); + gbc_letterPanel.anchor = GridBagConstraints.NORTH; + gbc_letterPanel.fill = GridBagConstraints.HORIZONTAL; + gbc_letterPanel.gridx = 0; + gbc_letterPanel.gridy = 2; + add(letterPanel, gbc_letterPanel); + + // Initialize board coordinates for (int i = 0; i < 8; i++) { + numberPanel.add(new JLabel(String.valueOf(8 - i))); JLabel letterLabel = new JLabel(String.valueOf((char) (65 + i))); letterLabel.setHorizontalAlignment(JLabel.CENTER); letterPanel.add(letterLabel); } - add(letterPanel, BorderLayout.SOUTH); - JPanel numberPanel = new JPanel(new GridLayout(8, 1)); - for (int i = 0; i < 8; i++) - numberPanel.add(new JLabel(String.valueOf(8 - i))); - add(numberPanel, BorderLayout.EAST); + // LogPanel logPanel = new LogPanel(game.getBoard().getLog()); + // GridBagConstraints gbc_logPanel = new GridBagConstraints(); + // + // add(logPanel, gbc_logPanel); } + /** + * @return The {@link BoardPane} instance associated with this game pane + */ public BoardPane getBoardPane() { return boardPane; } /** - * @return The {@link Game} instance associated with this board pane + * @return The {@link Game} instance associated with this game pane */ public Game getGame() { return game; } diff --git a/src/dev/kske/chess/ui/LogFrame.java b/src/dev/kske/chess/ui/LogPanel.java similarity index 71% rename from src/dev/kske/chess/ui/LogFrame.java rename to src/dev/kske/chess/ui/LogPanel.java index 14e2b88..3a9d8e3 100644 --- a/src/dev/kske/chess/ui/LogFrame.java +++ b/src/dev/kske/chess/ui/LogPanel.java @@ -6,7 +6,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; @@ -22,15 +21,14 @@ import dev.kske.chess.event.Subscribable; /** * Project: Chess
- * File: LogFrame.java
+ * File: LogPanel.java
* Created: 17.07.2019
* Author: Kai S. K. Engelbart */ -public class LogFrame extends JFrame implements Subscribable { +public class LogPanel extends JPanel implements Subscribable { private static final long serialVersionUID = 1932671698254197119L; - private JPanel mcontentPane; private JTable mtable; private Log log; @@ -38,20 +36,15 @@ public class LogFrame extends JFrame implements Subscribable { /** * Create the frame. */ - public LogFrame(Log log) { + public LogPanel(Log log) { this.log = log; - setTitle("Move History"); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - setBounds(100, 100, 450, 300); - mcontentPane = new JPanel(); - mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - mcontentPane.setLayout(new BorderLayout(0, 0)); - setContentPane(mcontentPane); + setBorder(new EmptyBorder(5, 5, 5, 5)); + setLayout(new BorderLayout(0, 0)); mtable = new JTable(); mtable.setEnabled(false); - mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER); + add(new JScrollPane(mtable), BorderLayout.CENTER); EventBus.getInstance().register(this); handle(null); diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index d95ae8a..3ce97f7 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -51,7 +51,7 @@ public class MainWindow { // Add frame content tabbedPane = new JTabbedPane(); - tabbedPane.add("Game 1", new GamePane()); + addGamePane(); // Game is initialized inside MenuBar frame.getContentPane().add(tabbedPane); frame.setJMenuBar(new MenuBar(this)); @@ -67,4 +67,15 @@ public class MainWindow { * @return The currently selected {@link GamePane} component */ public GamePane getSelectedGamePane() { return (GamePane) tabbedPane.getSelectedComponent(); } + + /** + * Creates a new {@link GamePane} and adds it to the tabbed pane. + * + * @return The new {@link GamePane} + */ + public GamePane addGamePane() { + GamePane gamePane = new GamePane(); + tabbedPane.add("Game " + (tabbedPane.getComponentCount() + 1), gamePane); + return gamePane; + } } diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index 36c4953..d3359d4 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -36,13 +36,16 @@ public class MenuBar extends JMenuBar { newGameMenuItem.addActionListener((evt) -> { GameConfigurationDialog dialog = new GameConfigurationDialog(); dialog.setVisible(true); - if (dialog.isStartGame()) startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), - dialog.getWhiteName(), dialog.getBlackName())); + if (dialog.isStartGame()) { + GamePane gamePane = mainWindow.addGamePane(); + gamePane.setGame(new Game(gamePane.getBoardPane(), dialog.getWhiteName(), dialog.getBlackName())); + } }); gameMenu.add(newGameMenuItem); add(gameMenu); + // TODO: Game initialization // Start a game startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), "Natural Player", "Natural Player")); } @@ -68,12 +71,6 @@ public class MenuBar extends JMenuBar { private void initToolsMenu() { JMenu toolsMenu = new JMenu("Tools"); - // TODO: prevent multiple initializations - JMenuItem showLogMenuItem = new JMenuItem("Show move log"); - showLogMenuItem.addActionListener( - (evt) -> new LogFrame(mainWindow.getSelectedGamePane().getGame().getBoard().getLog()).setVisible(true)); - toolsMenu.add(showLogMenuItem); - JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN"); exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit() .getSystemClipboard()