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

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.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();
}
}

View File

@ -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) {