Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 61914f0c2ab4dda50bf2e12b75e6c5fea8faf313
parent 404280bb628c684c60e2b91ef206b936a4f12bb0
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 11 Nov 2024 13:12:35 +0000

Added logic for login and register

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 2+-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 2+-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java | 64+++++++++++++++++++++++++++++++++++++++++++++++-----------------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java | 4+++-
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 9+++++++--
5 files changed, 59 insertions(+), 22 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 @@ -14,7 +14,7 @@ import javax.swing.SpringLayout; /** * - * @author Willi + * @author William * Contains the content visible in the main program. */ public class MainContentPanel extends JPanel { diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java @@ -12,7 +12,7 @@ import javax.swing.border.LineBorder; /** * - * @author Willi + * @author William */ public class TicTacToeGrid extends JPanel { JButton[][] grid = new JButton[3][3]; diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java @@ -4,6 +4,7 @@ */ package com.groupproject.tictactoeclient.ContentPanes; import com.groupproject.tictactoeclient.TicTacToeClient; +import java.awt.Color; import java.awt.event.*; @@ -30,15 +31,26 @@ public class login extends JPanel { JTextField usernameTextField = new JTextField(20); JPasswordField passwordTextField = new JPasswordField(20); + // Back button + JButton backButton = new JButton("Back"); + //Create a checkbox to show the password JCheckBox showPassword = new JCheckBox("show password"); + //Create error message if login is not correct + JLabel loginErrorLabel = new JLabel("Wrong username or password"); + loginErrorLabel.setForeground(Color.red); + loginErrorLabel.setVisible(false); + + // Add components to panel add(loginButton); add(usernameLabel); add(passwordLabel); add(usernameTextField); add(passwordTextField); add(showPassword); + add(backButton); + add(loginErrorLabel); // UsernameLabel constraints @@ -65,33 +77,51 @@ public class login extends JPanel { layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, loginButton, 0, SpringLayout.HORIZONTAL_CENTER, this); layout.putConstraint(SpringLayout.NORTH, loginButton, 20, SpringLayout.SOUTH, showPassword); + //Back button constraint + layout.putConstraint(SpringLayout.WEST, backButton, 20, SpringLayout.WEST, this); + layout.putConstraint(SpringLayout.NORTH, backButton, 20, SpringLayout.NORTH, this); + + //Error label constraint + layout.putConstraint(SpringLayout.EAST, loginErrorLabel, -20, SpringLayout.EAST, this); + layout.putConstraint(SpringLayout.NORTH, loginErrorLabel, 20, SpringLayout.NORTH, this); - // Login Button Action - check the entries of the textFields + + // Login Button Action - check the entries of the textFields loginButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - client.showPanel(new login(client)); - - + @Override + public void actionPerformed(ActionEvent e) { + client.UID = String.valueOf(client.proxy.login(usernameTextField.getText(), String.valueOf(passwordTextField.getPassword()))); + System.out.println("UID = " + client.UID); + + if (Integer.parseInt(client.UID) == -1) { + loginErrorLabel.setVisible(true); + } else { + client.showPanel(new MainContentPanel(client)); + } } - }); + }); //Item listener to check if the showPassword checkbox has been selected //If selected show the password and if not display the password as **** normal char defaultPassword = passwordTextField.getEchoChar(); // Login Button Action - check the entries of the textFields showPassword.addItemListener(new ItemListener() { - public void itemStateChanged(ItemEvent e) { - if (e.getStateChange() == ItemEvent.SELECTED) { - passwordTextField.setEchoChar((char) 0); + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + passwordTextField.setEchoChar((char) 0); } else { - passwordTextField.setEchoChar(defaultPassword); - } - } - }); - - - + passwordTextField.setEchoChar(defaultPassword); + } + } + }); + + // Back button listener + backButton.addActionListener( new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + client.showPanel(new StartPanel(client)); + } + }); } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java @@ -64,7 +64,9 @@ public class register extends JPanel { registerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - client.showPanel(new StartPanel(client)); + client.UID = client.proxy.register(usernameField.getText(), passwordField.getText(), nameField.getText(), surnameField.getText()); + System.out.println(client.UID); + client.showPanel(new MainContentPanel(client)); } }); } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -18,13 +18,14 @@ import javax.swing.UnsupportedLookAndFeelException; /** * - * @author Willi + * @author William */ public class TicTacToeClient { private static TicTacToeClient client; private static TicTacToeWebService service; - private static TicTacToeWS proxy; + public static TicTacToeWS proxy; private JFrame frame; + public String UID; public static void main(String[] args) { // Enable flatlaf dark theme @@ -39,6 +40,10 @@ public class TicTacToeClient { ); } + // Initialize SOAP interface + service = new TicTacToeWebService(); + proxy = service.getTicTacToeWSPort(); + // Initalize UI client = new TicTacToeClient(); }