Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 1eebaa9f51cee2b6043347997d3b5c4b5e97a405
parent fa5641023f5994ed9fd4e9a673e5c0ecf5764db7
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Thu,  7 Nov 2024 18:20:44 +0000

Implemented main window UI

Diffstat:
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 36++++++++++++++++++++++++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 37+++++++++++++++++++++++++++++++++++--
3 files changed, 162 insertions(+), 2 deletions(-)

diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java @@ -0,0 +1,90 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.groupproject.tictactoeclient.ContentPanes; + +import com.groupproject.tictactoeclient.TicTacToeClient; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.SpringLayout; + +/** + * + * @author Willi + * Contains the content visible in the main program. + */ +public class MainContentPanel extends JPanel { + public MainContentPanel(TicTacToeClient client) { + + // Set panel layout to spring layout + SpringLayout layout = new SpringLayout(); + setLayout(layout); + + // Create panel components + JButton createGameButton = new JButton("New Game"); + JButton scoreBoardButton = new JButton("Scoreboard"); + JButton joinGameButton = new JButton("Join Game"); + String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; // mock list + JList<String> openGamesList = new JList<>(items); + JScrollPane openGamesScrollPane = new JScrollPane(openGamesList); + TicTacToeGrid grid = new TicTacToeGrid(); + JLabel gameStatusLabel = new JLabel(); + JLabel gameTimerLabel = new JLabel(); + JButton viewScoreButton = new JButton("View Personal Score"); + + // set mock data for labels: + gameStatusLabel.setText("Opponents turn"); + gameTimerLabel.setText("12m 32s"); + + + // Add buttons to panel + add(scoreBoardButton); + add(createGameButton); + add(openGamesScrollPane); + add(joinGameButton); + add(grid); + add(gameStatusLabel); + add(gameTimerLabel); + add(viewScoreButton); + + // Add sprint layout constraints + // create game button constraints + layout.putConstraint(SpringLayout.WEST, createGameButton, 20, SpringLayout.WEST, this); + layout.putConstraint(SpringLayout.NORTH, createGameButton, 10, SpringLayout.NORTH, this); + + // scoreboard button constraints + layout.putConstraint(SpringLayout.WEST, scoreBoardButton, 20, SpringLayout.EAST, createGameButton); + layout.putConstraint(SpringLayout.NORTH, scoreBoardButton, 0, SpringLayout.NORTH, createGameButton); + + // Games list constraints + layout.putConstraint(SpringLayout.WEST, openGamesScrollPane, 0, SpringLayout.WEST, createGameButton); + layout.putConstraint(SpringLayout.EAST, openGamesScrollPane, 0, SpringLayout.EAST, scoreBoardButton); + layout.putConstraint(SpringLayout.NORTH, openGamesScrollPane, 10, SpringLayout.SOUTH, createGameButton); + layout.putConstraint(SpringLayout.SOUTH, openGamesScrollPane, -10, SpringLayout.NORTH, joinGameButton); + + // join button constraints + layout.putConstraint(SpringLayout.WEST, joinGameButton, 20, SpringLayout.WEST, this); + layout.putConstraint(SpringLayout.SOUTH, joinGameButton, -20, SpringLayout.SOUTH, this); + layout.putConstraint(SpringLayout.EAST, joinGameButton, 0, SpringLayout.EAST, scoreBoardButton); + + // Add label constraints + layout.putConstraint(SpringLayout.WEST, gameStatusLabel, 0, SpringLayout.WEST, grid); + layout.putConstraint(SpringLayout.NORTH, gameStatusLabel, -20, SpringLayout.NORTH, grid); + layout.putConstraint(SpringLayout.EAST, gameTimerLabel, 0, SpringLayout.EAST, grid); + layout.putConstraint(SpringLayout.NORTH, gameTimerLabel, -20, SpringLayout.NORTH, grid); + + // grid constraints + layout.putConstraint(SpringLayout.WEST, grid, 40, SpringLayout.EAST, scoreBoardButton); + layout.putConstraint(SpringLayout.NORTH, grid, 40, SpringLayout.NORTH, this); + layout.putConstraint(SpringLayout.SOUTH, grid, 240, SpringLayout.NORTH, this); + layout.putConstraint(SpringLayout.EAST, grid, -20, SpringLayout.EAST, this); + + // View score button constraints + layout.putConstraint(SpringLayout.SOUTH, viewScoreButton, -20, SpringLayout.SOUTH, this); + layout.putConstraint(SpringLayout.EAST, viewScoreButton, -20, SpringLayout.EAST, this); + } +} +\ No newline at end of file diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java @@ -0,0 +1,36 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.groupproject.tictactoeclient.ContentPanes; + +import java.awt.Color; +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.border.LineBorder; + +/** + * + * @author Willi + */ +public class TicTacToeGrid extends JPanel { + JButton[][] grid = new JButton[3][3]; + + public TicTacToeGrid() { + + setLayout(new GridLayout(3, 3)); + + for (int row = 0; row < 3; row++) { + for (int col = 0; col < 3; col++) { + grid[row][col] = new JButton(""); + grid[row][col].setBackground(new Color(45, 45, 45)); // Dark gray for FlatLaf Dark theme + grid[row][col].setForeground(Color.WHITE); // Set font color to white + grid[row][col].setOpaque(true); + grid[row][col].setBorder(new LineBorder(new Color(70, 70, 70), 1)); // Soft, light gray border + grid[row][col].setOpaque(true); + add(grid[row][col]); + } + } + } +} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -5,8 +5,12 @@ package com.groupproject.tictactoeclient; import com.formdev.flatlaf.FlatDarkLaf; +import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; import com.tttws.TicTacToeWS; import com.tttws.TicTacToeWebService; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; @@ -15,15 +19,44 @@ import javax.swing.UnsupportedLookAndFeelException; * @author Willi */ public class TicTacToeClient { + private static TicTacToeClient client; private static TicTacToeWebService service; private static TicTacToeWS proxy; + private JFrame frame; public static void main(String[] args) { - System.out.println("Hello World!"); - + // Enable flatlaf dark theme try { UIManager.setLookAndFeel(new FlatDarkLaf()); } catch (UnsupportedLookAndFeelException e) { + JOptionPane.showMessageDialog( + null, + "Could not initialize FlatLaf. Default swing look and feel will be used.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } + + // Initalize UI + client = new TicTacToeClient(); + } + + public TicTacToeClient() { + + + frame = new JFrame("TicTacToe"); // Create new Swing window + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(600, 400); + + showPanel(new MainContentPanel(this)); + + frame.setVisible(true); + } + + public void showPanel(JPanel panel) { + frame.getContentPane().removeAll(); // Clear current content + frame.getContentPane().add(panel); // Add new panel + frame.revalidate(); // Refresh the frame + frame.repaint(); } } \ No newline at end of file