Some UI improvements

This commit is contained in:
Marcel Schwarz 2021-11-03 23:14:13 +01:00
parent 73323ce7b3
commit eacfcd0547
3 changed files with 41 additions and 7 deletions

View File

@ -21,6 +21,7 @@ public class TicTacToeView {
for (int i = 0; i < 9; i++) {
var btn = new Button("");
btn.setEnabled(false);
btn.setBackground(Color.GRAY);
btn.setActionCommand(Integer.toString(i));
btn.addActionListener(e -> {
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
@ -45,12 +46,14 @@ public class TicTacToeView {
public void activate(List<Integer> btnIds) {
for (Integer btnId : btnIds) {
this.buttons[btnId].setEnabled(true);
this.buttons[btnId].setBackground(Color.GREEN);
}
}
public void deactivate() {
for (Button button : buttons) {
button.setEnabled(false);
button.setBackground(Color.GRAY);
}
}
@ -58,9 +61,12 @@ public class TicTacToeView {
this.view.removeAll();
this.view.setLayout(new BorderLayout());
Button winnerLabel = new Button(finishString);
winnerLabel.setEnabled(false);
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
winnerLabel.setFont(font);
this.view.add(winnerLabel);
this.view.add(winnerLabel, BorderLayout.CENTER);
this.view.validate();
this.view.repaint();
}
}

View File

@ -1,5 +1,6 @@
package ultimatetictactoe.view;
import ultimatetictactoe.model.Player;
import ultimatetictactoe.viewmodel.TicTacToePanelViewModel;
import ultimatetictactoe.viewmodel.UltimateTicTacToePanelViewModel;
@ -10,6 +11,7 @@ import java.awt.*;
public class UltimateTicTacToeView extends JFrame {
private final UltimateTicTacToePanelViewModel viewModel;
private final Label currentPlayerLabel;
public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) {
super("Ultimate TicTacToe");
@ -17,26 +19,45 @@ public class UltimateTicTacToeView extends JFrame {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//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++) {
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.setCurrentPlayerCallback(this::setCurrentPlayer);
//Display the window.
this.setSize(new Dimension(500, 500));
this.setSize(new Dimension(500, 520));
this.setVisible(true);
}
public void setGameResult(String resultText) {
this.removeAll();
this.setLayout(new BorderLayout());
this.getContentPane().removeAll();
this.getContentPane().setLayout(new BorderLayout());
Button winnerLabel = new Button(resultText);
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
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");
}
}
}

View File

@ -1,6 +1,7 @@
package ultimatetictactoe.viewmodel;
import ultimatetictactoe.model.GameState;
import ultimatetictactoe.model.Player;
import ultimatetictactoe.model.TicTacToeGame;
import ultimatetictactoe.model.UltimateTicTacToe;
@ -12,6 +13,7 @@ public class UltimateTicTacToePanelViewModel {
private final TicTacToePanelViewModel[] subGameViewModels;
private final UltimateTicTacToe ultimateTicTacToe;
private Consumer<String> gameResultCallback;
private Consumer<Player> currentPlayerCallback;
public UltimateTicTacToePanelViewModel() {
this.subGameViewModels = new TicTacToePanelViewModel[9];
@ -34,6 +36,7 @@ public class UltimateTicTacToePanelViewModel {
final int fieldId = this.ultimateTicTacToe.getActiveField();
final List<Integer> availableFields = this.ultimateTicTacToe.getSubGame(fieldId).getAvailableFields();
this.subGameViewModels[fieldId].activate(availableFields);
this.currentPlayerCallback.accept(this.ultimateTicTacToe.getCurrentPlayer());
}
public void onCellClicked(int cell) {
@ -62,4 +65,8 @@ public class UltimateTicTacToePanelViewModel {
public void setGameResultCallback(Consumer<String> gameResultCallback) {
this.gameResultCallback = gameResultCallback;
}
public void setCurrentPlayerCallback(Consumer<Player> currentPlayerCallback) {
this.currentPlayerCallback = currentPlayerCallback;
}
}