From 7736fb510141aa59b0413105d94e99f8cfecf8e1 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 3 Nov 2021 23:22:49 +0100 Subject: [PATCH] Introduce own view for JFrame, Change button listener to use static number --- .../de/icaotix/ultimatetictactoe/Main.java | 14 ++------- .../view/MainWindowView.java | 24 +++++++++++++++ .../ultimatetictactoe/view/TicTacToeView.java | 29 +++++++----------- .../view/UltimateTicTacToeView.java | 30 ++++++++----------- 4 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 src/main/java/de/icaotix/ultimatetictactoe/view/MainWindowView.java diff --git a/src/main/java/de/icaotix/ultimatetictactoe/Main.java b/src/main/java/de/icaotix/ultimatetictactoe/Main.java index 3f913bd..9405816 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/Main.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/Main.java @@ -1,22 +1,12 @@ package de.icaotix.ultimatetictactoe; -import de.icaotix.ultimatetictactoe.model.ModelFactory; -import de.icaotix.ultimatetictactoe.view.UltimateTicTacToeView; -import de.icaotix.ultimatetictactoe.viewmodel.ViewModelFactory; +import de.icaotix.ultimatetictactoe.view.MainWindowView; import javax.swing.*; public class Main { public static void main(String[] args) { - SwingUtilities.invokeLater(() -> { - - ModelFactory modelFactory = new ModelFactory(); - ViewModelFactory viewModelFactory = new ViewModelFactory(modelFactory); - - final var ultimateTicTacToePanelViewModel = viewModelFactory.getUltimateTicTacToePanelViewModel(); - new UltimateTicTacToeView(ultimateTicTacToePanelViewModel); - ultimateTicTacToePanelViewModel.prepareNextMove(); - }); + SwingUtilities.invokeLater(MainWindowView::new); } } diff --git a/src/main/java/de/icaotix/ultimatetictactoe/view/MainWindowView.java b/src/main/java/de/icaotix/ultimatetictactoe/view/MainWindowView.java new file mode 100644 index 0000000..ed5723f --- /dev/null +++ b/src/main/java/de/icaotix/ultimatetictactoe/view/MainWindowView.java @@ -0,0 +1,24 @@ +package de.icaotix.ultimatetictactoe.view; + +import de.icaotix.ultimatetictactoe.model.ModelFactory; +import de.icaotix.ultimatetictactoe.viewmodel.ViewModelFactory; + +import javax.swing.*; +import java.awt.*; + +public class MainWindowView extends JFrame { + + public MainWindowView() throws HeadlessException { + super("Ultimate TicTacToe"); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + + ModelFactory modelFactory = new ModelFactory(); + ViewModelFactory viewModelFactory = new ViewModelFactory(modelFactory); + + final var ultimateTicTacToePanelViewModel = viewModelFactory.getUltimateTicTacToePanelViewModel(); + this.setContentPane(new UltimateTicTacToeView(ultimateTicTacToePanelViewModel)); + ultimateTicTacToePanelViewModel.prepareNextMove(); + this.setSize(new Dimension(500, 520)); + this.setVisible(true); + } +} diff --git a/src/main/java/de/icaotix/ultimatetictactoe/view/TicTacToeView.java b/src/main/java/de/icaotix/ultimatetictactoe/view/TicTacToeView.java index e9fe474..cb4637c 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/view/TicTacToeView.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/view/TicTacToeView.java @@ -7,24 +7,21 @@ import javax.swing.plaf.metal.MetalButtonUI; import java.awt.*; import java.util.List; -public class TicTacToeView { +public class TicTacToeView extends JPanel { private final TicTacToePanelViewModel viewModel; - private final JPanel view; private final JButton[] buttons; public TicTacToeView(TicTacToePanelViewModel viewModel) { this.viewModel = viewModel; this.buttons = new JButton[9]; - this.view = new JPanel(new GridLayout(3, 3, 0, 0)); - view.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + this.setLayout(new GridLayout(3, 3, 0, 0)); + this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); for (int i = 0; i < 9; i++) { var btn = new JButton(""); - btn.setActionCommand(Integer.toString(i)); - btn.addActionListener(e -> { - viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand())); - }); + final int clickedButton = i; + btn.addActionListener(e -> viewModel.onButtonClicked(clickedButton)); btn.setUI(new MetalButtonUI() { @Override protected Color getDisabledTextColor() { @@ -32,7 +29,7 @@ public class TicTacToeView { } }); buttons[i] = btn; - view.add(btn); + this.add(btn); } this.deactivate(); @@ -42,10 +39,6 @@ public class TicTacToeView { this.viewModel.setButtonTextCallback(this::setButtonText); } - public JPanel getView() { - return this.view; - } - public void setButtonText(int btnId, String text) { this.buttons[btnId].setText(text); } @@ -65,14 +58,14 @@ public class TicTacToeView { } public void setFinishPanel(String finishString) { - this.view.removeAll(); - this.view.setLayout(new BorderLayout()); + this.removeAll(); + this.setLayout(new BorderLayout()); JButton winnerLabel = new JButton(finishString); winnerLabel.setEnabled(false); winnerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); - this.view.add(winnerLabel, BorderLayout.CENTER); - this.view.validate(); - this.view.repaint(); + this.add(winnerLabel, BorderLayout.CENTER); + this.validate(); + this.repaint(); } } diff --git a/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java b/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java index 0a13738..a5d6430 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java @@ -7,48 +7,42 @@ import javax.swing.*; import java.awt.*; -public class UltimateTicTacToeView extends JFrame { +public class UltimateTicTacToeView extends JPanel { private final UltimateTicTacToePanelViewModel viewModel; private JLabel currentPlayerLabel; public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) { - super("Ultimate TicTacToe"); this.viewModel = viewModel; - this.setDefaultCloseOperation(EXIT_ON_CLOSE); - this.setContentToGamePane(); - - this.setSize(new Dimension(500, 520)); - this.setVisible(true); } public void setContentToGamePane() { - this.getContentPane().removeAll(); - this.getContentPane().setLayout(new BorderLayout()); + this.removeAll(); + this.setLayout(new BorderLayout()); JPanel mainGamePane = new JPanel(); - this.getContentPane().add(mainGamePane, BorderLayout.CENTER); + this.add(mainGamePane, BorderLayout.CENTER); mainGamePane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainGamePane.setLayout(new GridLayout(3, 3, 2, 2)); for (int i = 0; i < 9; i++) { TicTacToePanelViewModel subViewModel = viewModel.getSubGameViewModel(i); - mainGamePane.add(new TicTacToeView(subViewModel).getView()); + mainGamePane.add(new TicTacToeView(subViewModel)); } this.currentPlayerLabel = new JLabel(); this.currentPlayerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); this.currentPlayerLabel.setHorizontalAlignment(SwingConstants.CENTER); - this.getContentPane().add(this.currentPlayerLabel, BorderLayout.NORTH); + this.add(this.currentPlayerLabel, BorderLayout.NORTH); this.viewModel.setGameResultCallback(this::setGameResult); this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer); } public void setGameResult(String resultText) { - this.getContentPane().removeAll(); + this.removeAll(); - this.getContentPane().setLayout(new BorderLayout()); + this.setLayout(new BorderLayout()); JLabel winnerLabel = new JLabel(resultText); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20); winnerLabel.setFont(font); @@ -61,10 +55,10 @@ public class UltimateTicTacToeView extends JFrame { this.viewModel.prepareNextMove(); }); - this.getContentPane().add(winnerLabel, BorderLayout.CENTER); - this.getContentPane().add(restartBtn, BorderLayout.SOUTH); - this.getContentPane().validate(); - this.getContentPane().repaint(); + this.add(winnerLabel, BorderLayout.CENTER); + this.add(restartBtn, BorderLayout.SOUTH); + this.validate(); + this.repaint(); } public void setCurrentPlayer(String playerText) {