Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 263641c7d215e5ee6e1a7cef615d3b580e8ce44c
parent 9dc0fbb285cb7ff6132f4806a2acb9c4a5be33b7
Author: adglas21 <adamglasheen99@gmail.com>
Date:   Mon,  2 Dec 2024 10:06:25 +0000

Updated register panel

-Fixed bug where empty user can create an account
-Fixed bug that 2 users with same username can create an account

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 2--
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java | 214++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 174 insertions(+), 42 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 @@ -297,8 +297,6 @@ public class MainContentPanel extends CustomPanel { timer.stop(); gameTimerLabel.setText("No opponent, please try again"); - //currently the game deletes when noone joins, however when the user makes a move and waits the game does not delete and is - //still present in the list of games String deleteGame = client.proxy.deleteGame(Integer.parseInt(client.GID), Integer.parseInt(client.UID)); System.out.println(deleteGame); diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/register.java @@ -1,16 +1,23 @@ package com.groupproject.tictactoeclient.ContentPanes; import com.groupproject.tictactoeclient.TicTacToeClient; +import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import java.awt.FlowLayout; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.Box; +import javax.swing.JCheckBox; +import javax.swing.JPasswordField; +import javax.swing.SpringLayout; +import javax.swing.JOptionPane; /** * @@ -18,58 +25,185 @@ import javax.swing.Box; */ public class register extends CustomPanel { public register(TicTacToeClient client) { - // Set the layout to BoxLayout for vertical alignment - setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); - - // Username - JPanel usernamePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); - JLabel usernameLabel = new JLabel("Username:"); - JTextField usernameField = new JTextField(15); - usernameField.setPreferredSize(new Dimension(600, 20)); - usernamePanel.add(usernameLabel); - usernamePanel.add(usernameField); - add(usernamePanel); - - // Password - JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); - JLabel passwordLabel = new JLabel("Password:"); - JTextField passwordField = new JTextField(15); - passwordField.setPreferredSize(new Dimension(600, 20)); - passwordPanel.add(passwordLabel); - passwordPanel.add(passwordField); - add(passwordPanel); - - // Name - JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); - JLabel nameLabel = new JLabel("Name:"); - JTextField nameField = new JTextField(10); // adjusting width - namePanel.add(nameLabel); - namePanel.add(nameField); - - // Surname - JLabel surnameLabel = new JLabel("Surname:"); - JTextField surnameField = new JTextField(10); - namePanel.add(surnameLabel); - namePanel.add(surnameField); + // Set panel layout to spring layout + SpringLayout layout = new SpringLayout(); + setLayout(layout); - add(namePanel); - - // Register button + //Create the login Button JButton registerButton = new JButton("Register"); - add(Box.createVerticalStrut(10)); - add(registerButton); + + //Create the username and password Labels + JLabel usernameLabel = new JLabel("username"); + JLabel passwordLabel = new JLabel("password"); + JLabel nameLabel = new JLabel("name"); + JLabel surnameLabel = new JLabel("surname"); + + + //Create the username and password text fields + JTextField usernameTextField = new JTextField(20); + JPasswordField passwordTextField = new JPasswordField(20); + JTextField nameTextField = new JTextField(20); + JTextField surnameTextField = new JTextField(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 registerErrorLabel = new JLabel("User is already created"); + registerErrorLabel.setForeground(Color.red); + registerErrorLabel.setVisible(false); + // Add components to panel + add(registerButton); + add(usernameLabel); + add(passwordLabel); + add(nameLabel); + add(surnameLabel); + add(usernameTextField); + add(passwordTextField); + add(nameTextField); + add(surnameTextField); + + add(showPassword); + add(backButton); + add(registerErrorLabel); + + + + + + // UsernameLabel constraints + layout.putConstraint(SpringLayout.EAST, usernameLabel, -20, SpringLayout.WEST, usernameTextField); + layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameLabel, 0, SpringLayout.VERTICAL_CENTER, usernameTextField); + + //Username textfield Constraint + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, usernameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameTextField, -60, SpringLayout.VERTICAL_CENTER, this); + + + //nameLabel constraints + layout.putConstraint(SpringLayout.EAST, nameLabel, -20, SpringLayout.WEST, nameTextField); + layout.putConstraint(SpringLayout.VERTICAL_CENTER, nameLabel, 0, SpringLayout.VERTICAL_CENTER, nameTextField); + + //name textfield constraints + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, nameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.NORTH, nameTextField, 20, SpringLayout.SOUTH, usernameTextField); + + + //surnameLabel constraints + layout.putConstraint(SpringLayout.EAST, surnameLabel, -20, SpringLayout.WEST, surnameTextField); + layout.putConstraint(SpringLayout.VERTICAL_CENTER, surnameLabel, 0, SpringLayout.VERTICAL_CENTER, surnameTextField); + + + //surname textfield constraints + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, surnameTextField, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.NORTH, surnameTextField, 20, SpringLayout.SOUTH, nameTextField); + + + + + //PasswordLabel constraints + layout.putConstraint(SpringLayout.EAST, passwordLabel, -20, SpringLayout.WEST, passwordTextField); + layout.putConstraint(SpringLayout.VERTICAL_CENTER, passwordLabel, 0, SpringLayout.VERTICAL_CENTER, passwordTextField); + + //Password textfield constraint + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, passwordTextField, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.NORTH, passwordTextField, 20, SpringLayout.SOUTH, surnameTextField); + + //Show password checkbox constraint + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, showPassword, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.NORTH, showPassword, 20, SpringLayout.SOUTH, passwordTextField); + + //Register Button constraint + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, registerButton, 0, SpringLayout.HORIZONTAL_CENTER, this); + layout.putConstraint(SpringLayout.NORTH, registerButton, 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, registerErrorLabel, -20, SpringLayout.EAST, this); + layout.putConstraint(SpringLayout.NORTH, registerErrorLabel, 20, SpringLayout.NORTH, this); + + // Action listener for register button registerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - client.UID = client.proxy.register(usernameField.getText(), passwordField.getText(), nameField.getText(), surnameField.getText()); - client.username = usernameField.getText(); + + //get the text from each textfield + String username = usernameTextField.getText().trim(); + String name = nameTextField.getText().trim(); + String surname = surnameTextField.getText().trim(); + String password = passwordTextField.getText().trim(); + + //check if any of the fields are empty to prevent a blank user being created + if(username.isEmpty() || name.isEmpty() || surname.isEmpty() || password.isEmpty()) + { + JOptionPane.showMessageDialog(client.frame, "Please fill in all fields"); + return; + } + + + String response = client.proxy.register(usernameTextField.getText(), passwordTextField.getText(), nameTextField.getText(), surnameTextField.getText()); + System.out.println(response); + + //check if the username already exists, if not allow user to register + if ("ERROR-REPEAT".equals(response)) { + JOptionPane.showMessageDialog(client.frame, "User already exists"); + return; + } + else { + client.UID = client.proxy.register(usernameTextField.getText(), passwordTextField.getText(), nameTextField.getText(), surnameTextField.getText()); + client.username = usernameTextField.getText(); System.out.println(client.UID); 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); + } else { + passwordTextField.setEchoChar(defaultPassword); + } + } + }); + + // Back button listener + backButton.addActionListener( new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + client.showPanel(new StartPanel(client)); + } + }); + + + + + + + + + + + } @Override