Skip to content
Snippets Groups Projects
Commit 897c112c authored by Samita Adhikari's avatar Samita Adhikari
Browse files

User registeration form and form submit. Hoever, it has a bug and fails to...

User registeration form and form submit. Hoever, it has a bug and fails to render the registration form
parent 72f0dcd1
Branches
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
package brickbreaker;
import Auth.User;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
......@@ -14,6 +15,8 @@ public class App {
public static GamePanel gamePanel;
public static ScoringSystem scoreSystem = new ScoringSystem();
public static JFrame frame;
public static HighScores highScorePanel;
public static Register registrationPanel;
public static void main(String[] args) {
frame = new JFrame();
......@@ -36,4 +39,13 @@ public class App {
public static void createMainMenu() {
App.mainMenu = new Menu(frame);
}
public static void createHighScorePanel() {
List<Integer> scores = scoreSystem.getScores();
App.highScorePanel = new HighScores(scores);
}
public static void createRegistrationPanel() {
App.registrationPanel = new Register(frame);
}
}
package brickbreaker;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class HighScores extends JPanel {
private List<Integer> scores;
public HighScores(List<Integer> scores) {
this.scores = scores;
initializeUI();
}
public void setScores(List<Integer> scores) {
this.scores = scores;
}
private void initializeUI() {
setLayout(new BorderLayout());
// Create a JLabel to display the title
JLabel titleLabel = new JLabel("High Scores");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setHorizontalAlignment(JLabel.CENTER);
add(titleLabel, BorderLayout.NORTH);
// Create a JTable to display the scores
JTable scoresTable = new JTable() {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Disable cell editing
}
};
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("S/N");
tableModel.addColumn("Score");
int rank = 1;
for (int score : scores) {
tableModel.addRow(new Object[]{rank++, score});
}
scoresTable.setModel(tableModel);
scoresTable.setFont(new Font("Arial", Font.PLAIN, 16));
scoresTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
scoresTable.setRowHeight(30);
// Add the scoresTable to a JScrollPane for scrolling
JScrollPane scrollPane = new JScrollPane(scoresTable);
add(scrollPane, BorderLayout.CENTER);
JButton backButton = new JButton("Back");
backButton.setFont(new Font("Arial", Font.PLAIN, 16));
// Add an ActionListener to handle the back button click
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
App.frame.remove(HighScores.this);
App.mainMenu.show();
App.frame.revalidate();
App.frame.repaint();
}
});
// Add the back button to the bottom of the panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(backButton);
add(buttonPanel, BorderLayout.SOUTH);
}
}
......@@ -112,6 +112,8 @@ public class Menu extends JPanel {
}
});
guest.setBackground(new java.awt.Color(36, 172, 226));
guest.setFont(new java.awt.Font("MV Boli", 0, 14)); // NOI18N
guest.setForeground(new java.awt.Color(255, 255, 255));
......@@ -181,18 +183,31 @@ public class Menu extends JPanel {
}
private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
frame.remove(this);
if(App.registrationPanel == null)
App.createRegistrationPanel();
App.registrationPanel.show();
}
private void guestActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
frame.remove(this);
if(App.gamePanel == null)
App.createGame();
frame.add(App.gamePanel);
frame.revalidate();
frame.repaint();
}
private void highScoreActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
frame.remove(this);
if(App.highScorePanel == null)
App.createHighScorePanel();
frame.add(App.highScorePanel);
// Refresh and repaint
frame.revalidate();
frame.repaint();
}
private void navigateToLogin() {
......
package brickbreaker;
import Auth.Authenticate;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Register extends JPanel {
private JTextField usernameField;
private JTextField emailField;
private JPasswordField passwordField;
private JButton registerButton;
private JButton backButton;
private JLabel headerLabel, usernameLabel, emailLabel, passwordLabel;
private JFrame parentFrame;
private Color buttonColor = new Color(36, 172, 226);
private Color hoverButtonColor = new Color(26, 152, 206);
private Font textFont = new Font("MV Boli", Font.PLAIN, 14);
private Font labelFont = new Font("MV Boli", Font.BOLD, 16);
private Font headerFont = new Font("MV Boli", Font.BOLD, 24);
private JLabel usernameErrorLabel = new JLabel("");
private JLabel emailErrorLabel = new JLabel("");
private JLabel passwordErrorLabel = new JLabel("");
private Authenticate fileAuth = new Authenticate();
public Register(JFrame parentFrame) {
this.parentFrame = parentFrame;
initComponents();
setBackground(new Color(240, 240, 255)); // Light blue background
}
private void initComponents() {
headerLabel = new JLabel("Registration");
headerLabel.setFont(headerFont);
headerLabel.setForeground(buttonColor);
usernameLabel = new JLabel("Username:");
emailLabel = new JLabel("Email:");
passwordLabel = new JLabel("Password:");
usernameField = new JTextField(20);
emailField = new JTextField(20);
passwordField = new JPasswordField(20);
registerButton = new JButton("Register");
backButton = new JButton("Back");
usernameLabel.setFont(labelFont);
emailLabel.setFont(labelFont);
passwordLabel.setFont(labelFont);
usernameField.setFont(textFont);
emailField.setFont(textFont);
passwordField.setFont(textFont);
registerButton.setFont(new Font("MV Boli", Font.BOLD, 16));
backButton.setFont(new Font("MV Boli", Font.BOLD, 16));
registerButton.setBackground(buttonColor);
registerButton.setForeground(Color.WHITE);
backButton.setBackground(buttonColor);
backButton.setForeground(Color.WHITE);
registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
usernameErrorLabel.setPreferredSize(new Dimension(200, 20));
emailErrorLabel.setPreferredSize(new Dimension(200, 20));
passwordErrorLabel.setPreferredSize(new Dimension(200, 20));
usernameErrorLabel.setForeground(Color.RED);
emailErrorLabel.setForeground(Color.RED);
passwordErrorLabel.setForeground(Color.RED);
// Hover effects for buttons
registerButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
registerButton.setBackground(hoverButtonColor);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
registerButton.setBackground(buttonColor);
}
});
backButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
backButton.setBackground(hoverButtonColor);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
backButton.setBackground(buttonColor);
}
});
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
handleRegistration();
}
});
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
handleBack();
}
});
// Layout with padding and alignment
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 0;
add(headerLabel, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
add(usernameLabel, gbc);
gbc.gridx = 1;
add(usernameField, gbc);
gbc.gridx = 2;
add(usernameErrorLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
add(emailLabel, gbc);
gbc.gridx = 1;
add(emailField, gbc);
gbc.gridx = 2;
add(emailErrorLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
add(passwordLabel, gbc);
gbc.gridx = 1;
add(passwordField, gbc);
gbc.gridx = 2;
add(passwordErrorLabel, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 4;
add(registerButton, gbc);
gbc.gridy = 5;
add(backButton, gbc);
setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
}
private void handleRegistration() {
// Reset error labels
usernameErrorLabel.setText("");
emailErrorLabel.setText("");
passwordErrorLabel.setText("");
String username = usernameField.getText();
String email = emailField.getText();
String password = new String(passwordField.getPassword());
boolean error = false;
// Simple validation
if (username.isEmpty()) {
usernameErrorLabel.setText("Username is required!");
error = true;
}
if (email.isEmpty()) {
emailErrorLabel.setText("Email is required!");
error = true;
} else if (!isValidEmail(email)) {
emailErrorLabel.setText("Invalid email format!");
error = true;
}
if (password.isEmpty()) {
passwordErrorLabel.setText("Password is required!");
error = true;
}
if (error) return;
// You can implement the registration logic here
// For example, call a method to register the user
try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) {
// Define the SQL query to insert registration data
String sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
// Create a prepared statement
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, username);
preparedStatement.setString(2, email);
preparedStatement.setString(3, password);
// Execute the insert query
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
JOptionPane.showMessageDialog(this, "Registration successful! You may now Login", "Success", JOptionPane.INFORMATION_MESSAGE);
handleBack(); // Go back to the login screen after successful registration
} else {
JOptionPane.showMessageDialog(this, "Registration failed. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error. Registration failed. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void handleBack() {
parentFrame.remove(this);
App.mainMenu.show();
}
public void show() {
App.frame.add(this);
App.frame.revalidate();
App.frame.repaint();
}
public static boolean isValidEmail(String email) {
String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}
......@@ -5,6 +5,8 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ScoringSystem {
......@@ -59,6 +61,51 @@ public class ScoringSystem {
return 0;
}
public List<Integer> getScores() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Integer> scores = new ArrayList<>();
if (App.user == null) return scores;
try {
// Establish a database connection
connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD);
// Define the SQL query to retrieve scores for the user
String sql = "SELECT score FROM scores WHERE user_id=? ORDER BY id DESC";
// Create a prepared statement
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, App.user.id);
// Execute the query and retrieve the results
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
// Read each score from the result set and add it to the list
int score = resultSet.getInt("score");
scores.add(score);
}
} catch (SQLException e) {
System.err.println("Database Error: " + e.getMessage());
} finally {
// Close database resources in reverse order of acquisition
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
System.err.println("Error closing database resources: " + e.getMessage());
}
}
// Return the list of scores
return scores;
}
private void saveHighScore() {
if(App.user == null) return;
try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment