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
This commit is contained in:
Kai S. K. Engelbart 2019-09-01 12:45:06 +02:00
parent 513c0077e0
commit c712e066fe
4 changed files with 73 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package dev.kske.chess.ui; package dev.kske.chess.ui;
import java.awt.BorderLayout; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JButton; import javax.swing.JButton;
@ -28,16 +29,20 @@ public class GamePane extends JComponent {
private Color activeColor; private Color activeColor;
public GamePane() { public GamePane() {
setLayout(new BorderLayout()); activeColor = Color.WHITE;
boardPane = new BoardPane();
add(boardPane, BorderLayout.CENTER); 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(); JPanel toolPanel = new JPanel();
btnRestart = new JButton("Restart"); btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); 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) -> { btnSwapColors.addActionListener((evt) -> {
game.swapColors(); game.swapColors();
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase()); btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
@ -46,26 +51,57 @@ public class GamePane extends JComponent {
toolPanel.add(btnRestart); toolPanel.add(btnRestart);
toolPanel.add(btnSwapColors); 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++) { for (int i = 0; i < 8; i++) {
numberPanel.add(new JLabel(String.valueOf(8 - i)));
JLabel letterLabel = new JLabel(String.valueOf((char) (65 + i))); JLabel letterLabel = new JLabel(String.valueOf((char) (65 + i)));
letterLabel.setHorizontalAlignment(JLabel.CENTER); letterLabel.setHorizontalAlignment(JLabel.CENTER);
letterPanel.add(letterLabel); letterPanel.add(letterLabel);
} }
add(letterPanel, BorderLayout.SOUTH);
JPanel numberPanel = new JPanel(new GridLayout(8, 1)); // LogPanel logPanel = new LogPanel(game.getBoard().getLog());
for (int i = 0; i < 8; i++) // GridBagConstraints gbc_logPanel = new GridBagConstraints();
numberPanel.add(new JLabel(String.valueOf(8 - i))); //
add(numberPanel, BorderLayout.EAST); // add(logPanel, gbc_logPanel);
} }
/**
* @return The {@link BoardPane} instance associated with this game pane
*/
public BoardPane getBoardPane() { return boardPane; } 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; } public Game getGame() { return game; }

View File

@ -6,7 +6,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTable; import javax.swing.JTable;
@ -22,15 +21,14 @@ import dev.kske.chess.event.Subscribable;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
* File: <strong>LogFrame.java</strong><br> * File: <strong>LogPanel.java</strong><br>
* Created: <strong>17.07.2019</strong><br> * Created: <strong>17.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong> * Author: <strong>Kai S. K. Engelbart</strong>
*/ */
public class LogFrame extends JFrame implements Subscribable { public class LogPanel extends JPanel implements Subscribable {
private static final long serialVersionUID = 1932671698254197119L; private static final long serialVersionUID = 1932671698254197119L;
private JPanel mcontentPane;
private JTable mtable; private JTable mtable;
private Log log; private Log log;
@ -38,20 +36,15 @@ public class LogFrame extends JFrame implements Subscribable {
/** /**
* Create the frame. * Create the frame.
*/ */
public LogFrame(Log log) { public LogPanel(Log log) {
this.log = log; this.log = log;
setTitle("Move History"); setBorder(new EmptyBorder(5, 5, 5, 5));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(new BorderLayout(0, 0));
setBounds(100, 100, 450, 300);
mcontentPane = new JPanel();
mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
mcontentPane.setLayout(new BorderLayout(0, 0));
setContentPane(mcontentPane);
mtable = new JTable(); mtable = new JTable();
mtable.setEnabled(false); mtable.setEnabled(false);
mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER); add(new JScrollPane(mtable), BorderLayout.CENTER);
EventBus.getInstance().register(this); EventBus.getInstance().register(this);
handle(null); handle(null);

View File

@ -51,7 +51,7 @@ public class MainWindow {
// Add frame content // Add frame content
tabbedPane = new JTabbedPane(); tabbedPane = new JTabbedPane();
tabbedPane.add("Game 1", new GamePane()); addGamePane(); // Game is initialized inside MenuBar
frame.getContentPane().add(tabbedPane); frame.getContentPane().add(tabbedPane);
frame.setJMenuBar(new MenuBar(this)); frame.setJMenuBar(new MenuBar(this));
@ -67,4 +67,15 @@ public class MainWindow {
* @return The currently selected {@link GamePane} component * @return The currently selected {@link GamePane} component
*/ */
public GamePane getSelectedGamePane() { return (GamePane) tabbedPane.getSelectedComponent(); } 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;
}
} }

View File

@ -36,13 +36,16 @@ public class MenuBar extends JMenuBar {
newGameMenuItem.addActionListener((evt) -> { newGameMenuItem.addActionListener((evt) -> {
GameConfigurationDialog dialog = new GameConfigurationDialog(); GameConfigurationDialog dialog = new GameConfigurationDialog();
dialog.setVisible(true); dialog.setVisible(true);
if (dialog.isStartGame()) startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), if (dialog.isStartGame()) {
dialog.getWhiteName(), dialog.getBlackName())); GamePane gamePane = mainWindow.addGamePane();
gamePane.setGame(new Game(gamePane.getBoardPane(), dialog.getWhiteName(), dialog.getBlackName()));
}
}); });
gameMenu.add(newGameMenuItem); gameMenu.add(newGameMenuItem);
add(gameMenu); add(gameMenu);
// TODO: Game initialization
// Start a game // Start a game
startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), "Natural Player", "Natural Player")); startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), "Natural Player", "Natural Player"));
} }
@ -68,12 +71,6 @@ public class MenuBar extends JMenuBar {
private void initToolsMenu() { private void initToolsMenu() {
JMenu toolsMenu = new JMenu("Tools"); 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"); JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN");
exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit() exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit()
.getSystemClipboard() .getSystemClipboard()