diff --git a/app/bin/main/Auth/Authenticate.class b/app/bin/main/Auth/Authenticate.class new file mode 100644 index 0000000000000000000000000000000000000000..9d11fe02a53d29cff70712d76a60459d854c4a5d Binary files /dev/null and b/app/bin/main/Auth/Authenticate.class differ diff --git a/app/bin/main/brickbreaker/App.class b/app/bin/main/brickbreaker/App.class index f13098a0863b3710e9822c487f35c5d3554fe3dc..8cace8824ecd09e79d77dc4de863b9861aa27d91 100644 Binary files a/app/bin/main/brickbreaker/App.class and b/app/bin/main/brickbreaker/App.class differ diff --git a/app/bin/main/brickbreaker/GamePanel$1.class b/app/bin/main/brickbreaker/GamePanel$1.class index 8421a1fd7f25a943e1207fcf088755b10f1a4044..acfdd048ec7ef79bb90d7d467ca3b2bfe4aab472 100644 Binary files a/app/bin/main/brickbreaker/GamePanel$1.class and b/app/bin/main/brickbreaker/GamePanel$1.class differ diff --git a/app/bin/main/brickbreaker/GamePanel$2.class b/app/bin/main/brickbreaker/GamePanel$2.class index ed5650947f630bb2f1ffef5792565667013f069a..d75f5e33e45e63f19b7cc5443982ab845f58cbd3 100644 Binary files a/app/bin/main/brickbreaker/GamePanel$2.class and b/app/bin/main/brickbreaker/GamePanel$2.class differ diff --git a/app/bin/main/brickbreaker/GamePanel$3.class b/app/bin/main/brickbreaker/GamePanel$3.class index a1d6dea2d22ea9a08b095013d2ad0c2d33e33d62..27750c33eac6f1c9fa7100ada6acdbf682a5271f 100644 Binary files a/app/bin/main/brickbreaker/GamePanel$3.class and b/app/bin/main/brickbreaker/GamePanel$3.class differ diff --git a/app/bin/main/brickbreaker/GamePanel.class b/app/bin/main/brickbreaker/GamePanel.class index 1b9496e8de4716ab5c577a91a1108c2d9060aeab..d2cab2d02e3d1ccf8f0ccc1437f1b625d1bbb109 100644 Binary files a/app/bin/main/brickbreaker/GamePanel.class and b/app/bin/main/brickbreaker/GamePanel.class differ diff --git a/app/bin/main/brickbreaker/LoginFrame$1.class b/app/bin/main/brickbreaker/LoginFrame$1.class new file mode 100644 index 0000000000000000000000000000000000000000..382f95c9d2152b82cdfe8936829c743fda3b9e14 Binary files /dev/null and b/app/bin/main/brickbreaker/LoginFrame$1.class differ diff --git a/app/bin/main/brickbreaker/LoginFrame$2.class b/app/bin/main/brickbreaker/LoginFrame$2.class new file mode 100644 index 0000000000000000000000000000000000000000..b303f091587d2359facd2ef8188a175837ff3a1c Binary files /dev/null and b/app/bin/main/brickbreaker/LoginFrame$2.class differ diff --git a/app/bin/main/brickbreaker/LoginFrame.class b/app/bin/main/brickbreaker/LoginFrame.class index 012d86a8df1bef8d703b901d7ca201b98df8516c..627b26806f4310de5a0aaf3530c940de005213ea 100644 Binary files a/app/bin/main/brickbreaker/LoginFrame.class and b/app/bin/main/brickbreaker/LoginFrame.class differ diff --git a/app/bin/main/brickbreaker/Menu$1.class b/app/bin/main/brickbreaker/Menu$1.class index 95053ba712d0b6fdd168b6ce75009cff0a90a813..8ccd6e2b0c6789e5df232a42a0ca0cc96a4ad5eb 100644 Binary files a/app/bin/main/brickbreaker/Menu$1.class and b/app/bin/main/brickbreaker/Menu$1.class differ diff --git a/app/bin/main/brickbreaker/Menu$2.class b/app/bin/main/brickbreaker/Menu$2.class index 68df9309fcc0d2cd2b8f0c398570e563d1dff986..7b51574bcd850f51f9e0d7e4b060e397620d596d 100644 Binary files a/app/bin/main/brickbreaker/Menu$2.class and b/app/bin/main/brickbreaker/Menu$2.class differ diff --git a/app/bin/main/brickbreaker/Menu$3.class b/app/bin/main/brickbreaker/Menu$3.class index e1f98e2eae7ea3655926c25a6e886e20c679186c..f704c0403e9ec14722778141dc7936146c833635 100644 Binary files a/app/bin/main/brickbreaker/Menu$3.class and b/app/bin/main/brickbreaker/Menu$3.class differ diff --git a/app/bin/main/brickbreaker/Menu$4.class b/app/bin/main/brickbreaker/Menu$4.class index 3c99d6aa4fe10550de84c77b2170d9eb0aa0ec53..0bfd40a606ae99a0e9b915689e468a86a6c3bdcb 100644 Binary files a/app/bin/main/brickbreaker/Menu$4.class and b/app/bin/main/brickbreaker/Menu$4.class differ diff --git a/app/bin/main/brickbreaker/Menu$5.class b/app/bin/main/brickbreaker/Menu$5.class deleted file mode 100644 index 994b65eeed87fb9149c33b39cc53280046e19a4c..0000000000000000000000000000000000000000 Binary files a/app/bin/main/brickbreaker/Menu$5.class and /dev/null differ diff --git a/app/bin/main/brickbreaker/Menu.class b/app/bin/main/brickbreaker/Menu.class index 0e10f4f6427890986fdd776258506ebc93337e0e..2e8216aaec1c650f58276e3247543fe0529e22d1 100644 Binary files a/app/bin/main/brickbreaker/Menu.class and b/app/bin/main/brickbreaker/Menu.class differ diff --git a/app/bin/main/brickbreaker/ScoringSystem.class b/app/bin/main/brickbreaker/ScoringSystem.class new file mode 100644 index 0000000000000000000000000000000000000000..723fc5f1b464eaef8df21a6e717a5ed519898265 Binary files /dev/null and b/app/bin/main/brickbreaker/ScoringSystem.class differ diff --git a/app/bin/main/highscore.txt b/app/bin/main/highscore.txt new file mode 100644 index 0000000000000000000000000000000000000000..573541ac9702dd3969c9bc859d2b91ec1f7e6e56 --- /dev/null +++ b/app/bin/main/highscore.txt @@ -0,0 +1 @@ +0 diff --git a/app/src/main/java/resources/user.txt b/app/bin/main/resources/user.txt similarity index 100% rename from app/src/main/java/resources/user.txt rename to app/bin/main/resources/user.txt diff --git a/app/bin/main/user.txt b/app/bin/main/user.txt new file mode 100644 index 0000000000000000000000000000000000000000..e80d5a7d39479fb0c96b392c846dfa05cdc16efb --- /dev/null +++ b/app/bin/main/user.txt @@ -0,0 +1 @@ +muna:2022 \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 55f0d4d95085316d4a49c0e4dbad8a33c875e25a..b8c6a0de5bbc94f632fa75a450bd3c14665f2ca6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,7 @@ dependencies { // This dependency is used by the application. implementation 'com.google.guava:guava:30.1.1-jre' + implementation 'mysql:mysql-connector-java:8.0.28' } application { diff --git a/app/src/main/java/Auth/Authenticate.java b/app/src/main/java/Auth/Authenticate.java index 24f86a38e40df048b981f5b4344c4068d8285522..f5640306663f79d639b2bbd26b77e523845dc9bf 100644 --- a/app/src/main/java/Auth/Authenticate.java +++ b/app/src/main/java/Auth/Authenticate.java @@ -3,43 +3,43 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package Auth; +import brickbreaker.App; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; public class Authenticate { - - private static final String CREDENTIALS_FILE = "user.txt"; - - private Map<String, String> users = new HashMap<>(); - - public Authenticate() { - loadCredentials(); - } - - private void loadCredentials() { - InputStream is = getClass().getClassLoader().getResourceAsStream(CREDENTIALS_FILE); - - try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(":"); - if (parts.length == 2) { - users.put(parts[0], parts[1]); + public boolean login(String email, String password) { + try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) { + String sql = "SELECT * FROM users WHERE email = ? AND password = ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, email); + preparedStatement.setString(2, password); + ResultSet resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + // User exists, return user details + int id = resultSet.getInt("id"); + String name = resultSet.getString("username"); + String fetchedEmail = resultSet.getString("email"); + User user = new User(id, name, fetchedEmail); + App.user = user; + return true; } } - } catch (IOException e) { - e.printStackTrace(); + } catch (SQLException e) { + System.err.println("Database Error: " + e.getMessage()); } - } - - public boolean authenticate(String username, String password) { - return password.equals(users.get(username)); + return false; } diff --git a/app/src/main/java/Auth/User.java b/app/src/main/java/Auth/User.java new file mode 100644 index 0000000000000000000000000000000000000000..3bec6bf4fb5054ef300b74fdfe2244d127116877 --- /dev/null +++ b/app/src/main/java/Auth/User.java @@ -0,0 +1,44 @@ +/* + * 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 Auth; + +import brickbreaker.App; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * + * @author Suman + */ +public class User { + public int id; + public String name; + public String email; + + public User(int id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + public Boolean register(String password) { + try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) { + String sql = "INSERT INTO users (name, email, password) VALUES (?, ?, ?)"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, name); + preparedStatement.setString(2, email); + preparedStatement.setString(3, password); + + int rowsAffected = preparedStatement.executeUpdate(); + return rowsAffected > 0; // Registration successful if rows are inserted + } + } catch (SQLException e) { + System.err.println("Database Error: " + e.getMessage()); + return false; // Registration failed due to an error + } + } +} diff --git a/app/src/main/java/brickbreaker/App.java b/app/src/main/java/brickbreaker/App.java index a0bcd2520848b9abc09c1b386c7ed8e4b0e94c2f..c73fede56e73eb760fca7ec88f843a36325dfe76 100644 --- a/app/src/main/java/brickbreaker/App.java +++ b/app/src/main/java/brickbreaker/App.java @@ -1,10 +1,19 @@ package brickbreaker; +import Auth.User; import javax.swing.JFrame; import javax.swing.JPanel; public class App { + public static User user; + public static final String DB_URL = "jdbc:mysql://localhost:3306/brick_breaker"; + public static final String DB_USER = "root"; + public static final String DB_PASSWORD = ""; + public static Menu mainMenu; + public static GamePanel gamePanel; + public static ScoringSystem scoreSystem = new ScoringSystem(); + public static void main(String[] args) { JFrame frame = new JFrame(); @@ -13,12 +22,11 @@ public class App { frame.setTitle("Brick Breaker"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - new Menu(frame); - - // Call pack() after adding components + mainMenu = new Menu(frame); + gamePanel = new GamePanel(frame); + frame.pack(); - // Then, set the frame visible frame.setVisible(true); } } diff --git a/app/src/main/java/brickbreaker/GamePanel.java b/app/src/main/java/brickbreaker/GamePanel.java index 7866786b274d5bac8b092168ce54f43d4ebc174b..a3fa4f37ce14fd0374d7671c578c1337baf692de 100644 --- a/app/src/main/java/brickbreaker/GamePanel.java +++ b/app/src/main/java/brickbreaker/GamePanel.java @@ -2,7 +2,6 @@ package brickbreaker; import brickbreaker.GameComponents.Brick; import brickbreaker.Interfaces.BrickFactory; -import brickbreaker.Interfaces.GameObserver; import brickbreaker.GameComponents.DefaultBrickFactory; import javax.swing.*; import java.awt.*; @@ -16,7 +15,7 @@ import java.util.List; public class GamePanel extends JPanel implements ActionListener { private boolean play = false; private boolean gameOver = false; - private int score = 0; + private ScoringSystem scoringSystem; private int totalBricks; private int rows = 10; @@ -29,34 +28,25 @@ public class GamePanel extends JPanel implements ActionListener { private List<Ball> balls = new ArrayList<>(); private String font = "MV Boli"; - + private JFrame frame; private GameMapBuilder gameMap; public GamePanel(JFrame frame) { Color color = Color.decode("#8B4513"); this.balls.add(new Ball(350, 450, 2, -2, 20, color)); this.totalBricks = rows * columns; - + this.frame = frame; BrickFactory brickfactory = new DefaultBrickFactory(); gameMap = new GameMapBuilder(rows, columns, brickfactory); setFocusable(true); setFocusTraversalKeysEnabled(false); timer = new Timer(delay, this); timer.start(); - frame.add(this); addKeyBindings(); + + scoringSystem = App.scoreSystem; } - private List<GameObserver> observers = new ArrayList<>(); - - public void addObserver(GameObserver observer) { - observers.add(observer); - } - - public void removeObserver(GameObserver observer) { - observers.remove(observer); - } - private void addKeyBindings() { InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = getActionMap(); @@ -89,13 +79,13 @@ public class GamePanel extends JPanel implements ActionListener { actionMap.put("Enter", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (!play || !gameOver) { + if (!play || gameOver) { play = true; gameOver = false; Color color = Color.decode("#8B4513"); balls.clear(); balls.add(new Ball(350, 450, 2, -2, 20, color)); - score = 0; + scoringSystem.resetScore(); totalBricks = rows * columns; BrickFactory brickfactory = new DefaultBrickFactory(); gameMap = new GameMapBuilder(rows, columns, brickfactory); @@ -104,6 +94,18 @@ public class GamePanel extends JPanel implements ActionListener { } } }); + + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ESC"); + JPanel thisPanel = this; + actionMap.put("ESC", new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + frame.remove(thisPanel); + App.mainMenu.show(); + frame.revalidate(); + frame.repaint(); + } + }); } protected void paintComponent(Graphics graphics) { @@ -146,8 +148,12 @@ public class GamePanel extends JPanel implements ActionListener { graphics.setColor(Color.black); graphics.setFont(new Font("MV Boli", Font.BOLD, 25)); - graphics.drawString("Score: " + score, 520, 30); + graphics.drawString("Score: " + scoringSystem.getScore(), 520, 30); + // Display the high score + graphics.setColor(Color.black); + graphics.setFont(new Font("MV Boli", Font.BOLD, 25)); + graphics.drawString("High Score: " + scoringSystem.getHighScore(), 10, 30); if (totalBricks <= 0) { Color color = new Color(0XFF6464); gameOver(graphics, "You Won", color); @@ -163,19 +169,12 @@ public class GamePanel extends JPanel implements ActionListener { gameOver = true; // Set the game over flag to true graphics.setColor(color); graphics.setFont(new Font(font, Font.BOLD, 30)); - graphics.drawString(gameOverText + ", Your Score: " + score, 190, 300); + graphics.drawString(gameOverText + ", Your Score: " + scoringSystem.getScore(), 190, 300); graphics.setFont(new Font(font, Font.BOLD, 20)); - graphics.drawString("Press Enter to Restart", 230, 350); - - // Notify observers about the game end - for (GameObserver observer : observers) { - if ("Game Over".equals(gameOverText)) { - observer.onGameOver(score); - } else if ("You Won".equals(gameOverText)) { - observer.onGameWin(score); - } - } + graphics.drawString("Press Enter to Restart or ESC to go back to main Menu", 100, 350); + + scoringSystem.checkForNewHighScore(); } @Override @@ -188,6 +187,7 @@ public class GamePanel extends JPanel implements ActionListener { Vector ballPosition = ball.getPosition(); Vector ballDirection = ball.getDirection(); + // Check ball and paddle collision if (new Rectangle(ballPosition.x, ballPosition.y, ball.radius, ball.radius).intersects(new Rectangle(paddle, 550, 100, 8))) { ballDirection.y = -ballDirection.y; ball.setDirection(ballDirection.x, ballDirection.y); @@ -205,7 +205,7 @@ public class GamePanel extends JPanel implements ActionListener { if (ballRect.intersects(brickRect)) { brick.setIsHit(true); totalBricks--; - score += brick.getValue(); + scoringSystem.brickBroken(brick.getValue()); if(brick.hasPowerUp) { int noOfBallsToSpawn = (int) (Math.random() * 5); @@ -248,6 +248,11 @@ public class GamePanel extends JPanel implements ActionListener { play = true; paddle -= 50; } - + + public void show() { + frame.add(this); + frame.revalidate(); + frame.repaint(); + } } diff --git a/app/src/main/java/brickbreaker/LoginFrame.java b/app/src/main/java/brickbreaker/LoginFrame.java index b172ae18fa6584f0018415e78fede59e0db1c484..69fe41bd364433372ab2b1a1081bdfe50c688126 100644 --- a/app/src/main/java/brickbreaker/LoginFrame.java +++ b/app/src/main/java/brickbreaker/LoginFrame.java @@ -10,9 +10,11 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class LoginFrame extends JPanel { - private JTextField usernameField; + private JTextField emailField; private JPasswordField passwordField; private JButton submitButton; private JButton backButton; @@ -23,7 +25,7 @@ public class LoginFrame extends JPanel { 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 LoginFrame(JFrame parentFrame) { @@ -38,10 +40,10 @@ public class LoginFrame extends JPanel { headerLabel.setFont(headerFont); headerLabel.setForeground(buttonColor); - usernameLabel = new JLabel("Username:"); + usernameLabel = new JLabel("Email:"); passwordLabel = new JLabel("Password:"); - usernameField = new JTextField(20); + emailField = new JTextField(20); passwordField = new JPasswordField(20); submitButton = new JButton("Login"); @@ -49,7 +51,7 @@ public class LoginFrame extends JPanel { usernameLabel.setFont(labelFont); passwordLabel.setFont(labelFont); - usernameField.setFont(textFont); + emailField.setFont(textFont); passwordField.setFont(textFont); submitButton.setFont(new Font("MV Boli", Font.BOLD, 16)); backButton.setFont(new Font("MV Boli", Font.BOLD, 16)); @@ -62,9 +64,9 @@ public class LoginFrame extends JPanel { submitButton.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 @@ -108,10 +110,10 @@ public class LoginFrame extends JPanel { add(usernameLabel, gbc); gbc.gridx = 1; - add(usernameField, gbc); + add(emailField, gbc); gbc.gridx = 2; - add(usernameErrorLabel, gbc); + add(emailErrorLabel, gbc); gbc.gridx = 0; gbc.gridy = 2; @@ -136,35 +138,48 @@ public class LoginFrame extends JPanel { private void handleLogin() { // 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!"); - return; + 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!"); - return; + error = true; } - - if (fileAuth.authenticate(username, password)) { - // User found - // Proceed with the application or show the next screen - System.out.println("Login successful!"); + + if(error) return; + Authenticate auth = new Authenticate(); + + if (auth.login(email, password)) { + App.scoreSystem.setHighScore(); + parentFrame.remove(this); + App.gamePanel.show(); } else { - // User not found JOptionPane.showMessageDialog(this, "Invalid username or password!", "Error", JOptionPane.ERROR_MESSAGE); } } private void handleBack() { - // TODO: Implement the back navigation logic + parentFrame.remove(this); + App.mainMenu.show(); + } + + 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(); } } diff --git a/app/src/main/java/brickbreaker/MainMenu.form b/app/src/main/java/brickbreaker/MainMenu.form deleted file mode 100644 index caedbec2e6763e9ffbbe70853490568af6b2d599..0000000000000000000000000000000000000000 --- a/app/src/main/java/brickbreaker/MainMenu.form +++ /dev/null @@ -1,192 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> - -<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> - <Properties> - <Property name="defaultCloseOperation" type="int" value="3"/> - <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="ff" green="ff" red="ff" type="rgb"/> - </Property> - </Properties> - <SyntheticProperties> - <SyntheticProperty name="formSizePolicy" type="int" value="1"/> - <SyntheticProperty name="generateCenter" type="boolean" value="false"/> - </SyntheticProperties> - <AuxValues> - <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> - <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> - <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> - <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> - <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> - <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> - <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> - <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> - <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> - </AuxValues> - - <Layout> - <DimensionLayout dim="0"> - <Group type="103" groupAlignment="0" attributes="0"> - <Group type="102" attributes="0"> - <Group type="103" groupAlignment="0" attributes="0"> - <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="310" max="-2" attributes="0"/> - <Component id="loginButton" min="-2" max="-2" attributes="0"/> - </Group> - <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="301" max="-2" attributes="0"/> - <Component id="registerButton" min="-2" max="-2" attributes="0"/> - </Group> - <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="282" max="-2" attributes="0"/> - <Component id="guest" min="-2" max="-2" attributes="0"/> - </Group> - <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="290" max="-2" attributes="0"/> - <Component id="highScore" min="-2" max="-2" attributes="0"/> - </Group> - <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="122" max="-2" attributes="0"/> - <Component id="jScrollPane1" min="-2" max="-2" attributes="0"/> - </Group> - </Group> - <EmptySpace pref="143" max="32767" attributes="0"/> - </Group> - </Group> - </DimensionLayout> - <DimensionLayout dim="1"> - <Group type="103" groupAlignment="0" attributes="0"> - <Group type="102" alignment="1" attributes="0"> - <EmptySpace min="-2" pref="117" max="-2" attributes="0"/> - <Component id="jScrollPane1" min="-2" max="-2" attributes="0"/> - <EmptySpace type="separate" max="-2" attributes="0"/> - <Component id="loginButton" max="-2" attributes="0"/> - <EmptySpace type="separate" max="-2" attributes="0"/> - <Component id="registerButton" max="-2" attributes="0"/> - <EmptySpace type="separate" max="-2" attributes="0"/> - <Component id="guest" max="-2" attributes="0"/> - <EmptySpace type="separate" max="-2" attributes="0"/> - <Component id="highScore" max="-2" attributes="0"/> - <EmptySpace pref="228" max="32767" attributes="0"/> - </Group> - </Group> - </DimensionLayout> - </Layout> - <SubComponents> - <Component class="javax.swing.JButton" name="loginButton"> - <Properties> - <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="e2" green="ac" red="24" type="rgb"/> - </Property> - <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> - <Font name="MV Boli" size="14" style="0"/> - </Property> - <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="e2" green="ac" red="24" type="rgb"/> - </Property> - <Property name="toolTipText" type="java.lang.String" value="login"/> - <Property name="actionCommand" type="java.lang.String" value="Login"/> - <Property name="cursor" type="java.awt.Cursor" editor="org.netbeans.modules.form.editors2.CursorEditor"> - <Color id="Hand Cursor"/> - </Property> - <Property name="horizontalTextPosition" type="int" value="0"/> - <Property name="label" type="java.lang.String" value="Login"/> - <Property name="opaque" type="boolean" value="false"/> - </Properties> - </Component> - <Component class="javax.swing.JButton" name="registerButton"> - <Properties> - <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="e2" green="ac" red="24" type="rgb"/> - </Property> - <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> - <Font name="MV Boli" size="14" style="0"/> - </Property> - <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="ff" green="ff" red="ff" type="rgb"/> - </Property> - <Property name="text" type="java.lang.String" value="Register"/> - <Property name="actionCommand" type="java.lang.String" value="Login"/> - <Property name="cursor" type="java.awt.Cursor" editor="org.netbeans.modules.form.editors2.CursorEditor"> - <Color id="Hand Cursor"/> - </Property> - <Property name="horizontalTextPosition" type="int" value="0"/> - </Properties> - <Events> - <EventHandler event="mouseEntered" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="registerButtonMouseEntered"/> - <EventHandler event="mouseExited" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="registerButtonMouseExited"/> - <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="registerButtonActionPerformed"/> - </Events> - </Component> - <Container class="javax.swing.JScrollPane" name="jScrollPane1"> - <AuxValues> - <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> - </AuxValues> - - <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> - <SubComponents> - <Component class="javax.swing.JTextPane" name="welconText"> - <Properties> - <Property name="editable" type="boolean" value="false"/> - <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor"> - <Border info="null"/> - </Property> - <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> - <Font name="MV Boli" size="36" style="1"/> - </Property> - <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="0" green="c0" red="ff" type="rgb"/> - </Property> - <Property name="text" type="java.lang.String" value="Welcome to Brick Breaker"/> - <Property name="toolTipText" type="java.lang.String" value=""/> - <Property name="focusable" type="boolean" value="false"/> - <Property name="opaque" type="boolean" value="false"/> - </Properties> - </Component> - </SubComponents> - </Container> - <Component class="javax.swing.JButton" name="guest"> - <Properties> - <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="e2" green="ac" red="24" type="rgb"/> - </Property> - <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> - <Font name="MV Boli" size="14" style="0"/> - </Property> - <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="ff" green="ff" red="ff" type="rgb"/> - </Property> - <Property name="actionCommand" type="java.lang.String" value="play"/> - <Property name="cursor" type="java.awt.Cursor" editor="org.netbeans.modules.form.editors2.CursorEditor"> - <Color id="Hand Cursor"/> - </Property> - <Property name="horizontalTextPosition" type="int" value="0"/> - <Property name="label" type="java.lang.String" value="Play as Guest"/> - </Properties> - <Events> - <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="guestActionPerformed"/> - </Events> - </Component> - <Component class="javax.swing.JButton" name="highScore"> - <Properties> - <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="e2" green="ac" red="24" type="rgb"/> - </Property> - <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> - <Font name="MV Boli" size="14" style="0"/> - </Property> - <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> - <Color blue="ff" green="ff" red="ff" type="rgb"/> - </Property> - <Property name="text" type="java.lang.String" value="High Scores"/> - <Property name="actionCommand" type="java.lang.String" value="play"/> - <Property name="cursor" type="java.awt.Cursor" editor="org.netbeans.modules.form.editors2.CursorEditor"> - <Color id="Hand Cursor"/> - </Property> - <Property name="horizontalTextPosition" type="int" value="0"/> - </Properties> - <Events> - <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="highScoreActionPerformed"/> - </Events> - </Component> - </SubComponents> -</Form> diff --git a/app/src/main/java/brickbreaker/MainMenu.java b/app/src/main/java/brickbreaker/MainMenu.java deleted file mode 100644 index 05091a641c2dc1ce229070e2379349a9b64a6aea..0000000000000000000000000000000000000000 --- a/app/src/main/java/brickbreaker/MainMenu.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template - */ -package brickbreaker; - -/** - * - * @author Suman - */ -public class MainMenu extends javax.swing.JFrame { - - /** - * Creates new form MainMenu - */ - public MainMenu() { - initComponents(); - } - - /** - * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. - */ - @SuppressWarnings("unchecked") - // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents - private void initComponents() { - - loginButton = new javax.swing.JButton(); - registerButton = new javax.swing.JButton(); - jScrollPane1 = new javax.swing.JScrollPane(); - welconText = new javax.swing.JTextPane(); - guest = new javax.swing.JButton(); - highScore = new javax.swing.JButton(); - - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - setBackground(new java.awt.Color(255, 255, 255)); - - loginButton.setBackground(new java.awt.Color(36, 172, 226)); - loginButton.setFont(new java.awt.Font("MV Boli", 0, 14)); // NOI18N - loginButton.setForeground(new java.awt.Color(36, 172, 226)); - loginButton.setToolTipText("login"); - loginButton.setActionCommand("Login"); - loginButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); - loginButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); - loginButton.setLabel("Login"); - loginButton.setOpaque(false); - - registerButton.setBackground(new java.awt.Color(36, 172, 226)); - registerButton.setFont(new java.awt.Font("MV Boli", 0, 14)); // NOI18N - registerButton.setForeground(new java.awt.Color(255, 255, 255)); - registerButton.setText("Register"); - registerButton.setActionCommand("Login"); - registerButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); - registerButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); - registerButton.addMouseListener(new java.awt.event.MouseAdapter() { - public void mouseEntered(java.awt.event.MouseEvent evt) { - registerButtonMouseEntered(evt); - } - public void mouseExited(java.awt.event.MouseEvent evt) { - registerButtonMouseExited(evt); - } - }); - registerButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - registerButtonActionPerformed(evt); - } - }); - - welconText.setEditable(false); - welconText.setBorder(null); - welconText.setFont(new java.awt.Font("MV Boli", 1, 36)); // NOI18N - welconText.setForeground(new java.awt.Color(255, 192, 0)); - welconText.setText("Welcome to Brick Breaker"); - welconText.setToolTipText(""); - welconText.setFocusable(false); - welconText.setOpaque(false); - jScrollPane1.setViewportView(welconText); - - 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)); - guest.setActionCommand("play"); - guest.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); - guest.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); - guest.setLabel("Play as Guest"); - guest.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - guestActionPerformed(evt); - } - }); - - highScore.setBackground(new java.awt.Color(36, 172, 226)); - highScore.setFont(new java.awt.Font("MV Boli", 0, 14)); // NOI18N - highScore.setForeground(new java.awt.Color(255, 255, 255)); - highScore.setText("High Scores"); - highScore.setActionCommand("play"); - highScore.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); - highScore.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); - highScore.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - highScoreActionPerformed(evt); - } - }); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); - getContentPane().setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(310, 310, 310) - .addComponent(loginButton)) - .addGroup(layout.createSequentialGroup() - .addGap(301, 301, 301) - .addComponent(registerButton)) - .addGroup(layout.createSequentialGroup() - .addGap(282, 282, 282) - .addComponent(guest)) - .addGroup(layout.createSequentialGroup() - .addGap(290, 290, 290) - .addComponent(highScore)) - .addGroup(layout.createSequentialGroup() - .addGap(122, 122, 122) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) - .addContainerGap(143, Short.MAX_VALUE)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addGap(117, 117, 117) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) - .addComponent(loginButton) - .addGap(18, 18, 18) - .addComponent(registerButton) - .addGap(18, 18, 18) - .addComponent(guest) - .addGap(18, 18, 18) - .addComponent(highScore) - .addContainerGap(228, Short.MAX_VALUE)) - ); - - pack(); - }// </editor-fold>//GEN-END:initComponents - - private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerButtonActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_registerButtonActionPerformed - - private void guestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guestActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_guestActionPerformed - - private void highScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_highScoreActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_highScoreActionPerformed - - private void registerButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_registerButtonMouseEntered - // TODO add your handling code here: - }//GEN-LAST:event_registerButtonMouseEntered - - private void registerButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_registerButtonMouseExited - // TODO add your handling code here: - }//GEN-LAST:event_registerButtonMouseExited - - - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton guest; - private javax.swing.JButton highScore; - private javax.swing.JScrollPane jScrollPane1; - private javax.swing.JButton loginButton; - private javax.swing.JButton registerButton; - private javax.swing.JTextPane welconText; - // End of variables declaration//GEN-END:variables -} diff --git a/app/src/main/java/brickbreaker/Menu.java b/app/src/main/java/brickbreaker/Menu.java index 1cb6ab9e2f92247822fa87622c3fa750f5a0edcd..c96671577fa8dc86fc7d9eab7eab9eaf416591b5 100644 --- a/app/src/main/java/brickbreaker/Menu.java +++ b/app/src/main/java/brickbreaker/Menu.java @@ -17,10 +17,6 @@ import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.Timer; -/** - * - * @author Suman - */ public class Menu extends JPanel { private JButton guest; private JButton highScore; @@ -189,18 +185,7 @@ public class Menu extends JPanel { private void guestActionPerformed(java.awt.event.ActionEvent evt) { this.setVisible(false); - GamePanel game = new GamePanel(frame); - game.addObserver(new GameObserver() { - @Override - public void onGameOver(int finalScore) { - System.out.println("Game over with a score of " + finalScore); - } - - @Override - public void onGameWin(int finalScore) { - System.out.println("You won with a score of " + finalScore); - } - }); + } private void highScoreActionPerformed(java.awt.event.ActionEvent evt) { @@ -208,14 +193,27 @@ public class Menu extends JPanel { } private void navigateToLogin() { - // Remove current panel - frame.remove(this); - - LoginFrame loginPage = new LoginFrame(frame); - frame.add(loginPage); + // Remove current panel + frame.remove(this); + + LoginFrame loginPage = new LoginFrame(frame); + frame.add(loginPage); + + // Refresh and repaint + frame.revalidate(); + frame.repaint(); + } - // Refresh and repaint - frame.revalidate(); - frame.repaint(); -} + public void show() { + + if(App.user != null) { + this.remove(loginButton); + this.remove(registerButton); + this.remove(guest); + } + + frame.add(this); + frame.revalidate(); + frame.repaint(); + } } diff --git a/app/src/main/java/brickbreaker/ScoringSystem.java b/app/src/main/java/brickbreaker/ScoringSystem.java index 39d164dfa2e0ece2e6832d47f5e9cd4bb91b675f..0f5548035bbdf9570899c056ab6ca0a5405ac820 100644 --- a/app/src/main/java/brickbreaker/ScoringSystem.java +++ b/app/src/main/java/brickbreaker/ScoringSystem.java @@ -1,52 +1,88 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +package brickbreaker; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; public class ScoringSystem { private int score; // Current Score private int highScore; // Highest Score - private static final int POINTS = 1; // Point Value - - private static final String HIGHSCORE_FILE = "src/main/resources/highscore.txt"; public ScoringSystem() { this.score = 0; - this.highScore = loadHighScore(); + this.setHighScore(); } + public void setHighScore() { + this.highScore = loadHighScore(); + } + private int loadHighScore() { - try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) { - String line = reader.readLine(); - if (line != null) { - return Integer.parseInt(line.trim()); + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + if (App.user == null) return 0; + try { + // Establish a database connection + connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD); + + // Define the SQL query to retrieve the high score + String sql = "SELECT MAX(score) AS high_score FROM scores WHERE user_id=? GROUP BY user_id"; + + // Create a prepared statement + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setInt(1, App.user.id); + // Execute the query and retrieve the result + resultSet = preparedStatement.executeQuery(); + + if (resultSet.next()) { + // Read the high score value from the result set + return resultSet.getInt("high_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()); } - } catch (IOException e) { - System.err.println("Loading Error: " + e.getMessage()); } + + // Return a default value if high score retrieval fails return 0; } - private void saveHighScore(int newHighScore) { - try (FileWriter writer = new FileWriter(HIGHSCORE_FILE)) { - writer.write(Integer.toString(newHighScore)); - } catch (IOException e) { - System.err.println("Saving Error: " + e.getMessage()); + private void saveHighScore() { + if(App.user == null) return; + try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) { + String sql = "INSERT INTO scores (user_id, score) VALUES (?, ?)"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, App.user.id); + preparedStatement.setInt(2, score); + preparedStatement.executeUpdate(); + } + } catch (SQLException e) { + System.err.println("Database Error: " + e.getMessage()); } } // Add point to each broken brick - public void brickBroken() { - score += POINTS; - checkForNewHighScore(); + public void brickBroken(int points) { + score += points; } // Compare Current Score and Hgihest Score - private void checkForNewHighScore() { + public void checkForNewHighScore() { if (score > highScore) { highScore = score; - saveHighScore(highScore); + this.saveHighScore(); } }