Introduce own view for JFrame, Change button listener to use static number

This commit is contained in:
Marcel Schwarz 2021-11-03 23:22:49 +01:00
parent 9d866c7034
commit 7736fb5101
4 changed files with 49 additions and 48 deletions

View File

@ -1,22 +1,12 @@
package de.icaotix.ultimatetictactoe; package de.icaotix.ultimatetictactoe;
import de.icaotix.ultimatetictactoe.model.ModelFactory; import de.icaotix.ultimatetictactoe.view.MainWindowView;
import de.icaotix.ultimatetictactoe.view.UltimateTicTacToeView;
import de.icaotix.ultimatetictactoe.viewmodel.ViewModelFactory;
import javax.swing.*; import javax.swing.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(MainWindowView::new);
ModelFactory modelFactory = new ModelFactory();
ViewModelFactory viewModelFactory = new ViewModelFactory(modelFactory);
final var ultimateTicTacToePanelViewModel = viewModelFactory.getUltimateTicTacToePanelViewModel();
new UltimateTicTacToeView(ultimateTicTacToePanelViewModel);
ultimateTicTacToePanelViewModel.prepareNextMove();
});
} }
} }

View File

@ -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);
}
}

View File

@ -7,24 +7,21 @@ import javax.swing.plaf.metal.MetalButtonUI;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
public class TicTacToeView { public class TicTacToeView extends JPanel {
private final TicTacToePanelViewModel viewModel; private final TicTacToePanelViewModel viewModel;
private final JPanel view;
private final JButton[] buttons; private final JButton[] buttons;
public TicTacToeView(TicTacToePanelViewModel viewModel) { public TicTacToeView(TicTacToePanelViewModel viewModel) {
this.viewModel = viewModel; this.viewModel = viewModel;
this.buttons = new JButton[9]; this.buttons = new JButton[9];
this.view = new JPanel(new GridLayout(3, 3, 0, 0)); this.setLayout(new GridLayout(3, 3, 0, 0));
view.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
var btn = new JButton(""); var btn = new JButton("");
btn.setActionCommand(Integer.toString(i)); final int clickedButton = i;
btn.addActionListener(e -> { btn.addActionListener(e -> viewModel.onButtonClicked(clickedButton));
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
});
btn.setUI(new MetalButtonUI() { btn.setUI(new MetalButtonUI() {
@Override @Override
protected Color getDisabledTextColor() { protected Color getDisabledTextColor() {
@ -32,7 +29,7 @@ public class TicTacToeView {
} }
}); });
buttons[i] = btn; buttons[i] = btn;
view.add(btn); this.add(btn);
} }
this.deactivate(); this.deactivate();
@ -42,10 +39,6 @@ public class TicTacToeView {
this.viewModel.setButtonTextCallback(this::setButtonText); this.viewModel.setButtonTextCallback(this::setButtonText);
} }
public JPanel getView() {
return this.view;
}
public void setButtonText(int btnId, String text) { public void setButtonText(int btnId, String text) {
this.buttons[btnId].setText(text); this.buttons[btnId].setText(text);
} }
@ -65,14 +58,14 @@ public class TicTacToeView {
} }
public void setFinishPanel(String finishString) { public void setFinishPanel(String finishString) {
this.view.removeAll(); this.removeAll();
this.view.setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
JButton winnerLabel = new JButton(finishString); JButton winnerLabel = new JButton(finishString);
winnerLabel.setEnabled(false); winnerLabel.setEnabled(false);
winnerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); winnerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
this.view.add(winnerLabel, BorderLayout.CENTER); this.add(winnerLabel, BorderLayout.CENTER);
this.view.validate(); this.validate();
this.view.repaint(); this.repaint();
} }
} }

View File

@ -7,48 +7,42 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class UltimateTicTacToeView extends JFrame { public class UltimateTicTacToeView extends JPanel {
private final UltimateTicTacToePanelViewModel viewModel; private final UltimateTicTacToePanelViewModel viewModel;
private JLabel currentPlayerLabel; private JLabel currentPlayerLabel;
public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) { public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) {
super("Ultimate TicTacToe");
this.viewModel = viewModel; this.viewModel = viewModel;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentToGamePane(); this.setContentToGamePane();
this.setSize(new Dimension(500, 520));
this.setVisible(true);
} }
public void setContentToGamePane() { public void setContentToGamePane() {
this.getContentPane().removeAll(); this.removeAll();
this.getContentPane().setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
JPanel mainGamePane = new JPanel(); JPanel mainGamePane = new JPanel();
this.getContentPane().add(mainGamePane, BorderLayout.CENTER); this.add(mainGamePane, BorderLayout.CENTER);
mainGamePane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainGamePane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainGamePane.setLayout(new GridLayout(3, 3, 2, 2)); mainGamePane.setLayout(new GridLayout(3, 3, 2, 2));
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
TicTacToePanelViewModel subViewModel = viewModel.getSubGameViewModel(i); TicTacToePanelViewModel subViewModel = viewModel.getSubGameViewModel(i);
mainGamePane.add(new TicTacToeView(subViewModel).getView()); mainGamePane.add(new TicTacToeView(subViewModel));
} }
this.currentPlayerLabel = new JLabel(); this.currentPlayerLabel = new JLabel();
this.currentPlayerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); this.currentPlayerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
this.currentPlayerLabel.setHorizontalAlignment(SwingConstants.CENTER); 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.setGameResultCallback(this::setGameResult);
this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer); this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer);
} }
public void setGameResult(String resultText) { public void setGameResult(String resultText) {
this.getContentPane().removeAll(); this.removeAll();
this.getContentPane().setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
JLabel winnerLabel = new JLabel(resultText); JLabel winnerLabel = new JLabel(resultText);
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20);
winnerLabel.setFont(font); winnerLabel.setFont(font);
@ -61,10 +55,10 @@ public class UltimateTicTacToeView extends JFrame {
this.viewModel.prepareNextMove(); this.viewModel.prepareNextMove();
}); });
this.getContentPane().add(winnerLabel, BorderLayout.CENTER); this.add(winnerLabel, BorderLayout.CENTER);
this.getContentPane().add(restartBtn, BorderLayout.SOUTH); this.add(restartBtn, BorderLayout.SOUTH);
this.getContentPane().validate(); this.validate();
this.getContentPane().repaint(); this.repaint();
} }
public void setCurrentPlayer(String playerText) { public void setCurrentPlayer(String playerText) {