Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit a0b1d4fbb4eb9c91a50ebdd263c3592920608336
parent d1e1cb6ac54abdeb92ed39add9caeb5b29955b9b
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Tue, 12 Nov 2024 11:56:32 +0000

Implemented CustomPanel and way to update games list

Diffstat:
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/CustomPanel.java | 15+++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 26++++++++++++++++++--------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/StartPanel.java | 7++++++-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 7++++++-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java | 7++++++-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java | 7++++++-
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java | 40++++++++++++++++++++++++++++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 15++++++++++++---
MTicTacToeWS/src/main/java/com/tttws/TicTacToeWS.java | 1+
9 files changed, 110 insertions(+), 15 deletions(-)

diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/CustomPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/CustomPanel.java @@ -0,0 +1,15 @@ +/* + * 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 javax.swing.JPanel; + +/** + * + * @author Willi + */ +public abstract class CustomPanel extends JPanel { + public abstract void refresh(); +} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java @@ -19,9 +19,15 @@ import javax.swing.SpringLayout; * @author William * Contains the content visible in the main program. */ -public class MainContentPanel extends JPanel { +public class MainContentPanel extends CustomPanel { + + TicTacToeClient client; + JList<String> openGamesList; + public MainContentPanel(TicTacToeClient client) { + this.client = client; + // Set panel layout to spring layout SpringLayout layout = new SpringLayout(); setLayout(layout); @@ -30,8 +36,7 @@ public class MainContentPanel extends JPanel { 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); + openGamesList = new JList<>(); JScrollPane openGamesScrollPane = new JScrollPane(openGamesList); TicTacToeGrid grid = new TicTacToeGrid(); JLabel gameStatusLabel = new JLabel(); @@ -90,9 +95,6 @@ public class MainContentPanel extends JPanel { layout.putConstraint(SpringLayout.EAST, viewScoreButton, -20, SpringLayout.EAST, this); - - - //Create game button action listener createGameButton.addActionListener(new ActionListener() { @Override @@ -100,7 +102,15 @@ public class MainContentPanel extends JPanel { client.proxy.newGame(Integer.parseInt(client.UID)); } }); - - + } + + @Override + public void refresh() { + System.out.println("Refreshing MainContentPanel"); + var games = client.openGames.split(","); + System.out.println("Current Games: " + client.openGames); + openGamesList.setListData(games); + this.revalidate(); + this.repaint(); } } \ No newline at end of file diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/StartPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/StartPanel.java @@ -15,7 +15,7 @@ import javax.swing.SpringLayout; * * @author Adam */ -public class StartPanel extends JPanel{ +public class StartPanel extends CustomPanel { public StartPanel(TicTacToeClient client) { // Set panel layout to spring layout @@ -56,4 +56,9 @@ public class StartPanel extends JPanel{ } }); } + + @Override + public void refresh() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java @@ -14,7 +14,7 @@ import javax.swing.border.LineBorder; * * @author William */ -public class TicTacToeGrid extends JPanel { +public class TicTacToeGrid extends CustomPanel { JButton[][] grid = new JButton[3][3]; public TicTacToeGrid() { @@ -33,4 +33,9 @@ public class TicTacToeGrid extends JPanel { } } } + + @Override + public void refresh() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java @@ -13,7 +13,7 @@ import javax.swing.*; * * @author adam */ -public class login extends JPanel { +public class login extends CustomPanel { public login(TicTacToeClient client) { // Set panel layout to spring layout @@ -124,4 +124,9 @@ public class login extends JPanel { } }); } + + @Override + public void refresh() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java @@ -16,7 +16,7 @@ import javax.swing.Box; * * Author: Luke */ -public class register extends JPanel { +public class register extends CustomPanel { public register(TicTacToeClient client) { // Set the layout to BoxLayout for vertical alignment setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); @@ -70,4 +70,9 @@ public class register extends JPanel { } }); } + + @Override + public void refresh() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } } \ No newline at end of file diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java @@ -0,0 +1,40 @@ +/* + * 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; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Willi + */ +public class FetchGamesThread implements Runnable { + private TicTacToeClient client; + + public FetchGamesThread(TicTacToeClient client) { + this.client = client; + } + + @Override + public void run() { + while (true) { + try { + Thread.sleep(1000); + + if (client.UID == null || Integer.parseInt(client.UID) == -1) { + continue; + } + + System.out.println("Fetch Games thread executing..."); + client.openGames = client.proxy.showOpenGames(); + System.out.println("Result: " + client.openGames); + client.refreshCurrentPanel(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -5,6 +5,7 @@ package com.groupproject.tictactoeclient; import com.formdev.flatlaf.FlatDarkLaf; +import com.groupproject.tictactoeclient.ContentPanes.CustomPanel; import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; import com.groupproject.tictactoeclient.ContentPanes.StartPanel; import com.groupproject.tictactoeclient.ContentPanes.register; @@ -25,7 +26,9 @@ public class TicTacToeClient { private static TicTacToeWebService service; public static TicTacToeWS proxy; private JFrame frame; + private CustomPanel CurrentPanel; public String UID; + public String openGames; public static void main(String[] args) { // Enable flatlaf dark theme @@ -49,8 +52,6 @@ public class TicTacToeClient { } public TicTacToeClient() { - - frame = new JFrame("TicTacToe"); // Create new Swing window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); @@ -58,12 +59,20 @@ public class TicTacToeClient { showPanel(new StartPanel(this)); frame.setVisible(true); + + Thread t1 = new Thread(new FetchGamesThread(this)); + t1.start(); } - public void showPanel(JPanel panel) { + public void showPanel(CustomPanel panel) { frame.getContentPane().removeAll(); // Clear current content + CurrentPanel = panel; frame.getContentPane().add(panel); // Add new panel frame.revalidate(); // Refresh the frame frame.repaint(); } + + public void refreshCurrentPanel() { + CurrentPanel.refresh(); + } } \ No newline at end of file diff --git a/TicTacToeWS/src/main/java/com/tttws/TicTacToeWS.java b/TicTacToeWS/src/main/java/com/tttws/TicTacToeWS.java @@ -389,6 +389,7 @@ public class TicTacToeWS { */ @WebMethod(operationName = "showMyOpenGames") public String showMyOpenGames(@WebParam(name = "uid") int uid) { + System.out.println("Called showMyOpenGames()"); String sqlCmd = "SELECT g.autokey, u.username, g.started FROM games g, users u WHERE g.p1 = " + uid + " AND g.p2 IS NULL AND g.p1 = u.autokey ORDER BY g.started ASC;"; try { String result = dao.retrieve(sqlCmd);