Some UI improvements
This commit is contained in:
parent
73323ce7b3
commit
eacfcd0547
@ -21,6 +21,7 @@ public class TicTacToeView {
|
|||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
var btn = new Button("");
|
var btn = new Button("");
|
||||||
btn.setEnabled(false);
|
btn.setEnabled(false);
|
||||||
|
btn.setBackground(Color.GRAY);
|
||||||
btn.setActionCommand(Integer.toString(i));
|
btn.setActionCommand(Integer.toString(i));
|
||||||
btn.addActionListener(e -> {
|
btn.addActionListener(e -> {
|
||||||
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
|
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
|
||||||
@ -45,12 +46,14 @@ public class TicTacToeView {
|
|||||||
public void activate(List<Integer> btnIds) {
|
public void activate(List<Integer> btnIds) {
|
||||||
for (Integer btnId : btnIds) {
|
for (Integer btnId : btnIds) {
|
||||||
this.buttons[btnId].setEnabled(true);
|
this.buttons[btnId].setEnabled(true);
|
||||||
|
this.buttons[btnId].setBackground(Color.GREEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deactivate() {
|
public void deactivate() {
|
||||||
for (Button button : buttons) {
|
for (Button button : buttons) {
|
||||||
button.setEnabled(false);
|
button.setEnabled(false);
|
||||||
|
button.setBackground(Color.GRAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,9 +61,12 @@ public class TicTacToeView {
|
|||||||
this.view.removeAll();
|
this.view.removeAll();
|
||||||
this.view.setLayout(new BorderLayout());
|
this.view.setLayout(new BorderLayout());
|
||||||
Button winnerLabel = new Button(finishString);
|
Button winnerLabel = new Button(finishString);
|
||||||
|
winnerLabel.setEnabled(false);
|
||||||
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
||||||
winnerLabel.setFont(font);
|
winnerLabel.setFont(font);
|
||||||
this.view.add(winnerLabel);
|
this.view.add(winnerLabel, BorderLayout.CENTER);
|
||||||
|
this.view.validate();
|
||||||
|
this.view.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ultimatetictactoe.view;
|
package ultimatetictactoe.view;
|
||||||
|
|
||||||
|
import ultimatetictactoe.model.Player;
|
||||||
import ultimatetictactoe.viewmodel.TicTacToePanelViewModel;
|
import ultimatetictactoe.viewmodel.TicTacToePanelViewModel;
|
||||||
import ultimatetictactoe.viewmodel.UltimateTicTacToePanelViewModel;
|
import ultimatetictactoe.viewmodel.UltimateTicTacToePanelViewModel;
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ import java.awt.*;
|
|||||||
public class UltimateTicTacToeView extends JFrame {
|
public class UltimateTicTacToeView extends JFrame {
|
||||||
|
|
||||||
private final UltimateTicTacToePanelViewModel viewModel;
|
private final UltimateTicTacToePanelViewModel viewModel;
|
||||||
|
private final Label currentPlayerLabel;
|
||||||
|
|
||||||
public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) {
|
public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) {
|
||||||
super("Ultimate TicTacToe");
|
super("Ultimate TicTacToe");
|
||||||
@ -17,26 +19,45 @@ public class UltimateTicTacToeView extends JFrame {
|
|||||||
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
|
||||||
//Create and set up the window.
|
//Create and set up the window.
|
||||||
this.setLayout(new GridLayout(3, 3, 2, 2));
|
this.getContentPane().setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
JPanel mainGamePane = new JPanel();
|
||||||
|
this.getContentPane().add(mainGamePane, BorderLayout.CENTER);
|
||||||
|
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);
|
||||||
this.add(new TicTacToeView(subViewModel).getView());
|
mainGamePane.add(new TicTacToeView(subViewModel).getView());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.currentPlayerLabel = new Label();
|
||||||
|
this.getContentPane().add(this.currentPlayerLabel, BorderLayout.NORTH);
|
||||||
|
|
||||||
this.viewModel.setGameResultCallback(this::setGameResult);
|
this.viewModel.setGameResultCallback(this::setGameResult);
|
||||||
|
this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer);
|
||||||
|
|
||||||
//Display the window.
|
//Display the window.
|
||||||
this.setSize(new Dimension(500, 500));
|
this.setSize(new Dimension(500, 520));
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGameResult(String resultText) {
|
public void setGameResult(String resultText) {
|
||||||
this.removeAll();
|
this.getContentPane().removeAll();
|
||||||
this.setLayout(new BorderLayout());
|
|
||||||
|
this.getContentPane().setLayout(new BorderLayout());
|
||||||
Button winnerLabel = new Button(resultText);
|
Button winnerLabel = new Button(resultText);
|
||||||
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
||||||
winnerLabel.setFont(font);
|
winnerLabel.setFont(font);
|
||||||
this.add(winnerLabel);
|
|
||||||
|
this.getContentPane().add(winnerLabel);
|
||||||
|
this.getContentPane().validate();
|
||||||
|
this.getContentPane().repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentPlayer(Player player) {
|
||||||
|
if (player == Player.O) {
|
||||||
|
this.currentPlayerLabel.setText("Current player: O");
|
||||||
|
} else if (player == Player.X) {
|
||||||
|
this.currentPlayerLabel.setText("Current player: X");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ultimatetictactoe.viewmodel;
|
package ultimatetictactoe.viewmodel;
|
||||||
|
|
||||||
import ultimatetictactoe.model.GameState;
|
import ultimatetictactoe.model.GameState;
|
||||||
|
import ultimatetictactoe.model.Player;
|
||||||
import ultimatetictactoe.model.TicTacToeGame;
|
import ultimatetictactoe.model.TicTacToeGame;
|
||||||
import ultimatetictactoe.model.UltimateTicTacToe;
|
import ultimatetictactoe.model.UltimateTicTacToe;
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ public class UltimateTicTacToePanelViewModel {
|
|||||||
private final TicTacToePanelViewModel[] subGameViewModels;
|
private final TicTacToePanelViewModel[] subGameViewModels;
|
||||||
private final UltimateTicTacToe ultimateTicTacToe;
|
private final UltimateTicTacToe ultimateTicTacToe;
|
||||||
private Consumer<String> gameResultCallback;
|
private Consumer<String> gameResultCallback;
|
||||||
|
private Consumer<Player> currentPlayerCallback;
|
||||||
|
|
||||||
public UltimateTicTacToePanelViewModel() {
|
public UltimateTicTacToePanelViewModel() {
|
||||||
this.subGameViewModels = new TicTacToePanelViewModel[9];
|
this.subGameViewModels = new TicTacToePanelViewModel[9];
|
||||||
@ -34,6 +36,7 @@ public class UltimateTicTacToePanelViewModel {
|
|||||||
final int fieldId = this.ultimateTicTacToe.getActiveField();
|
final int fieldId = this.ultimateTicTacToe.getActiveField();
|
||||||
final List<Integer> availableFields = this.ultimateTicTacToe.getSubGame(fieldId).getAvailableFields();
|
final List<Integer> availableFields = this.ultimateTicTacToe.getSubGame(fieldId).getAvailableFields();
|
||||||
this.subGameViewModels[fieldId].activate(availableFields);
|
this.subGameViewModels[fieldId].activate(availableFields);
|
||||||
|
this.currentPlayerCallback.accept(this.ultimateTicTacToe.getCurrentPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCellClicked(int cell) {
|
public void onCellClicked(int cell) {
|
||||||
@ -62,4 +65,8 @@ public class UltimateTicTacToePanelViewModel {
|
|||||||
public void setGameResultCallback(Consumer<String> gameResultCallback) {
|
public void setGameResultCallback(Consumer<String> gameResultCallback) {
|
||||||
this.gameResultCallback = gameResultCallback;
|
this.gameResultCallback = gameResultCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCurrentPlayerCallback(Consumer<Player> currentPlayerCallback) {
|
||||||
|
this.currentPlayerCallback = currentPlayerCallback;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user