From 16d3bc453492bed2ef3260cad84dcb278cc83ecf Mon Sep 17 00:00:00 2001 From: lcaohoanq Date: Thu, 29 Aug 2024 17:20:21 +0700 Subject: [PATCH] feat: migrate current swing componenet --- logs/app.log | 37 ++ .../java/com/lcaohoanq/formhandling/Main.java | 3 + .../formhandling/constants/APIConstants.java | 6 + .../controllers/ForgotPasswordController.java | 37 ++ .../controllers/LoginController.java | 35 +- .../controllers/MenuController.java | 305 +++++++++++ .../controllers/PlayController.java | 44 ++ .../controllers/RegisterController.java | 220 ++++++++ .../controllers/SwingLoginController.java | 188 +++++++ .../formhandling/modules/user/UserDAO.java | 1 + .../formhandling/styles/UIHovers.java | 96 ++++ .../formhandling/styles/UIImages.java | 17 + .../formhandling/views/MenuView.java | 258 +++++++++ .../formhandling/views/SwingLoginView.java | 201 +++++++ .../formhandling/views/SwingRegisterView.java | 275 ++++++++++ .../formhandling/views/base/AppComponent.java | 200 +++++++ .../formhandling/views/base/Board.java | 511 ++++++++++++++++++ .../formhandling/views/base/MyFrame.java | 221 ++++++++ .../formhandling/views/game/Apartment.java | 173 ++++++ .../formhandling/views/game/Box.java | 104 ++++ .../formhandling/views/game/Campaign.java | 20 + .../formhandling/views/game/Mill.java | 143 +++++ .../formhandling/views/game/NoMaze.java | 87 +++ .../formhandling/views/game/Rails.java | 151 ++++++ .../formhandling/views/game/Snake.java | 79 +++ .../formhandling/views/game/Tunnel.java | 185 +++++++ .../com/lcaohoanq/formhandling/register.fxml | 62 --- 27 files changed, 3592 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/lcaohoanq/formhandling/constants/APIConstants.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/controllers/ForgotPasswordController.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/controllers/MenuController.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/controllers/PlayController.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/controllers/RegisterController.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/controllers/SwingLoginController.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/styles/UIHovers.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/styles/UIImages.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/MenuView.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/SwingLoginView.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/SwingRegisterView.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/base/AppComponent.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/base/Board.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/base/MyFrame.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Apartment.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Box.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Campaign.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Mill.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/NoMaze.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Rails.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Snake.java create mode 100644 src/main/java/com/lcaohoanq/formhandling/views/game/Tunnel.java delete mode 100644 src/main/resources/com/lcaohoanq/formhandling/register.fxml diff --git a/logs/app.log b/logs/app.log index 2bb0a07..8ceffbf 100644 --- a/logs/app.log +++ b/logs/app.log @@ -92,3 +92,40 @@ 2024-08-27 09:00:45 WARN org.hibernate.orm.deprecation - HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default) 2024-08-27 09:00:46 INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2024-08-27 09:00:46 INFO o.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3df2199] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. +2024-08-29 17:09:39 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: JFXSGPU] +2024-08-29 17:09:39 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version [WORKING] +2024-08-29 17:09:39 INFO o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled +2024-08-29 17:09:39 WARN o.hibernate.orm.connections.pooling - HHH10001002: Using built-in connection pool (not intended for production use) +2024-08-29 17:09:39 INFO o.hibernate.orm.connections.pooling - HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver +2024-08-29 17:09:39 INFO o.hibernate.orm.connections.pooling - HHH10001012: Connecting with JDBC URL [jdbc:mysql://localhost:3307/snake_game_app] +2024-08-29 17:09:39 INFO o.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {password=****, user=lcaohoanq} +2024-08-29 17:09:39 INFO o.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false +2024-08-29 17:09:39 INFO o.hibernate.orm.connections.pooling - HHH10001115: Connection pool size: 20 (min=1) +2024-08-29 17:09:39 WARN org.hibernate.orm.deprecation - HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default) +2024-08-29 17:09:40 INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) +2024-08-29 17:09:40 INFO o.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@23b863ff] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. +2024-08-29 17:10:07 INFO c.l.f.c.SwingLoginController - Admin login successful +2024-08-29 17:17:32 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: JFXSGPU] +2024-08-29 17:17:32 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version [WORKING] +2024-08-29 17:17:32 INFO o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled +2024-08-29 17:17:33 WARN o.hibernate.orm.connections.pooling - HHH10001002: Using built-in connection pool (not intended for production use) +2024-08-29 17:17:33 INFO o.hibernate.orm.connections.pooling - HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver +2024-08-29 17:17:33 INFO o.hibernate.orm.connections.pooling - HHH10001012: Connecting with JDBC URL [jdbc:mysql://localhost:3307/snake_game_app] +2024-08-29 17:17:33 INFO o.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {password=****, user=lcaohoanq} +2024-08-29 17:17:33 INFO o.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false +2024-08-29 17:17:33 INFO o.hibernate.orm.connections.pooling - HHH10001115: Connection pool size: 20 (min=1) +2024-08-29 17:17:33 WARN org.hibernate.orm.deprecation - HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default) +2024-08-29 17:17:34 INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) +2024-08-29 17:17:34 INFO o.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3dfe5ca] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. +2024-08-29 17:19:43 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: JFXSGPU] +2024-08-29 17:19:43 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version [WORKING] +2024-08-29 17:19:43 INFO o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled +2024-08-29 17:19:43 WARN o.hibernate.orm.connections.pooling - HHH10001002: Using built-in connection pool (not intended for production use) +2024-08-29 17:19:43 INFO o.hibernate.orm.connections.pooling - HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver +2024-08-29 17:19:43 INFO o.hibernate.orm.connections.pooling - HHH10001012: Connecting with JDBC URL [jdbc:mysql://localhost:3307/snake_game_app] +2024-08-29 17:19:43 INFO o.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {password=****, user=lcaohoanq} +2024-08-29 17:19:43 INFO o.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false +2024-08-29 17:19:43 INFO o.hibernate.orm.connections.pooling - HHH10001115: Connection pool size: 20 (min=1) +2024-08-29 17:19:43 WARN org.hibernate.orm.deprecation - HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default) +2024-08-29 17:19:44 INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) +2024-08-29 17:19:44 INFO o.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1d91bfa] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. diff --git a/src/main/java/com/lcaohoanq/formhandling/Main.java b/src/main/java/com/lcaohoanq/formhandling/Main.java index 7170140..36c1506 100644 --- a/src/main/java/com/lcaohoanq/formhandling/Main.java +++ b/src/main/java/com/lcaohoanq/formhandling/Main.java @@ -1,6 +1,9 @@ package com.lcaohoanq.formhandling; import com.lcaohoanq.formhandling.utils.LogsUtils; +import com.lcaohoanq.formhandling.views.OTPVerificationView; +import com.lcaohoanq.formhandling.views.SwingLoginView; +import java.awt.EventQueue; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; diff --git a/src/main/java/com/lcaohoanq/formhandling/constants/APIConstants.java b/src/main/java/com/lcaohoanq/formhandling/constants/APIConstants.java new file mode 100644 index 0000000..4e95b97 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/constants/APIConstants.java @@ -0,0 +1,6 @@ +package com.lcaohoanq.formhandling.constants; + +public class APIConstants { + public static final String API_PREFIX = "/api/v1"; + public static final String BASE_URL = "http://localhost:8081" + API_PREFIX; +} diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/ForgotPasswordController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/ForgotPasswordController.java new file mode 100644 index 0000000..370d3cd --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/ForgotPasswordController.java @@ -0,0 +1,37 @@ +package com.lcaohoanq.formhandling.controllers; + +import java.awt.Desktop; +import java.awt.event.ActionListener; +import java.net.URI; +import lombok.extern.slf4j.Slf4j; +import com.lcaohoanq.formhandling.views.SwingLoginView; +import com.lcaohoanq.formhandling.views.OTPVerificationView; + +@Slf4j +public class ForgotPasswordController implements ActionListener { + + private final SwingLoginView loginView; + + @Deprecated + private OTPVerificationView otpVerificationView; + + public ForgotPasswordController(SwingLoginView loginView, OTPVerificationView otpVerificationView) { + this.loginView = loginView; + this.otpVerificationView = otpVerificationView; + } + + @Override + public void actionPerformed(java.awt.event.ActionEvent e) { + String api = "http://localhost:3000/forgot-password"; + try { + // Specify the URL of the website + URI uri = new URI("http://localhost:3000/forgot-password"); + // Open the website in the default browser + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(uri); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/LoginController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/LoginController.java index 66cc87f..f543ce8 100644 --- a/src/main/java/com/lcaohoanq/formhandling/controllers/LoginController.java +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/LoginController.java @@ -1,16 +1,23 @@ package com.lcaohoanq.formhandling.controllers; +import com.lcaohoanq.formhandling.constants.APIConstants; import com.lcaohoanq.formhandling.utils.ApiUtils; +import com.lcaohoanq.formhandling.utils.EnvUtils; +import com.lcaohoanq.formhandling.views.MenuView; import com.lcaohoanq.formhandling.views.UIPrompts; import com.lcaohoanq.formhandling.views.base.BaseResources; +import java.awt.EventQueue; +import java.awt.event.ActionListener; import java.net.http.HttpResponse; import java.util.Map; +import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.input.KeyCode; import javafx.event.ActionEvent; +import javafx.stage.Stage; import javax.swing.JOptionPane; import lombok.AllArgsConstructor; import lombok.Getter; @@ -76,7 +83,7 @@ private void login(String email_phone, String password) { new Thread(() -> { try { // Replace with your API URL - String apiUrl = "http://localhost:8081/users/login"; + String apiUrl = APIConstants.BASE_URL + "/users/login"; // Create the payload as a map and convert it to JSON Map payload = Map.of( "email_phone", email_phone, // Replace with actual value @@ -90,6 +97,13 @@ private void login(String email_phone, String password) { switch (response.statusCode()) { case 200: UIPrompts.IS_LOGIN_SUCCESS(); + + Platform.runLater(() -> { + new MenuView().setVisible(true); + Stage stage = (Stage) loginButton.getScene().getWindow(); + stage.close(); + }); + break; case 400: JOptionPane.showMessageDialog(null, @@ -107,14 +121,24 @@ private void login(String email_phone, String password) { } @FXML - // Custom method for login via Google action private void loginViaGoogleAction() { - UIPrompts.IS_NOT_SUPPORT(); + Platform.runLater(() -> { + String googleAuthUrl = EnvUtils.get("GOOGLE_AUTH_URL"); + try { + URI uri = new URI(googleAuthUrl); + if (Desktop.isDesktopSupported() && Desktop.getDesktop() + .isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(uri); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + }); } @FXML private void loginViaFacebookAction() { - UIPrompts.IS_NOT_SUPPORT(); + Platform.runLater(() -> UIPrompts.IS_NOT_SUPPORT()); } public void signupHereAction(ActionEvent actionEvent) { @@ -122,7 +146,8 @@ public void signupHereAction(ActionEvent actionEvent) { // Specify the URL of the website URI uri = new URI("http://localhost:3000/users/register"); // Open the website in the default browser - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + if (Desktop.isDesktopSupported() && Desktop.getDesktop() + .isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(uri); } } catch (Exception ex) { diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/MenuController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/MenuController.java new file mode 100644 index 0000000..add4597 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/MenuController.java @@ -0,0 +1,305 @@ +package com.lcaohoanq.formhandling.controllers; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import com.lcaohoanq.formhandling.enums.Hover; +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import javax.swing.JButton; +import com.lcaohoanq.formhandling.modules.sound.AudioHandler; +import com.lcaohoanq.formhandling.styles.UIHovers; +import com.lcaohoanq.formhandling.views.MenuView; +import com.lcaohoanq.formhandling.views.UIPrompts; +import com.lcaohoanq.formhandling.views.base.AppComponent; +import com.lcaohoanq.formhandling.views.game.Snake; + +public class MenuController implements MouseListener, ActionListener { + + public static MenuView menuView; + public static MenuView.MenuModern menuModern; + private boolean isMenuModern = true; + private final AudioHandler audioHandler; + private UIHovers uiHovers; + + private final List jButtonList; +// private UIHovers uiHovers; + + public MenuController(MenuView menuView) { + MenuController.menuView = menuView; + this.audioHandler = new AudioHandler(); + this.jButtonList = Arrays.asList( + menuView.jButton_Mode_Classic, + menuView.jButton_Mode_Modern, + menuView.jButton_Mode_Campaign); + this.uiHovers = new UIHovers<>(menuView); + + } + + @Override + public void mouseClicked(MouseEvent e) { + if (e.getSource() == menuView.jButton_Mode_Classic) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Classic").startGame(); + }); + } + if (e.getSource() == menuView.jButton_Mode_Modern) { + EventQueue.invokeLater(() -> { + + if (isMenuModern) { + menuModern = menuView.new MenuModern(); + isMenuModern = true; +// menuView.dispose(); + menuModern.setVisible(true); + } else { +// menuView.dispose(); + isMenuModern = false; + } + }); + } + if (e.getSource() == menuView.jButton_Mode_Campaign) { + EventQueue.invokeLater(() -> { +// menuView.dispose(); +// new Snake("Campaign").startGame(); + + prepareUnsupportFeature(); + }); + } + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + if (menuView.isActive()) { + jButtonList.stream() + .filter(button -> e.getSource() == button) + .forEach(button -> { + if (menuView.getStatusToggle()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", button); + } else { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", button); + } + }); + } + } + + + @Override + public void mouseExited(MouseEvent e) { + if (menuView.isActive()) { + jButtonList.stream() + .filter(button -> e.getSource() == button) + .forEach(button -> { + if (menuView.getStatusToggle()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", button); + } else { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", button); + } + }); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().equals("Off")) { + System.out.println("user click Off sound"); + menuView.setAudio(false); + } else if (e.getActionCommand().equals("On")) { + menuView.setAudio(Hover.ENABLE.isStatus()); + } + } + + private void prepareUnsupportFeature() { + InputStream unsupported = getClass().getResourceAsStream(ResourcePaths.URL_EATING); + audioHandler.playAudio(unsupported); + UIPrompts.IS_NOT_SUPPORT(); + } + + public static class MenuModernController extends AppComponent implements MouseListener { + + private final MenuView.MenuModern menuModern; + private final AudioHandler audioHandler; + private UIHovers uiHovers; + + public MenuModernController(MenuView.MenuModern menuModern) { + this.menuModern = menuModern; + this.audioHandler = new AudioHandler(); + this.uiHovers = new UIHovers<>(menuModern); + } + + @Override + public void mouseClicked(MouseEvent e) { + if (e.getSource() == menuModern.getJButton_NoMaze()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("NoMaze").startGame(); + }); + } + if (e.getSource() == menuModern.getJButton_Box()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Box").startGame(); + }); + } + if (e.getSource() == menuModern.getJButton_Tunnel()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Tunnel").startGame(); + }); + } + if (e.getSource() == menuModern.getJButton_Mill()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Mill").startGame(); + }); + } + if (e.getSource() == menuModern.getJButton_Rails()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Rails").startGame(); + }); + } + if (e.getSource() == menuModern.getJButton_Apartment()) { + EventQueue.invokeLater(() -> { + menuView.dispose(); + new Snake("Apartment").startGame(); + }); + } + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + if (menuModern.getStatusToggle()) { + if (e.getSource() == menuModern.getJButton_NoMaze()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_NoMaze()); + } + if (e.getSource() == menuModern.getJButton_Box()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_Box()); + } + if (e.getSource() == menuModern.getJButton_Tunnel()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_Tunnel()); + } + if (e.getSource() == menuModern.getJButton_Mill()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_Mill()); + } + if (e.getSource() == menuModern.getJButton_Rails()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_Rails()); + } + if (e.getSource() == menuModern.getJButton_Apartment()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark", menuModern.getJButton_Apartment()); + } + } else { + if (e.getSource() == menuModern.getJButton_NoMaze()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_NoMaze()); + } + if (e.getSource() == menuModern.getJButton_Box()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_Box()); + } + if (e.getSource() == menuModern.getJButton_Tunnel()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_Tunnel()); + } + if (e.getSource() == menuModern.getJButton_Mill()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_Mill()); + } + if (e.getSource() == menuModern.getJButton_Rails()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_Rails()); + } + if (e.getSource() == menuModern.getJButton_Apartment()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light", menuModern.getJButton_Apartment()); + } + } + } + + @Override + public void mouseExited(MouseEvent e) { + if (menuModern.getStatusToggle()) { + if (e.getSource() == menuModern.getJButton_NoMaze()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_NoMaze()); + } + if (e.getSource() == menuModern.getJButton_Box()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_Box()); + } + if (e.getSource() == menuModern.getJButton_Tunnel()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_Tunnel()); + } + if (e.getSource() == menuModern.getJButton_Mill()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_Mill()); + } + if (e.getSource() == menuModern.getJButton_Rails()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_Rails()); + } + if (e.getSource() == menuModern.getJButton_Apartment()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark", menuModern.getJButton_Apartment()); + } + } else { + if (e.getSource() == menuModern.getJButton_NoMaze()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_NoMaze()); + } + if (e.getSource() == menuModern.getJButton_Box()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_Box()); + } + if (e.getSource() == menuModern.getJButton_Tunnel()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_Tunnel()); + } + if (e.getSource() == menuModern.getJButton_Mill()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_Mill()); + } + if (e.getSource() == menuModern.getJButton_Rails()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_Rails()); + } + if (e.getSource() == menuModern.getJButton_Apartment()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light", menuModern.getJButton_Apartment()); + } + } + } + + private void prepareUnsupportFeature() { + InputStream unsupported = getClass().getResourceAsStream(ResourcePaths.URL_EATING); + audioHandler.playAudio(unsupported); + UIPrompts.IS_NOT_SUPPORT(); + } + + @Override + public void initComponents() { + + } + + @Override + public void doAction() { + + } + } + + public static void main(String[] args) { + EventQueue.invokeLater(() -> { + MenuView menuView = new MenuView(); + MenuController menuController = new MenuController(menuView); + menuView.setVisible(true); + }); + } + +} diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/PlayController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/PlayController.java new file mode 100644 index 0000000..04898cc --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/PlayController.java @@ -0,0 +1,44 @@ +package com.lcaohoanq.formhandling.controllers; + +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import com.lcaohoanq.formhandling.views.SwingLoginView; +import com.lcaohoanq.formhandling.views.MenuView; +import com.lcaohoanq.formhandling.views.game.Snake; + +public class PlayController implements ActionListener { + public static MenuView menuView; + private SwingLoginView loginView; + private Snake snake; + private boolean isLoginView = false; + private boolean isSnake = false; + + public PlayController(SwingLoginView loginView) { + this.loginView = loginView; + isLoginView = true; + } + + public PlayController(Snake snake) { + this.snake = snake; + isSnake = true; + } + + @Override + public void actionPerformed(ActionEvent e) { + if (isLoginView) { + loginView.dispose(); + EventQueue.invokeLater(() -> { + menuView = new MenuView(); + menuView.setVisible(true); + }); + } + if (isSnake) { + snake.dispose(); + EventQueue.invokeLater(() -> { + menuView = new MenuView(); + menuView.setVisible(true); + }); + } + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/RegisterController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/RegisterController.java new file mode 100644 index 0000000..1200483 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/RegisterController.java @@ -0,0 +1,220 @@ +package com.lcaohoanq.formhandling.controllers; + +import com.lcaohoanq.formhandling.enums.Hover; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.util.Arrays; +import java.util.List; +import javax.swing.JButton; +import javax.swing.JTextField; +import lombok.extern.slf4j.Slf4j; +import com.lcaohoanq.formhandling.modules.email.EmailUtils; +import com.lcaohoanq.formhandling.modules.otp.OTPUtils; +import com.lcaohoanq.formhandling.styles.UIHovers; +import com.lcaohoanq.formhandling.views.OTPVerificationView; +import com.lcaohoanq.formhandling.views.SwingRegisterView; +import com.lcaohoanq.formhandling.views.UIPrompts; + +@Slf4j +public class RegisterController implements ActionListener, MouseListener { + + private final SwingRegisterView registerView; + private final List inputFieldList; + private final List buttonList; + private OTPVerificationView otpVerificationView; + private UIHovers uiHovers; + + public RegisterController(SwingRegisterView registerView) { + super(); + this.registerView = registerView; + this.inputFieldList = Arrays.asList( + registerView.getJTextField_Right_Middle_Email(), + registerView.getJTextField_Right_Middle_FirstName(), + registerView.getJTextField_Right_Middle_LastName(), + registerView.getJPasswordField_Right_Middle_Password(), + registerView.getJPasswordField_Right_Middle_Confirm_Password()); + this.buttonList = Arrays.asList( + registerView.getJButton_Right_Bottom_Submit(), + registerView.getJButton_Right_Bottom_Others()); + this.uiHovers = new UIHovers<>(registerView); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (!registerView.isEmpty()) { + if (registerView.isMatchingPattern()) { + if (registerView.isMatchingPasswordAndConfirmPassword()) { + if (!registerView.isDuplicateEmail()) { + EmailUtils handleEmail = new EmailUtils(); + String email = registerView.getDataWhenRegister().getEmail(); + String otp = OTPUtils.generateOTP(); + handleEmail.sendEmail( + handleEmail.subjectGreeting(registerView.getDataWhenRegister().getFirstName()), + handleEmail.emailSendOtp(registerView.getDataWhenRegister().getFirstName(), + otp), email); + + OTPUtils.IS_NOTIFY_VERIFY_ACCOUNT(); + otpVerificationView = new OTPVerificationView(otp, + new OTPVerificationListener() { + @Override + public void onOtpVerified() { + registerView.insertMail(); + UIPrompts.IS_REGISTER_SUCCESS(); + registerView.setEnabled(true); + log.info("User {} registered successfully", email); + } + + @Override + public void onResendOtp() { + // Handle resending OTP + String newOtp = OTPUtils.generateOTP(); + handleEmail.sendEmail(handleEmail.subjectGreeting( + registerView.getDataWhenRegister().getFirstName()), + handleEmail.emailSendOtp( + registerView.getDataWhenRegister().getFirstName(), newOtp), + email); + otpVerificationView.setGeneratedOtp(newOtp); + log.info("Resend OTP to email {}", email); + } + + @Override + public void onBlockUser() { + handleEmail.sendEmail(handleEmail.subjectGreeting( + registerView.getDataWhenRegister().getFirstName()), + handleEmail.emailSendBlockAccount( + registerView.getDataWhenRegister().getFirstName(), + "Too many request, maybe abuse action, we added you to application blacklist"), + email); + log.warn( + "Blocked user with email {}, too many request register in time", + email); + } + }); + otpVerificationView.setVisible(true); + registerView.setEnabled(false); + } else { + UIPrompts.IS_EXISTED_EMAIL(); + log.error("Email already exists, please try again"); + } + } else { + UIPrompts.IS_WRONG_USERNAME_OR_PASSWORD(); + log.error("Password and confirm password do not match, please try again"); + } + } + } else { + UIPrompts.IS_EMPTY_FIELD(); + log.error("Empty field when register, please try again"); + } + + } + + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + + @Override + public void mouseEntered(MouseEvent e) { + inputFieldList.stream() + .filter(inputField -> e.getSource() == inputField) + .forEach(inputField -> { + if (!registerView.getStatusToggle()) { + if (inputField == registerView.getJTextField_Right_Middle_Email()) { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "light"); + } + if (inputField == registerView.getJTextField_Right_Middle_FirstName()) { + uiHovers.setHoverFirstName(Hover.ENABLE.isStatus(), "light"); + } + if (inputField == registerView.getJTextField_Right_Middle_LastName()) { + uiHovers.setHoverLastName(Hover.ENABLE.isStatus(), "light"); + } + if (inputField == registerView.getJPasswordField_Right_Middle_Password()) { + uiHovers.setHoverPassword(Hover.ENABLE.isStatus(), "light"); + } + if (inputField + == registerView.getJPasswordField_Right_Middle_Confirm_Password()) { + uiHovers.setHoverConfirmPassword(Hover.ENABLE.isStatus(), "light"); + } + } else { + if(inputField == registerView.getJTextField_Right_Middle_Email()){ + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "dark"); + } + if(inputField == registerView.getJTextField_Right_Middle_FirstName()){ + uiHovers.setHoverFirstName(Hover.ENABLE.isStatus(),"dark"); + } + if(inputField == registerView.getJTextField_Right_Middle_LastName()){ + uiHovers.setHoverLastName(Hover.ENABLE.isStatus(), "dark"); + } + if(inputField == registerView.getJPasswordField_Right_Middle_Password()){ + uiHovers.setHoverPassword(Hover.ENABLE.isStatus(), "dark"); + } + if(inputField == registerView.getJPasswordField_Right_Middle_Confirm_Password()){ + uiHovers.setHoverConfirmPassword(Hover.ENABLE.isStatus(), "dark"); + } + } + }); + + buttonList.stream() + .filter(button -> e.getSource() == button) + .forEach(button -> { + if (button.getText().equals("Submit")) { + if (!registerView.getStatusToggle()) { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "light"); + } else { + uiHovers.setHoverButton(Hover.ENABLE.isStatus(), "dark"); + } + } else { + uiHovers.setHoverOther(Hover.ENABLE.isStatus()); + } + }); + } + + @Override + public void mouseExited(MouseEvent e) { + inputFieldList.stream() + .filter(inputField -> e.getSource() == inputField) + .forEach(inputField -> { + if (!registerView.getStatusToggle()) { + uiHovers.setHoverEmail(Hover.DISABLE.isStatus(), "light"); + uiHovers.setHoverFirstName(Hover.DISABLE.isStatus(), "light"); + uiHovers.setHoverLastName(Hover.DISABLE.isStatus(), "light"); + uiHovers.setHoverPassword(Hover.DISABLE.isStatus(), "light"); + uiHovers.setHoverConfirmPassword(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverEmail(Hover.DISABLE.isStatus(), "dark"); + uiHovers.setHoverFirstName(Hover.DISABLE.isStatus(), "dark"); + uiHovers.setHoverLastName(Hover.DISABLE.isStatus(), "dark"); + uiHovers.setHoverPassword(Hover.DISABLE.isStatus(), "dark"); + uiHovers.setHoverConfirmPassword(Hover.DISABLE.isStatus(), "dark"); + + } + }); + + buttonList.stream() + .filter(button -> e.getSource() == button) + .forEach(button -> { + if (button.getText().equals("Submit")) { + if (!registerView.getStatusToggle()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark"); + } + } else { + uiHovers.setHoverOther(Hover.DISABLE.isStatus()); + } + }); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/controllers/SwingLoginController.java b/src/main/java/com/lcaohoanq/formhandling/controllers/SwingLoginController.java new file mode 100644 index 0000000..10c6dcc --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/controllers/SwingLoginController.java @@ -0,0 +1,188 @@ +package com.lcaohoanq.formhandling.controllers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.lcaohoanq.formhandling.enums.Hover; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.IOException; +import java.net.http.HttpClient; +import java.net.http.HttpResponse; +import java.util.Map; +import javax.swing.JButton; +import javax.swing.JOptionPane; +import lombok.extern.slf4j.Slf4j; +import com.lcaohoanq.formhandling.styles.UIHovers; +import com.lcaohoanq.formhandling.utils.ApiUtils; +import com.lcaohoanq.formhandling.views.SwingLoginView; +import com.lcaohoanq.formhandling.views.UIPrompts; + +@Slf4j +public final class SwingLoginController implements ActionListener, MouseListener { + + public static String email = ""; + private final SwingLoginView loginView; + public String password = ""; + private final UIHovers uiHovers; + private final HttpClient httpClient; + private final ObjectMapper objectMapper; + + public SwingLoginController(SwingLoginView loginView) { + super(); + this.loginView = loginView; + this.uiHovers = new UIHovers<>(loginView); + httpClient = HttpClient.newHttpClient(); + objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()).enable( + SerializationFeature.INDENT_OUTPUT); + } + + @Override + public void actionPerformed(ActionEvent e) { + email = loginView.getDataWhenLogin().getEmail(); + password = loginView.getDataWhenLogin().getPassword(); + + if (loginView.isAdmin()) { + loginView.handleSuccess(); + log.info("Admin login successful"); + return; + } + //prevent empty field when click submit button, but not when click on the menu + if (loginView.isEmpty() && e.getSource() instanceof JButton) { + UIPrompts.IS_EMPTY_FIELD(); + log.error("Empty field when login, please try again"); + } else { + login(email, password); + } + + } + + private void login(String email_phone, String password) { + // Create a new thread to avoid blocking the Swing event dispatch thread + new Thread(() -> { + try { + // Replace with your API URL + String apiUrl = "http://localhost:8081/users/login"; + // Create the payload as a map and convert it to JSON + Map payload = Map.of( + "email_phone", email_phone, // Replace with actual value + "password", password // Replace with actual value + ); + + // Send the request and get the response + HttpResponse response = ApiUtils.postRequest(apiUrl, payload); + + // Handle the response + switch (response.statusCode()) { + case 200: + loginView.handleSuccess(); + break; + case 400: + JOptionPane.showMessageDialog(null, + "Username or password is incorrect, please try again!"); + break; + default: + JOptionPane.showMessageDialog(null, + "Internal server error, please try again later!"); + break; + } + } catch (IOException | InterruptedException ex) { + JOptionPane.showMessageDialog(null, "An error occurred: " + ex.getMessage()); + } + }).start(); + } + + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + if (e.getSource() == loginView.getJTextField_Right_Middle_Email()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "light"); + } else { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJPasswordField_Right_Middle_Password()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "light"); + + } else { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Submit()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "light"); + } else { + uiHovers.setHoverEmail(Hover.ENABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Others()) { + uiHovers.setHoverOther(Hover.ENABLE.isStatus()); + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Forgot_Password()) { + uiHovers.setHoverForgotPassword(Hover.ENABLE.isStatus()); + } + } + + @Override + public void mouseExited(MouseEvent e) { + if (e.getSource() == loginView.getJTextField_Right_Middle_Email()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverEmail(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverEmail(Hover.DISABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJTextField_Right_Middle_FirstName()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverFirstName(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverFirstName(Hover.DISABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJTextField_Right_Middle_LastName()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverLastName(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverLastName(Hover.DISABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJPasswordField_Right_Middle_Password()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverPassword(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverPassword(Hover.DISABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Submit()) { + if (!loginView.getStatusToggle()) { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "light"); + } else { + uiHovers.setHoverButton(Hover.DISABLE.isStatus(), "dark"); + } + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Others()) { + uiHovers.setHoverOther(Hover.DISABLE.isStatus()); + } + if (e.getSource() == loginView.getJButton_Right_Bottom_Forgot_Password()) { + uiHovers.setHoverForgotPassword(Hover.DISABLE.isStatus()); + } + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/modules/user/UserDAO.java b/src/main/java/com/lcaohoanq/formhandling/modules/user/UserDAO.java index e0cbf1f..ec03ba5 100644 --- a/src/main/java/com/lcaohoanq/formhandling/modules/user/UserDAO.java +++ b/src/main/java/com/lcaohoanq/formhandling/modules/user/UserDAO.java @@ -13,6 +13,7 @@ import org.hibernate.Transaction; import org.hibernate.query.Query; +@Deprecated public class UserDAO { private static UserDAO instance; diff --git a/src/main/java/com/lcaohoanq/formhandling/styles/UIHovers.java b/src/main/java/com/lcaohoanq/formhandling/styles/UIHovers.java new file mode 100644 index 0000000..7d5398d --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/styles/UIHovers.java @@ -0,0 +1,96 @@ +package com.lcaohoanq.formhandling.styles; + +import java.awt.Color; +import java.awt.Font; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JTextField; +import com.lcaohoanq.formhandling.views.base.AppComponent; + +public record UIHovers(T frame) { + + private void applyHoverEffect(JComponent component, boolean isInside, String mode) { + Color normalColor; + Color hoverColor; + Font normalFont = UIFonts.OTHERS; + Font hoverFont = UIFonts.OTHERS_HOVER; + + if (component instanceof JTextField) { + normalColor = + mode.equals("light") ? UIColors.SECONDARY_COLOR_L : UIColors.SECONDARY_COLOR_D; + hoverColor = mode.equals("light") ? UIColors.SECONDARY_COLOR_L_HOVER + : UIColors.SECONDARY_COLOR_D_HOVER; + component.setBackground(isInside ? hoverColor : normalColor); + } else if (component instanceof JButton) { + //button contain text only, border is null + if (((JButton) component).getText().equals(UILabels.SIGN_IN_HERE) + || ((JButton) component).getText().equals(UILabels.SIGN_UP_HERE) + || ((JButton) component).getText().equals(UILabels.FORGOT_PASSWORD)) { + component.setFont(isInside ? hoverFont : normalFont); + } else if (((JButton) component).getText().equals(UILabels.SUBMIT)) { + //font follow the default setting, only need to change the color + normalColor = mode.equals("light") ? UIColors.TEXT_COLOR_L : UIColors.TEXT_COLOR_D; + hoverColor = mode.equals("light") ? UIColors.TEXT_COLOR_L_HOVER + : UIColors.TEXT_COLOR_D_HOVER; + component.setBackground(isInside ? hoverColor : normalColor); + } else { + normalColor = mode.equals("light") ? UIColors.TEXT_COLOR_L : UIColors.TEXT_COLOR_D; + hoverColor = mode.equals("light") ? UIColors.TEXT_COLOR_L_HOVER + : UIColors.TEXT_COLOR_D_HOVER; + component.setFont(isInside ? hoverFont : normalFont); + component.setBackground(isInside ? hoverColor : normalColor); + } + } + + } + + public void setHoverEmail(boolean isInside, String mode) { + applyHoverEffect(frame.getJTextField_Right_Middle_Email(), isInside, mode); + } + + public void setHoverFirstName(boolean isInside, String mode) { + applyHoverEffect(frame.getJTextField_Right_Middle_FirstName(), isInside, mode); + } + + public void setHoverLastName(boolean isInside, String mode) { + applyHoverEffect(frame.getJTextField_Right_Middle_LastName(), isInside, mode); + } + + public void setHoverPassword(boolean isInside, String mode) { + applyHoverEffect(frame.getJPasswordField_Right_Middle_Password(), isInside, mode); + } + + public void setHoverConfirmPassword(boolean isInside, String mode) { + applyHoverEffect(frame.getJPasswordField_Right_Middle_Confirm_Password(), isInside, mode); + } + + public void setHoverButton(boolean isInside, String mode) { + applyHoverEffect(frame.getJButton_Right_Bottom_Submit(), isInside, mode); + } + + public void setHoverOther(boolean isInside) { + applyHoverEffect(frame.getJButton_Right_Bottom_Others(), isInside, "light"); + } + + public void setHoverForgotPassword(boolean isInside) { + applyHoverEffect(frame.getJButton_Right_Bottom_Forgot_Password(), isInside, "light"); + } + + public void setHoverButton(boolean isInside, String mode, JButton button) { + if (isInside) { + if (mode.equals("light")) { + button.setBackground(UIColors.TEXT_COLOR_L_HOVER); + } else { + button.setBackground(UIColors.TEXT_COLOR_D_HOVER); + } + } else { + if (mode.equals("light")) { + button.setBackground(UIColors.TEXT_COLOR_L); + } else { + button.setBackground(UIColors.TEXT_COLOR_D); + } + } + + } + +} \ No newline at end of file diff --git a/src/main/java/com/lcaohoanq/formhandling/styles/UIImages.java b/src/main/java/com/lcaohoanq/formhandling/styles/UIImages.java new file mode 100644 index 0000000..9d79e87 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/styles/UIImages.java @@ -0,0 +1,17 @@ +package com.lcaohoanq.formhandling.styles; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Image; +import java.awt.Toolkit; +import java.net.URL; +import javax.swing.ImageIcon; +import com.lcaohoanq.formhandling.views.base.MyFrame; + +public class UIImages { + public static final URL iconURL = MyFrame.class.getResource(ResourcePaths.URL_KEY_ICON); + public static final Image icon = Toolkit.getDefaultToolkit().createImage(iconURL); + + public static final URL snakeURL = MyFrame.class.getResource(ResourcePaths.URL_SNAKE_LOGO); + public static final Image logo = Toolkit.getDefaultToolkit().getImage(snakeURL); + public static final ImageIcon logoIcon = new ImageIcon(logo); +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/MenuView.java b/src/main/java/com/lcaohoanq/formhandling/views/MenuView.java new file mode 100644 index 0000000..21e5dfb --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/MenuView.java @@ -0,0 +1,258 @@ +package com.lcaohoanq.formhandling.views; + +import com.lcaohoanq.formhandling.controllers.MenuController; +import com.lcaohoanq.formhandling.controllers.ToggleHandler; +import java.awt.BorderLayout; +import java.awt.EventQueue; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Objects; +import javax.swing.JFrame; +import javax.swing.JMenuBar; +import com.lcaohoanq.formhandling.modules.sound.AudioHandler; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIHovers; +import com.lcaohoanq.formhandling.styles.UIImages; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.AppComponent; + +public class MenuView extends AppComponent implements ToggleHandler { + boolean isActive; + private UIHovers uiHovers; + public MenuView() { + setTitle("Snake Game"); + setSize(UISizes.HEIGHT_MY_FRAME, UISizes.HEIGHT_MY_FRAME); + setIconImage(UIImages.icon); + setLocationRelativeTo(null); + setResizable(false); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + initUI(); + this.uiHovers = new UIHovers<>(this); + } + + @Override + public void initComponents() { + + } + + private void initUI() { + initMenu(); + initButton(); + initContainer(); + changeColorBaseOnToggle(); + doAction(); + } + + private void initMenu() { + jMenu_Back_To_Main_Menu.add(jMenuItem_Back_To_Main_Menu); + jMenu_Sound.add(jMenuItem_Sound_On); + jMenu_Sound.add(jMenuItem_Sound_Off); + jMenu_Log_Out.add(jMenuItem_Log_Out); + + jMenuBar_MenuView = new JMenuBar(); + + jMenuBar_MenuView.setLayout(new FlowLayout(FlowLayout.LEFT)); + jMenuBar_MenuView.setBorder(UIBorders.LINE_MENU_BAR); + jMenuBar_MenuView.add(jMenu_Back_To_Main_Menu); + jMenuBar_MenuView.add(jMenu_Sound); + jMenuBar_MenuView.add(jMenu_Log_Out); +// jMenu.add(jMenu_Sound); + this.setJMenuBar(jMenuBar_MenuView); + } + + private void initButton() { + jPanel_Button_MenuView.add(jButton_Mode_Classic); + jPanel_Button_MenuView.add(jButton_Mode_Modern); + jPanel_Button_MenuView.add(jButton_Mode_Campaign); + } + + private void initContainer() { + jPanel_Container_MenuView.add(jLabel_Title_MenuView, BorderLayout.NORTH); + jPanel_Container_MenuView.add(jPanel_Button_MenuView, BorderLayout.CENTER); + add(jPanel_Container_MenuView); + } + + @Override + public void changeColorBaseOnToggle() { + //if toggle is on, change color to dark + if (getStatusToggle()) { + jLabel_Title_MenuView.setForeground(UIColors.TEXT_COLOR_D); + jButton_Mode_Classic.setBackground(UIColors.TEXT_COLOR_D); + jButton_Mode_Classic.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Mode_Modern.setBackground(UIColors.TEXT_COLOR_D); + jButton_Mode_Modern.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Mode_Campaign.setBackground(UIColors.TEXT_COLOR_D); + jButton_Mode_Campaign.setForeground(UIColors.PRIMARY_COLOR_D); + jPanel_Button_MenuView.setBackground(UIColors.PRIMARY_COLOR_D); + jPanel_Container_MenuView.setBackground(UIColors.PRIMARY_COLOR_D); + } else { + jLabel_Title_MenuView.setForeground(UIColors.TEXT_COLOR_L); + jButton_Mode_Classic.setBackground(UIColors.TEXT_COLOR_L); + jButton_Mode_Classic.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Mode_Modern.setBackground(UIColors.TEXT_COLOR_L); + jButton_Mode_Modern.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Mode_Campaign.setBackground(UIColors.TEXT_COLOR_L); + jButton_Mode_Campaign.setForeground(UIColors.PRIMARY_COLOR_L); + jPanel_Button_MenuView.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Container_MenuView.setBackground(UIColors.PRIMARY_COLOR_L); + } + } + + @Override + public void doAction() { +// jMenuItem_Back_To_Main_Menu.addActionListener(new BackToMainMenu()); + jMenuItem_Back_To_Main_Menu.addActionListener(new BackToMainMenu()); + jMenuItem_Sound_On.addActionListener(new MenuController(this)); + jMenuItem_Sound_Off.addActionListener(new MenuController(this)); + jMenuItem_Log_Out.addActionListener(new LogOut()); + jButton_Mode_Classic.addMouseListener(new MenuController(this)); + jButton_Mode_Modern.addMouseListener(new MenuController(this)); + jButton_Mode_Campaign.addMouseListener(new MenuController(this)); + } + + public void setAudio(boolean msg) { + if (!msg) { + AudioHandler.path = false; + System.out.println("path update nek: " + AudioHandler.path); + System.out.println("set audio nhan gia tri null: " + msg); + } else { + AudioHandler.path = true; + System.out.println("set audio nhan gia tri: " + msg); + } + } + + public class MenuModern extends AppComponent implements ActionListener, ToggleHandler { + + public MenuModern() { + setTitle("Modern Menu"); + setSize(UISizes.WIDTH_SCORE_FORM, UISizes.HEIGHT_MY_FRAME); + setIconImage(UIImages.icon); + setLocationRelativeTo(null); + setResizable(false); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setVisible(false); + initComponents(); + doAction(); + } + + @Override + public void doAction() { + jButton_NoMaze.addMouseListener(new MenuController.MenuModernController(this)); + jButton_Box.addMouseListener(new MenuController.MenuModernController(this)); + jButton_Tunnel.addMouseListener(new MenuController.MenuModernController(this)); + jButton_Mill.addMouseListener(new MenuController.MenuModernController(this)); + jButton_Rails.addMouseListener(new MenuController.MenuModernController(this)); + jButton_Apartment.addMouseListener(new MenuController.MenuModernController(this)); + } + + @Override + public void initComponents() { + initButtonMenuModern(); + initContainerMenuModern(); + changeColorBaseOnToggle(); + } + + private void initButtonMenuModern() { + jPanel_Menu_Modern.add(jButton_NoMaze); + jPanel_Menu_Modern.add(jButton_Box); + jPanel_Menu_Modern.add(jButton_Tunnel); + jPanel_Menu_Modern.add(jButton_Mill); + jPanel_Menu_Modern.add(jButton_Rails); + jPanel_Menu_Modern.add(jButton_Apartment); + } + + private void initContainerMenuModern() { + jPanel_Container_MenuModern.setLayout(new BorderLayout()); + jPanel_Container_MenuModern.add(jLabel_Title_Modern, BorderLayout.NORTH); + jPanel_Container_MenuModern.add(jPanel_Menu_Modern, BorderLayout.CENTER); + add(jPanel_Container_MenuModern); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (Objects.equals(e.getActionCommand(), "No Maze")) { + System.out.println("No Maze"); + } + if (Objects.equals(e.getActionCommand(), "Box")) { + System.out.println("Box"); + } + if (Objects.equals(e.getActionCommand(), "Tunnel")) { + System.out.println("Tunnel"); + } + if (Objects.equals(e.getActionCommand(), "Mill")) { + System.out.println("Mill"); + } + if (Objects.equals(e.getActionCommand(), "Rails")) { + System.out.println("Rails"); + } + if (Objects.equals(e.getActionCommand(), "Apartment")) { + System.out.println("Apartment"); + } + } + + @Override + public void changeColorBaseOnToggle() { + if (MenuView.this.getStatusToggle()) { + jLabel_Title_Modern.setBackground(UIColors.PRIMARY_COLOR_D); + jLabel_Title_Modern.setForeground(UIColors.TEXT_COLOR_D); + jButton_NoMaze.setBackground(UIColors.TEXT_COLOR_D); + jButton_NoMaze.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Box.setBackground(UIColors.TEXT_COLOR_D); + jButton_Box.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Tunnel.setBackground(UIColors.TEXT_COLOR_D); + jButton_Tunnel.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Mill.setBackground(UIColors.TEXT_COLOR_D); + jButton_Mill.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Rails.setBackground(UIColors.TEXT_COLOR_D); + jButton_Rails.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Apartment.setBackground(UIColors.TEXT_COLOR_D); + jButton_Apartment.setForeground(UIColors.PRIMARY_COLOR_D); + jPanel_Menu_Modern.setBackground(UIColors.PRIMARY_COLOR_D); + jPanel_Container_MenuModern.setBackground(UIColors.PRIMARY_COLOR_D); + } else { + jLabel_Title_Modern.setBackground(UIColors.PRIMARY_COLOR_L); + jLabel_Title_Modern.setForeground(UIColors.TEXT_COLOR_L); + jButton_NoMaze.setBackground(UIColors.TEXT_COLOR_L); + jButton_NoMaze.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Box.setBackground(UIColors.TEXT_COLOR_L); + jButton_Box.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Tunnel.setBackground(UIColors.TEXT_COLOR_L); + jButton_Tunnel.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Mill.setBackground(UIColors.TEXT_COLOR_L); + jButton_Mill.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Rails.setBackground(UIColors.TEXT_COLOR_L); + jButton_Rails.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Apartment.setBackground(UIColors.TEXT_COLOR_L); + jButton_Apartment.setForeground(UIColors.PRIMARY_COLOR_L); + jPanel_Menu_Modern.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Container_MenuModern.setBackground(UIColors.PRIMARY_COLOR_L); + } + } + } + + private class BackToMainMenu implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + EventQueue.invokeLater(() -> { + dispose(); + System.out.println("Back to main menu"); + toggleButton.setSelected(false); + new SwingLoginView().setVisible(true); + }); + } + } + + private class LogOut implements ActionListener{ + @Override + public void actionPerformed(ActionEvent e) { + EventQueue.invokeLater(() -> { + dispose(); + toggleButton.setSelected(false); + new SwingLoginView().setVisible(true); + }); + } + } +} + diff --git a/src/main/java/com/lcaohoanq/formhandling/views/SwingLoginView.java b/src/main/java/com/lcaohoanq/formhandling/views/SwingLoginView.java new file mode 100644 index 0000000..fc96653 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/SwingLoginView.java @@ -0,0 +1,201 @@ +package com.lcaohoanq.formhandling.views; + +import com.lcaohoanq.formhandling.controllers.SwingLoginController; +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import com.lcaohoanq.formhandling.controllers.ForgotPasswordController; +import com.lcaohoanq.formhandling.controllers.LoginController; +import com.lcaohoanq.formhandling.controllers.PlayController; +import com.lcaohoanq.formhandling.controllers.ToggleHandler; +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.GridLayout; +import java.io.InputStream; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import lombok.Getter; +import com.lcaohoanq.formhandling.models.LoginModel; +import com.lcaohoanq.formhandling.modules.user.UserEntity; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIFonts; +import com.lcaohoanq.formhandling.styles.UILabels; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.MyFrame; + +@Getter +public class SwingLoginView extends MyFrame implements ToggleHandler{ + + private CardLayout cardLayout; + private final LoginModel loginModel; + private SwingLoginController loginController; + private OTPVerificationView otpVerificationView; + + public SwingLoginView() { + super(); + this.loginModel = new LoginModel(); + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_INTRO); + audioHandler.playAudio(inputStream); + // Initialize OTPVerificationView + } + + @Override + public void initRight() { + initRightTop(); + initRightMiddle(); + initRightBottom(); + initRightPanel(); + } + + @Override + public void initRightTop() { + jLabel_Right_Top_Title = new JLabel(UILabels.LOGIN, JLabel.CENTER); + jLabel_Right_Top_Title.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Top_Title.setFont(UIFonts.RIGHT_TITLE); + jPanel_Right_Top_Title.add(jLabel_Right_Top_Title); + } + + @Override + public void initRightMiddle() { + jPanel_Right_Middle_Data = new JPanel(new GridLayout(3, 1)); + + cardLayout = new CardLayout(); + + jTextField_Right_Middle_Email.setFont(UIFonts.INPUT_LARGE); + + jPasswordField_Right_Middle_Password.setFont(UIFonts.INPUT_LARGE); + + jPanel_Right_Middle_Email.add(jLabel_Right_Middle_Email); + jPanel_Right_Middle_Email.add(jTextField_Right_Middle_Email); + + jPanel_Right_Middle_Password.add(jLabel_Right_Middle_Password); + jPanel_Right_Middle_Password.add(jPasswordField_Right_Middle_Password); + + jButton_Right_Play.setBackground(UIColors.TEXT_COLOR_L); + jButton_Right_Play.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Right_Play.setFont(UIFonts.BUTTON); + jButton_Right_Play.setPreferredSize(UISizes.SIZE_BUTTON); + + jButton_Right_Play.setCursor(cursor); + + jPanel_Right_Bottom_Button.setLayout(cardLayout); + jPanel_Right_Bottom_Button.add(jButton_Right_Bottom_Submit, "Card1"); + jPanel_Right_Bottom_Button.add(jButton_Right_Play, "Card2"); + + // Add jPanel_Right_Middle_Username and jPanel_Right_Middle_Password directly to + // jPanel_Right + + jPanel_Right_Middle_Data.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_Email); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_Password); + jPanel_Right_Middle_Data.add(jPanel_Right_Bottom_Button); + } + + @Override + public void initRightBottom() { + jLabel_Right_Bottom_Option = new JLabel(UILabels.DONT_HAVE_ACCOUNT); + jButton_Right_Bottom_Others = new JButton(UILabels.SIGN_UP_HERE); + + jButton_Right_Bottom_Forgot_Password.setBackground(UIColors.PRIMARY_COLOR_L); + jButton_Right_Bottom_Forgot_Password.setForeground(UIColors.OTHER_OPTIONS_L); + jButton_Right_Bottom_Forgot_Password.setFont(UIFonts.OTHERS); + jButton_Right_Bottom_Forgot_Password.setBorder(null); + + jButton_Right_Bottom_Forgot_Password.setCursor(cursor); + + jLabel_Right_Bottom_Option.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Bottom_Option.setFont(UIFonts.OTHERS); + jPanel_Right_Bottom_Option.setBackground(UIColors.PRIMARY_COLOR_L); + + jButton_Right_Bottom_Others.setBackground(UIColors.PRIMARY_COLOR_L); + jButton_Right_Bottom_Others.setForeground(UIColors.OTHER_OPTIONS_L); + jButton_Right_Bottom_Others.setFont(UIFonts.OTHERS); + jButton_Right_Bottom_Others.setBorder(null); + jButton_Right_Bottom_Others.setCursor(cursor); + + jPanel_Right_Bottom_Option.add(jLabel_Right_Bottom_Option); + jPanel_Right_Bottom_Option.add(jButton_Right_Bottom_Others); + jPanel_Right_Bottom_Option.add(jButton_Right_Bottom_Forgot_Password); + } + + @Override + public void initRightPanel(){ + jPanel_Right.add(jPanel_Right_Top_Title, BorderLayout.NORTH); + jPanel_Right.add(jPanel_Right_Middle_Data, BorderLayout.CENTER); + jPanel_Right.add(jPanel_Right_Bottom_Option, BorderLayout.SOUTH); + } + + @Override + public void initToggle() { + super.initToggle(); + toggleButton.addEventSelected(selected -> { + if (selected) { + jButton_Right_Play.setForeground(UIColors.PRIMARY_COLOR_D); + jButton_Right_Play.setBackground(UIColors.TEXT_COLOR_D); + } else { + jButton_Right_Play.setBackground(UIColors.TEXT_COLOR_L); + jButton_Right_Play.setForeground(UIColors.PRIMARY_COLOR_L); + } + }); + } + + @Override + public void doAction() { + // TODO Auto-generated method stub + super.doAction(); + + loginController = new SwingLoginController(this); + + jTextField_Right_Middle_Email.addMouseListener(loginController); + jPasswordField_Right_Middle_Password.addMouseListener(loginController); + jButton_Right_Bottom_Submit.addMouseListener(loginController); + jButton_Right_Bottom_Others.addMouseListener(loginController); + jButton_Right_Bottom_Submit.addActionListener(loginController); + jButton_Right_Play.addActionListener(new PlayController(this)); + jPasswordField_Right_Middle_Password.addActionListener(new PressEnter()); + jButton_Right_Bottom_Others.addActionListener(new ClickOtherOption()); + jButton_Right_Bottom_Forgot_Password.addActionListener(new ForgotPasswordController(this, otpVerificationView)); + jButton_Right_Bottom_Forgot_Password.addMouseListener(loginController); + } + + //xu li cac ham o day + public UserEntity getDataWhenLogin() { + return new UserEntity(jTextField_Right_Middle_Email.getText(), String.valueOf(jPasswordField_Right_Middle_Password.getPassword())); + } + + public boolean isEmpty() { + return this.loginModel.isEmpty(this.getDataWhenLogin().getEmail(), this.getDataWhenLogin().getPassword()); + } + + public boolean isAdmin() { + return this.loginModel.isAdmin(this.getDataWhenLogin().getEmail(), this.getDataWhenLogin().getPassword()); + } + + public boolean isMatching() { + return this.loginModel.isMatching(this.getDataWhenLogin().getEmail(), this.getDataWhenLogin().getPassword()); + } + + public void handleSuccess() { + UIPrompts.IS_LOGIN_SUCCESS(); + // Switch to the play button card + this.getCardLayout().next(SwingLoginView.jPanel_Right_Bottom_Button); + //hidden the username and password input field + this.setStatusInputData(false); + } + + //this method for test getLogin above + public void setLogin(String username, String password) { + jTextField_Right_Middle_Email.setText(username); + jPasswordField_Right_Middle_Password.setText(password); + } + + public void setStatusInputData(boolean status) { + jTextField_Right_Middle_Email.setEnabled(status); + jPasswordField_Right_Middle_Password.setEnabled(status); + } + + @Override + public void changeColorBaseOnToggle() { + + } + +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/SwingRegisterView.java b/src/main/java/com/lcaohoanq/formhandling/views/SwingRegisterView.java new file mode 100644 index 0000000..55a8240 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/SwingRegisterView.java @@ -0,0 +1,275 @@ +package com.lcaohoanq.formhandling.views; + +import com.lcaohoanq.formhandling.controllers.RegisterController; +import com.lcaohoanq.formhandling.controllers.ToggleHandler; +import java.awt.BorderLayout; +import java.awt.GridLayout; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import lombok.Getter; +import com.lcaohoanq.formhandling.models.RegisterModel; +import com.lcaohoanq.formhandling.modules.user.UserEntity; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIFonts; +import com.lcaohoanq.formhandling.styles.UILabels; +import com.lcaohoanq.formhandling.views.base.MyFrame; + +@Deprecated +@Getter +public class SwingRegisterView extends MyFrame implements ToggleHandler { + + private RegisterModel registerModel; + + public SwingRegisterView() { + super(); + this.registerModel = new RegisterModel(); + } + + @Override + public void initRight() { + initRightTop(); + initRightMiddle(); + initRightBottom(); + initRightPanel(); + } + + @Override + public void initRightTop() { + jLabel_Right_Top_Title = new JLabel(UILabels.REGISTER, JLabel.CENTER); + jLabel_Right_Top_Title.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Top_Title.setFont(UIFonts.RIGHT_TITLE); + jPanel_Right_Top_Title.add(jLabel_Right_Top_Title); + } + + @Override + public void initRightMiddle() { + jPanel_Right_Middle_Data = new JPanel(new GridLayout(0, 1)); + + jLabel_Right_Middle_FirstName = new JLabel(UILabels.FIRST_NAME); + jLabel_Right_Middle_LastName = new JLabel(UILabels.LAST_NAME); + jLabel_Right_Middle_Confirm_Password = new JLabel(UILabels.CONFIRM_PASSWORD); + + jTextField_Right_Middle_Email.setFont(UIFonts.TEXT_FIELD_REGISTER); + + jLabel_Right_Middle_FirstName.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_FirstName.setFont(UIFonts.LABEL); + jLabel_Right_Middle_FirstName.setBorder(UIBorders.MID_LABEL); + jTextField_Right_Middle_FirstName.setFont(UIFonts.TEXT_FIELD_REGISTER); + jTextField_Right_Middle_FirstName.setBorder(UIBorders.DATA_FIELD); + jTextField_Right_Middle_FirstName.setBackground(UIColors.SECONDARY_COLOR_L); + jTextField_Right_Middle_FirstName.setForeground(UIColors.TEXT_COLOR_L); + + jLabel_Right_Middle_LastName.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_LastName.setFont(UIFonts.LABEL); + jLabel_Right_Middle_LastName.setBorder(UIBorders.MID_LABEL); + jTextField_Right_Middle_LastName.setFont(UIFonts.TEXT_FIELD_REGISTER); + jTextField_Right_Middle_LastName.setBorder(UIBorders.DATA_FIELD); + jTextField_Right_Middle_LastName.setBackground(UIColors.SECONDARY_COLOR_L); + jTextField_Right_Middle_LastName.setForeground(UIColors.TEXT_COLOR_L); + + jPasswordField_Right_Middle_Password.setFont(UIFonts.TEXT_FIELD_REGISTER); + + jLabel_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_Confirm_Password.setFont(UIFonts.LABEL); + jLabel_Right_Middle_Confirm_Password.setBorder(UIBorders.MID_LABEL); + jPasswordField_Right_Middle_Confirm_Password.setFont(UIFonts.TEXT_FIELD_REGISTER); + jPasswordField_Right_Middle_Confirm_Password.setBorder(UIBorders.DATA_FIELD); + jPasswordField_Right_Middle_Confirm_Password.setBackground(UIColors.SECONDARY_COLOR_L); + jPasswordField_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_L); + + jPanel_Right_Middle_Email.add(jLabel_Right_Middle_Email); + jPanel_Right_Middle_Email.add(jTextField_Right_Middle_Email); + + jPanel_Right_Middle_FirstName.setLayout( + new BoxLayout(jPanel_Right_Middle_FirstName, BoxLayout.Y_AXIS)); + jPanel_Right_Middle_FirstName.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_FirstName.add(jLabel_Right_Middle_FirstName); + jPanel_Right_Middle_FirstName.add(jTextField_Right_Middle_FirstName); + jPanel_Right_Middle_FirstName.setBorder(UIBorders.MIDDLE); + + jPanel_Right_Middle_LastName.setLayout( + new BoxLayout(jPanel_Right_Middle_LastName, BoxLayout.Y_AXIS)); + jPanel_Right_Middle_LastName.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_LastName.add(jLabel_Right_Middle_LastName); + jPanel_Right_Middle_LastName.add(jTextField_Right_Middle_LastName); + jPanel_Right_Middle_LastName.setBorder(UIBorders.MIDDLE); + + jPanel_Right_Middle_Password.add(jLabel_Right_Middle_Password); + jPanel_Right_Middle_Password.add(jPasswordField_Right_Middle_Password); + + jPanel_Right_Middle_Confirm_Password + .setLayout(new BoxLayout(jPanel_Right_Middle_Confirm_Password, BoxLayout.Y_AXIS)); + jPanel_Right_Middle_Confirm_Password.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_Confirm_Password.add(jLabel_Right_Middle_Confirm_Password); + jPanel_Right_Middle_Confirm_Password.add(jPasswordField_Right_Middle_Confirm_Password); + jPanel_Right_Middle_Confirm_Password.setBorder(UIBorders.MIDDLE); + + jPanel_Right_Bottom_Button.add(jButton_Right_Bottom_Submit); + // Add jPanel_Right_Middle_Username and jPanel_Right_Middle_Password directly to + // jPanel_Right + + jPanel_Right_Middle_Data.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_Email); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_FirstName); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_LastName); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_Password); + jPanel_Right_Middle_Data.add(jPanel_Right_Middle_Confirm_Password); + jPanel_Right_Middle_Data.add(jPanel_Right_Bottom_Button); + } + + @Override + public void initRightBottom() { + jLabel_Right_Bottom_Option = new JLabel(UILabels.HAVE_ACCOUNT); + jButton_Right_Bottom_Others = new JButton(UILabels.SIGN_IN_HERE); + + jLabel_Right_Bottom_Option.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Bottom_Option.setFont(UIFonts.OTHERS); + jPanel_Right_Bottom_Option.setBackground(UIColors.PRIMARY_COLOR_L); + + jButton_Right_Bottom_Others.setBackground(UIColors.PRIMARY_COLOR_L); + jButton_Right_Bottom_Others.setForeground(UIColors.OTHER_OPTIONS_L); + jButton_Right_Bottom_Others.setFont(UIFonts.OTHERS); + jButton_Right_Bottom_Others.setBorder(null); + jButton_Right_Bottom_Others.setCursor(cursor); + + jPanel_Right_Bottom_Option.add(jLabel_Right_Bottom_Option); + jPanel_Right_Bottom_Option.add(jButton_Right_Bottom_Others); + } + + @Override + public void initRightPanel() { + jPanel_Right.add(jPanel_Right_Top_Title, BorderLayout.NORTH); + jPanel_Right.add(jPanel_Right_Middle_Data, BorderLayout.CENTER); + jPanel_Right.add(jPanel_Right_Bottom_Option, BorderLayout.SOUTH); + } + + @Override + public void initToggle() { + super.initToggle(); + toggleButton.addEventSelected(selected -> { + if (selected) { + jLabel_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_D); + jLabel_Right_Middle_Confirm_Password.setBackground(UIColors.PRIMARY_COLOR_D); + jPasswordField_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_D); + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_D); + jPanel_Right_Middle_Confirm_Password.setBackground(UIColors.PRIMARY_COLOR_D); + } else { + jLabel_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_Confirm_Password.setBackground(UIColors.PRIMARY_COLOR_L); + jPasswordField_Right_Middle_Confirm_Password.setForeground(UIColors.TEXT_COLOR_L); + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_L); + jPanel_Right_Middle_Confirm_Password.setBackground(UIColors.PRIMARY_COLOR_L); + } + }); + } + + @Override + public void doAction() { + // TODO Auto-generated method stub + super.doAction(); + jTextField_Right_Middle_Email.addMouseListener(new RegisterController(this)); + jTextField_Right_Middle_FirstName.addMouseListener(new RegisterController(this)); + jTextField_Right_Middle_LastName.addMouseListener(new RegisterController(this)); + jPasswordField_Right_Middle_Password.addMouseListener(new RegisterController(this)); + jPasswordField_Right_Middle_Confirm_Password.addMouseListener(new RegisterController(this)); + jButton_Right_Bottom_Submit.addMouseListener(new RegisterController(this)); + jButton_Right_Bottom_Others.addMouseListener(new RegisterController(this)); + jButton_Right_Bottom_Submit.addActionListener(new RegisterController(this)); + jPasswordField_Right_Middle_Confirm_Password.addActionListener(new PressEnter()); + jButton_Right_Bottom_Others.addActionListener(new ClickOtherOption()); + } + + public boolean isMatchingPattern() { + boolean isMatching = false; + //matching accept both email and phonenumber from Vietnam + if (!this.registerModel.isEmailFormat(this.getDataWhenRegister().getEmail())) { + UIPrompts.IS_WRONG_FORMAT_EMAIL(); + } else if (!this.registerModel.isNameFormat(this.getDataWhenRegister().getFirstName())) { + UIPrompts.IS_WRONG_FORMAT_NAME(); + } else if (!this.registerModel.isNameFormat(this.getDataWhenRegister().getLastName())) { + UIPrompts.IS_WRONG_FORMAT_NAME(); + } else if (!this.registerModel.isPasswordFormat(this.getDataWhenRegister().getPassword())) { + UIPrompts.IS_WRONG_FORMAT_PASSWORD(); + } else { + isMatching = true; + } + return isMatching; + } + + public boolean isMatchingPasswordAndConfirmPassword() { + return this.registerModel.isMatching(this.getDataWhenRegister().getPassword(), + this.getDataWhenRegister().getConfirmPassword()); + } + + public boolean isDuplicateEmail() { + return this.registerModel.isDuplicateEmail(this.getDataWhenRegister().getEmail()); + } + + public boolean isEmpty() { + return this.registerModel.isEmpty(this.getDataWhenRegister().getEmail(), + this.getDataWhenRegister().getFirstName(), this.getDataWhenRegister().getLastName(), + this.getDataWhenRegister().getPassword(), + this.getDataWhenRegister().getConfirmPassword()); + } + + //xu li cac ham o day + public UserEntity getDataWhenRegister() { + return new UserEntity(jTextField_Right_Middle_Email.getText(), + jTextField_Right_Middle_FirstName.getText(), + jTextField_Right_Middle_LastName.getText(), + String.valueOf(jPasswordField_Right_Middle_Password.getPassword()), + String.valueOf( + jPasswordField_Right_Middle_Confirm_Password.getPassword())); + } + + public void insertMail() { + registerModel.insertMail(this.getDataWhenRegister().getEmail(), + this.getDataWhenRegister().getFirstName(), this.getDataWhenRegister().getLastName(), + this.getDataWhenRegister().getPassword()); + } + + public void handleNotMatchingPasswordAndConfirmPassword() { + UIPrompts.IS_NOT_MATCH_PASSWORD_AND_CONFIRM_PASSWORD(); + } + + //this method for test getRegister above + public void setRegister(String email, String firstName, String lastName, String password, + String confirmPassword) { + jTextField_Right_Middle_Email.setText(email); + jTextField_Right_Middle_FirstName.setText(firstName); + jTextField_Right_Middle_LastName.setText(lastName); + jPasswordField_Right_Middle_Password.setText(password); + jPasswordField_Right_Middle_Confirm_Password.setText(confirmPassword); + } + + public void setHoverConfirmPassword(boolean isInside, String mode) { + if (isInside) { + if (mode.equals("light")) { + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_L_HOVER); + } else { + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_D_HOVER); + } + } else { + if (mode.equals("light")) { + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_L); + } else { + jPasswordField_Right_Middle_Confirm_Password.setBackground( + UIColors.SECONDARY_COLOR_D); + } + } + } + + @Override + public void changeColorBaseOnToggle() { + + } + +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/base/AppComponent.java b/src/main/java/com/lcaohoanq/formhandling/views/base/AppComponent.java new file mode 100644 index 0000000..aba3eff --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/base/AppComponent.java @@ -0,0 +1,200 @@ +package com.lcaohoanq.formhandling.views.base; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.BorderLayout; +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.GridLayout; +import java.awt.Image; +import java.awt.Toolkit; +import javax.swing.BoxLayout; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; +import javax.swing.SwingConstants; +import lombok.Getter; +import com.lcaohoanq.formhandling.modules.sound.AudioHandler; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIFonts; +import com.lcaohoanq.formhandling.styles.UILabels; +import com.lcaohoanq.formhandling.styles.UISizes; + +@Getter +public abstract class AppComponent extends JFrame { + + public static JPanel jPanel_Right_Bottom_Button = new JPanel();; + protected final JMenu jMenu = new JMenu("HELP"); + protected final JMenu jMenu_Play_Here = new JMenu("Play without login"); + protected final JMenuItem jMenuItem_AboutMe = new JMenuItem("About me"); + protected final JMenuItem jMenuItem_Score = new JMenuItem("Show Score"); + protected final JMenuItem jMenuItem_Go = new JMenuItem("Go!!!"); + protected JTextField jTextField_Right_Middle_Email = new JTextField(20); // 20 is the number of columns + protected JTextField jTextField_Right_Middle_FirstName = new JTextField(20);; // 20 is the number of columns + protected JTextField jTextField_Right_Middle_LastName = new JTextField(20);; // 20 is the number of columns + protected JPasswordField jPasswordField_Right_Middle_Password = new JPasswordField(20);; + protected JPasswordField jPasswordField_Right_Middle_Confirm_Password = new JPasswordField(20);; + protected JButton jButton_Right_Bottom_Submit = new JButton(UILabels.SUBMIT);; + protected JButton jButton_Right_Bottom_Forgot_Password = new JButton(UILabels.FORGOT_PASSWORD);; + protected JButton jButton_Right_Bottom_Others; + protected JPanel jPanel_Container = new JPanel(); + protected JPanel jPanel_Right = new JPanel(new BorderLayout()); + protected JPanel jPanel_Right_Top_Title = new JPanel();; + protected JLabel jLabel_Right_Top_Title; + protected JPanel jPanel_Right_Middle_Email = new JPanel(); + protected JPanel jPanel_Right_Middle_FirstName = new JPanel(); + protected JPanel jPanel_Right_Middle_LastName = new JPanel(); + protected JLabel jLabel_Right_Middle_Email; + protected JLabel jLabel_Right_Middle_FirstName; + protected JLabel jLabel_Right_Middle_LastName; + protected JPanel jPanel_Right_Middle_Password = new JPanel();; + protected JLabel jLabel_Right_Middle_Password; + protected JLabel jLabel_Right_Middle_Confirm_Password; + protected JPanel jPanel_Right_Middle_Confirm_Password = new JPanel(); + protected JPanel jPanel_Right_Middle_Data; + protected JButton jButton_Right_Play = new JButton(UILabels.PLAY);; + protected JPanel jPanel_Right_Bottom_Option = new JPanel();; + protected JLabel jLabel_Right_Bottom_Option; + protected JPanel jPanel_Left_Icon = new JPanel(new BorderLayout());; + protected AudioHandler audioHandler = new AudioHandler(); + protected JPanel jPanel_Left = new JPanel(new BorderLayout());; + protected JLabel jLabel_Left_Icon = new JLabel( + new ImageIcon(new ImageIcon( + Toolkit.getDefaultToolkit().getImage(MyFrame.class.getResource(ResourcePaths.URL_SNAKE_LOGO))).getImage().getScaledInstance(250, 250, Image.SCALE_SMOOTH)));; + protected JMenuBar jMenuBar = new JMenuBar();; + protected Cursor cursor = new Cursor(Cursor.HAND_CURSOR); + + // MenuView + public JButton jButton_Mode_Classic = new JButton("Classic");; + public JButton jButton_Mode_Modern = new JButton("Modern");; + public JButton jButton_Mode_Campaign = new JButton("Campaign");; + protected JPanel jPanel_Container_MenuView = new JPanel(new BorderLayout());; + protected JLabel jLabel_Title_MenuView = new JLabel("Snake Game", JLabel.CENTER);; + protected JPanel jPanel_Button_MenuView = new JPanel(new GridLayout(3, 1, 30, 20));; + protected JMenuBar jMenuBar_MenuView; + protected final JMenu jMenu_Back_To_Main_Menu = new JMenu("Back to"); + protected final JMenuItem jMenuItem_Back_To_Main_Menu = new JMenuItem("main menu"); + protected final JMenu jMenu_Sound = new JMenu("Turn Sound"); + protected final JMenuItem jMenuItem_Sound_On = new JMenuItem("On"); + protected final JMenuItem jMenuItem_Sound_Off = new JMenuItem("Off"); + protected final JMenu jMenu_Log_Out = new JMenu("Others"); + protected final JMenuItem jMenuItem_Log_Out = new JMenuItem("Logout"); + + // MenuModern + //6 button: No Maze, Box, Tunnel, Mill, Rails, Apartment + protected JPanel jPanel_Container_MenuModern = new JPanel(); + protected JPanel jPanel_Menu_Modern = new JPanel(new GridLayout(6, 1, 30, 20)); + protected JLabel jLabel_Title_Modern = new JLabel("Snake Game"); + protected JButton jButton_NoMaze = new JButton("No Maze"); + protected JButton jButton_Box = new JButton("Box"); + protected JButton jButton_Tunnel = new JButton("Tunnel"); + protected JButton jButton_Mill = new JButton("Mill"); + protected JButton jButton_Rails = new JButton("Rails"); + protected JButton jButton_Apartment = new JButton("Apartment"); + + public AppComponent() { + super(); + doStyling(); + } + public abstract void initComponents(); + public abstract void doAction(); + private void doStyling(){ + jPanel_Right_Bottom_Button.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Bottom_Button.setBorder(UIBorders.BUTTON); + + jPanel_Right.setPreferredSize(new Dimension(UISizes.WIDTH_MY_RIGHT_FRAME, UISizes.HEIGHT_MY_RIGHT_FRAME)); + jPanel_Right.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right.setBorder(UIBorders.MID_FIELD); + + jPanel_Right_Top_Title.setBackground(UIColors.PRIMARY_COLOR_L); + + jPanel_Right_Top_Title.setBorder(UIBorders.TITLE); + + jLabel_Right_Middle_Email = new JLabel(UILabels.EMAIL_PHONE); + jLabel_Right_Middle_Email.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_Email.setFont(UIFonts.LABEL); + jLabel_Right_Middle_Email.setBorder(UIBorders.MID_LABEL); + jTextField_Right_Middle_Email.setBorder(UIBorders.DATA_FIELD); + jTextField_Right_Middle_Email.setBackground(UIColors.SECONDARY_COLOR_L); + jTextField_Right_Middle_Email.setForeground(UIColors.TEXT_COLOR_L); + jPanel_Right_Middle_Email.setLayout( + new BoxLayout(jPanel_Right_Middle_Email, BoxLayout.Y_AXIS)); + jPanel_Right_Middle_Email.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_Email.setBorder(UIBorders.MIDDLE); + + jLabel_Right_Middle_Password = new JLabel(UILabels.PASSWORD); + jLabel_Right_Middle_Password.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Right_Middle_Password.setFont(UIFonts.LABEL); + jLabel_Right_Middle_Password.setBorder(UIBorders.MID_LABEL); + jPasswordField_Right_Middle_Password.setBorder(UIBorders.DATA_FIELD); + jPasswordField_Right_Middle_Password.setBackground(UIColors.SECONDARY_COLOR_L); + jPasswordField_Right_Middle_Password.setForeground(UIColors.TEXT_COLOR_L); + jPanel_Right_Middle_Password.setLayout( + new BoxLayout(jPanel_Right_Middle_Password, BoxLayout.Y_AXIS)); + jPanel_Right_Middle_Password.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Right_Middle_Password.setBorder(UIBorders.MIDDLE); + + jButton_Right_Bottom_Submit.setBackground(UIColors.TEXT_COLOR_L); + jButton_Right_Bottom_Submit.setForeground(UIColors.PRIMARY_COLOR_L); + jButton_Right_Bottom_Submit.setFont(UIFonts.BUTTON); + jButton_Right_Bottom_Submit.setPreferredSize(UISizes.SIZE_BUTTON); + jButton_Right_Bottom_Submit.setCursor(cursor); + + stylingMenuView(); + stylingMenuModern(); + } + + private void stylingMenuView(){ + jLabel_Title_MenuView.setFont(UIFonts.RIGHT_TITLE); + jLabel_Title_MenuView.setBorder(UIBorders.CONTAINER_MENU); + + jButton_Mode_Classic.setFont(UIFonts.BUTTON); + jButton_Mode_Modern.setFont(UIFonts.BUTTON); + jButton_Mode_Campaign.setFont(UIFonts.BUTTON); + } + + private void stylingMenuModern(){ + jLabel_Title_Modern.setFont(UIFonts.RIGHT_TITLE); + jLabel_Title_Modern.setBackground(UIColors.PRIMARY_COLOR_L); + jLabel_Title_Modern.setForeground(UIColors.TEXT_COLOR_L); + jLabel_Title_Modern.setBorder(UIBorders.TITLE); + jLabel_Title_Modern.setHorizontalAlignment(SwingConstants.CENTER); + + jButton_NoMaze.setFont(UIFonts.BUTTON); + jButton_NoMaze.setBackground(UIColors.TEXT_COLOR_L); + jButton_NoMaze.setForeground(UIColors.PRIMARY_COLOR_L); + + jButton_Box.setFont(UIFonts.BUTTON); + jButton_Box.setBackground(UIColors.TEXT_COLOR_L); + jButton_Box.setForeground(UIColors.PRIMARY_COLOR_L); + + jButton_Tunnel.setFont(UIFonts.BUTTON); + jButton_Tunnel.setBackground(UIColors.TEXT_COLOR_L); + jButton_Tunnel.setForeground(UIColors.PRIMARY_COLOR_L); + + jButton_Mill.setFont(UIFonts.BUTTON); + jButton_Mill.setBackground(UIColors.TEXT_COLOR_L); + jButton_Mill.setForeground(UIColors.PRIMARY_COLOR_L); + + jButton_Rails.setFont(UIFonts.BUTTON); + jButton_Rails.setBackground(UIColors.TEXT_COLOR_L); + jButton_Rails.setForeground(UIColors.PRIMARY_COLOR_L); + + jButton_Apartment.setFont(UIFonts.BUTTON); + jButton_Apartment.setBackground(UIColors.TEXT_COLOR_L); + jButton_Apartment.setForeground(UIColors.PRIMARY_COLOR_L); + + jPanel_Menu_Modern.setBorder(UIBorders.TITLE); + jPanel_Menu_Modern.setBackground(UIColors.PRIMARY_COLOR_L); + + jPanel_Container_MenuModern.setBackground(UIColors.PRIMARY_COLOR_L); + } + +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/base/Board.java b/src/main/java/com/lcaohoanq/formhandling/views/base/Board.java new file mode 100644 index 0000000..ebc03e2 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/base/Board.java @@ -0,0 +1,511 @@ +package com.lcaohoanq.formhandling.views.base; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import com.lcaohoanq.formhandling.controllers.SwingLoginController; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.io.InputStream; +import java.util.Objects; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JProgressBar; +import javax.swing.SwingUtilities; +import javax.swing.Timer; +import com.lcaohoanq.formhandling.modules.sound.AudioHandler; +import com.lcaohoanq.formhandling.modules.user.UserDAO; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIFonts; +import com.lcaohoanq.formhandling.styles.UILabels; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.MenuView; +import com.lcaohoanq.formhandling.views.UIPrompts; + +public abstract class Board extends JPanel implements ActionListener { + + // Board dimensions and settings + protected final int DOT_SIZE = 10; // Size of the snake's body + protected final int RAND_POS = 29; // Random positioning parameter + private final int ALL_DOTS = 900; // Maximum number of dots on the board + // Snake position and movement + protected final int[] x = new int[ALL_DOTS]; // X-coordinate of each snake dot + protected final int[] y = new int[ALL_DOTS]; // Y-coordinate of each snake dot + private final int DELAY = 50; // Timer delay for the game loop + private final int DECREASE_DELAY = 2; // Decrease delay for faster snake movement + protected boolean inGame = true; // Flag indicating whether the game is currently active + protected int dots; // Current number of snake dots + protected int apple_count = 0; // Counter for regular apples + protected int apple_x; // X-coordinate of a regular apple + protected int bigApple_x; // X-coordinate of a big apple + protected int bigApple_y; // Y-coordinate of a big apple + protected int apple_y; // Y-coordinate of a regular apple + // Game timers and images + protected Timer timer; // Timer for regular game events + protected AudioHandler audioHandler = new AudioHandler(); + // Snake movement directions + protected boolean leftDirection = false; // Flag for moving left + protected boolean rightDirection = true; // Flag for moving right + protected boolean upDirection = false; // Flag for moving up + protected boolean downDirection = false; // Flag for moving down + // Game state variables + private int score = 0; // Player's score + private Timer bigAppleTimer; // Timer for big apple appearance + private Image ball; // Snake body image + private Image apple; // Regular apple image + private Image head; // Snake head image + private Image bigApple; // Big apple image + // UI components + private JLabel gameOverLabel; // Label to display the "Game Over" message + private JPanel gameOverPanel; // Panel for UI components at the game over + private JButton playAgainButton; // Button to play the game again + private JButton exitButton; // Button to exit the game + private JPanel playAgainExitButtonPanel; // Panel for UI components at the game over + private JButton backToMainMenuButton; // Button to go back to the main menu + private JPanel backToMainMenuButtonPanel; // Panel for UI components at the game over + private JLabel scoreLabel; // Label to display the player's score + private int lineBottom; // Bottom line + private JProgressBar bigAppleProgressBar; // Progress bar for big apple timer + private final JPanel bottomPanel = new JPanel(); // Panel for UI components at the bottom + private final JPanel gameOverButtonPanel = new JPanel(); // Panel for UI components at the game over + + public Board() { + initBoard(); + } + + private void initBoard() { + addKeyListener(new TAdapter()); + setBackground(UIColors.OTHER_OPTIONS_L); + setFocusable(true); + bottomPanel.setVisible(true); + setPreferredSize(UISizes.SIZE_BOARD); + + setLayout(new BorderLayout()); + loadImages(); + initGame(); + + initBottomPanel(); + initLine(); + initGameOverPanel(); + } + + private void initLine() { + lineBottom = + UISizes.HEIGHT_BOARD - UISizes.LINE_SPACE_FROM_BOTTOM; // Adjust this value as needed + } + + private void initScoreLabel() { + // Initialize the JLabel for live score display + scoreLabel = new JLabel(UILabels.SCORE_LIVE); + scoreLabel.setForeground(Color.white); + scoreLabel.setFont(UIFonts.SCORE_LIVE); + scoreLabel.setBounds(10, UISizes.HEIGHT_BOARD - 30, 100, 20); + scoreLabel.setVisible(true); + } + + private void initProgressBar() { + // Initialize the JProgressBar for big apple countdown + bigAppleProgressBar = new JProgressBar(UISizes.MIN_PROGRESS_BAR, UISizes.MAX_PROGRESS_BAR); + bigAppleProgressBar.setPreferredSize(UISizes.SIZE_PROGRESS_BAR); + bigAppleProgressBar.setValue(100); + bigAppleProgressBar.setStringPainted(true); + bigAppleProgressBar.setForeground(UIColors.PROGRESS_BAR_LOADING); + bigAppleProgressBar.setBackground(UIColors.PRIMARY_COLOR_L); + bigAppleProgressBar.setVisible(false); + } + + private void initBottomPanel() { + initScoreLabel(); + initProgressBar(); + bottomPanel.setLayout(new BorderLayout()); + bottomPanel.setBackground(UIColors.OTHER_OPTIONS_L); + bottomPanel.setBorder(UIBorders.BOTTOM_SCORE_PROGRESS_BAR); + bottomPanel.add(scoreLabel, BorderLayout.WEST); + bottomPanel.add(bigAppleProgressBar, BorderLayout.EAST); + add(bottomPanel, BorderLayout.SOUTH); + } + + protected void renderProgressBar() { + // Display the progress bar + bigAppleProgressBar.setVisible(true); + // Start the progress bar + bigAppleProgressBar.setValue(100); + // Start the timer + Timer progressBarTimer = new Timer(45, e -> { + int value = bigAppleProgressBar.getValue(); + if (value > 0) { + bigAppleProgressBar.setValue(value - 1); + } else { + ((Timer) e.getSource()).stop(); + } + }); + progressBarTimer.start(); + } + + private void initGameOverTitle() { + gameOverPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + gameOverLabel = new JLabel(UILabels.GAME_OVER); + gameOverPanel.setBackground(UIColors.OTHER_OPTIONS_L); + gameOverLabel.setForeground(UIColors.PRIMARY_COLOR_L); + gameOverLabel.setBackground(UIColors.OTHER_OPTIONS_L); + gameOverLabel.setFont(UIFonts.GAME_OVER); + gameOverLabel.setBounds((UISizes.WIDTH_BOARD - 260) / 2, + (UISizes.HEIGHT_BOARD - 50) / 2 - 50, 260, 50); + gameOverPanel.add(gameOverLabel); + } + + private void initPlayAgainButton() { + playAgainButton = new JButton(UILabels.PLAY_AGAIN); + playAgainButton.setFont(UIFonts.PLAY_EXIT_BUTTON); + playAgainButton.setBackground(UIColors.TEXT_COLOR_L); + playAgainButton.setForeground(UIColors.PRIMARY_COLOR_L); + playAgainButton.setPreferredSize(UISizes.SIZE_BUTTON_GAME_OVER); + playAgainButton.addActionListener(e -> { + SwingUtilities.invokeLater(() -> { + // Reset game parameters and restart the game + resetGame(); + }); + }); + } + + private void initExitButton() { + exitButton = new JButton(UILabels.EXIT); + exitButton.setFont(UIFonts.PLAY_EXIT_BUTTON); + exitButton.setBackground(UIColors.PROGRESS_BAR_LOADING); + exitButton.setForeground(UIColors.PRIMARY_COLOR_L); + exitButton.addActionListener(e -> { + if (UIPrompts.IS_CONFIRM_EXIT() == JOptionPane.YES_OPTION) { + SwingUtilities.getWindowAncestor(this).dispose(); + } + }); + exitButton.setPreferredSize(UISizes.SIZE_BUTTON_GAME_OVER); + } + + private void initPlayAgainExitButtonPanel() { + initPlayAgainButton(); + initExitButton(); + playAgainExitButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + playAgainExitButtonPanel.setBackground(UIColors.OTHER_OPTIONS_L); + playAgainExitButtonPanel.add(playAgainButton); + playAgainExitButtonPanel.add(exitButton); + } + + private void initBackToMainMenuButton() { + backToMainMenuButton = new JButton(UILabels.BACK_TO_MAIN_MENU); + backToMainMenuButton.setFont(UIFonts.PLAY_EXIT_BUTTON); + backToMainMenuButton.setBackground(UIColors.BACK_TO_MAIN_MENU); + backToMainMenuButton.setForeground(UIColors.PRIMARY_COLOR_L); + backToMainMenuButton.addActionListener(e -> { + SwingUtilities.getWindowAncestor(this).dispose(); + new MenuView().setVisible(true); + }); + backToMainMenuButton.setPreferredSize(UISizes.SIZE_BUTTON_GAME_OVER_BACK_TO_MAIN_MENU); + } + + private void initBackToMainMenuButtonPanel() { + initBackToMainMenuButton(); + backToMainMenuButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + backToMainMenuButtonPanel.setBackground(UIColors.OTHER_OPTIONS_L); + backToMainMenuButtonPanel.add(backToMainMenuButton); + } + + private void initGameOverPanel() { + initGameOverTitle(); + initPlayAgainExitButtonPanel(); + initBackToMainMenuButtonPanel(); + gameOverButtonPanel.setLayout(new BorderLayout()); + gameOverButtonPanel.setBackground(UIColors.OTHER_OPTIONS_L); + gameOverButtonPanel.setBorder(UIBorders.GAME_OVER_ELEMENT); + gameOverButtonPanel.add(gameOverPanel, BorderLayout.NORTH); + gameOverButtonPanel.add(playAgainExitButtonPanel, BorderLayout.CENTER); + gameOverButtonPanel.add(backToMainMenuButtonPanel, BorderLayout.SOUTH); + gameOverButtonPanel.setVisible(false); + add(gameOverButtonPanel, BorderLayout.CENTER); + } + + protected void loadImages() { + + ball = new ImageIcon(getClass().getResource(ResourcePaths.URL_DOT)).getImage(); + + apple = new ImageIcon(getClass().getResource(ResourcePaths.URL_APPLE)).getImage(); + + head = new ImageIcon(getClass().getResource(ResourcePaths.URL_HEAD)).getImage(); + + bigApple = new ImageIcon(getClass().getResource(ResourcePaths.URL_BIG_APPLE)).getImage(); + } + + private void initGame() { + + dots = 3; + for (int z = 0; z < dots; z++) { + x[z] = 50 - z * 10; + y[z] = 50; + } + + locateApple(); + timer = new Timer(DELAY, this); + timer.start(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + + doDrawing(g); + g.setColor(UIColors.PRIMARY_COLOR_L); + g.drawLine(0, lineBottom, UISizes.WIDTH_BOARD, lineBottom); + } + + private void doDrawing(Graphics g) { + if (inGame) { + if (apple_count % 5 == 0 && apple_count != 0) { + g.drawImage(bigApple, bigApple_x, bigApple_y, this); + } else { + g.drawImage(apple, apple_x, apple_y, this); + } + scoreLabel.setText("Score: " + score); + for (int z = 0; z < dots; z++) { + if (z == 0) { + g.drawImage(head, x[z], y[z], this); + } else { + g.drawImage(ball, x[z], y[z], this); + } + } + + Toolkit.getDefaultToolkit().sync(); + } else { + gameOver(g); + updateScore(); + } + } + + public int compareDatabaseAndCurrentScore(int dbScore, int currentScore) { + return dbScore - currentScore; + } + + public int handleScore(String username) { + UserDAO executeQuery = UserDAO.getInstance(); + int currentScore = this.score; + int dbScore = Objects.requireNonNull(executeQuery.selectEmailAndScoreByEmail(username)) + .getScore(); + return compareDatabaseAndCurrentScore(dbScore, currentScore); + } + + public void updateScore() { + UserDAO executeQuery = UserDAO.getInstance(); + String username = SwingLoginController.email; + if (username.isEmpty()) { + return; + } + // if the current score > db score, update the score in the database + if (handleScore(username) < 0) { + if (executeQuery.setSafeUpdate() == 0) { + executeQuery.updateEmailScore(username, String.valueOf(this.score)); + } + } + } + + private void gameOver(Graphics g) { + // Show the "Play Again" and "Exit" button after displaying "Game Over" message + gameOverButtonPanel.setVisible(true); + playAgainButton.setVisible(true); + exitButton.setVisible(true); + backToMainMenuButton.setVisible(true); +// Hide the progress bar + bigAppleProgressBar.setVisible(false); + } + + private void resetGame() { + // Reset game variables here + // For example: + score = 0; + dots = 3; + apple_count = 0; + inGame = true; + bigApple_x = -100; + bigApple_y = -100; + // Reset the snake's position + for (int z = 0; z < dots; z++) { + x[z] = 50 - z * 10; + y[z] = 50; + } + // Reset any other necessary game state variables + rightDirection = true; + leftDirection = false; + upDirection = false; + downDirection = false; + // Hide the "Play Again" button again + playAgainButton.setVisible(false); + exitButton.setVisible(false); + backToMainMenuButton.setVisible(false); + // Ensure that the gameOverButtonPanel is not visible + gameOverButtonPanel.setVisible(false); + // Restart the timer and initialize the game + timer.stop(); + initGame(); + timer.start(); + } + + private void checkApple() { + + if ((x[0] == apple_x) && (y[0] == apple_y)) { + dots++; + checkScore(); + apple_count++; + locateApple(); + if (score % 5 != 0) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream( + ResourcePaths.URL_EATING2); + audioHandler.playAudio(inputStream); + } + } + return; + } + if ((x[0] >= bigApple_x) && (x[0] <= bigApple_x + 2 * DOT_SIZE) + && (y[0] >= bigApple_y) && (y[0] <= bigApple_y + 2 * DOT_SIZE)) { + dots += 5; + checkBigScore(); + + // change the game speed + int newDelay = Math.max(timer.getDelay() - DECREASE_DELAY, 0); + timer.setDelay(newDelay); + + // disable the big apple progress bar + bigAppleProgressBar.setVisible(false); + + // check the big apple are eaten + bigAppleTimer.stop(); + + apple_count = 0; + locateApple(); + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_EATING); + audioHandler.playAudio(inputStream); + } + } + } + + private void checkScore() { + score++; + } + + private void checkBigScore() { + score += 5; + } + + private void move() { + + for (int z = dots; z > 0; z--) { + x[z] = x[(z - 1)]; + y[z] = y[(z - 1)]; + } + + if (leftDirection) { + x[0] -= DOT_SIZE; + } + + if (rightDirection) { + x[0] += DOT_SIZE; + } + + if (upDirection) { + y[0] -= DOT_SIZE; + } + + if (downDirection) { + y[0] += DOT_SIZE; + } + } + + protected abstract void checkCollision(); + + protected abstract void locateApple(); + + protected abstract void locateBigApple(); + + public void setBigAppleTime() { + // neu ma bigAppleTimer dang null thi tao mot timer moi + if (bigAppleTimer != null) { + bigAppleTimer.stop(); + } + + // Apple-related variables + // Timer for big apple appearance + int BIG_APPLE_TIMER = 5000; + bigAppleTimer = new Timer(BIG_APPLE_TIMER, e -> { + bigAppleTimer.stop(); + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream( + ResourcePaths.URL_BIG_APPLE_DIS); + audioHandler.playAudio(inputStream); + } + apple_count = 0; + locateApple(); + bigAppleProgressBar.setVisible(false); + }); + bigAppleProgressBar.setVisible(true); + bigAppleTimer.start(); + } + + protected boolean isOnSound() { + System.out.println("check is On Sound: " + !audioHandler.isEmptyPath()); + return !audioHandler.isEmptyPath(); + } + + @Override + public void actionPerformed(ActionEvent e) { + + if (inGame) { + + checkApple(); + checkCollision(); + move(); + } + + repaint(); + } + + private class TAdapter extends KeyAdapter { + + @Override + public void keyPressed(KeyEvent e) { + + int key = e.getKeyCode(); + + if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) { + leftDirection = true; + upDirection = false; + downDirection = false; + } + + if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) { + rightDirection = true; + upDirection = false; + downDirection = false; + } + + if ((key == KeyEvent.VK_UP) && (!downDirection)) { + upDirection = true; + rightDirection = false; + leftDirection = false; + } + + if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { + downDirection = true; + rightDirection = false; + leftDirection = false; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/lcaohoanq/formhandling/views/base/MyFrame.java b/src/main/java/com/lcaohoanq/formhandling/views/base/MyFrame.java new file mode 100644 index 0000000..c07a29a --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/base/MyFrame.java @@ -0,0 +1,221 @@ +package com.lcaohoanq.formhandling.views.base; + +import com.lcaohoanq.formhandling.constants.Info; +import com.lcaohoanq.formhandling.controllers.ScoreController; +import com.lcaohoanq.formhandling.controllers.ToggleHandler; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Desktop; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.ActionListener; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import javax.swing.JComponent; +import javax.swing.JFrame; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIColors; +import com.lcaohoanq.formhandling.styles.UIImages; +import com.lcaohoanq.formhandling.styles.UILabels; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.MenuView; +import com.lcaohoanq.formhandling.views.ScoreView; + +public abstract class MyFrame extends AppComponent implements ToggleHandler{ + + public MyFrame() { + setTitle(UILabels.WINDOW); + setSize(UISizes.WIDTH_MY_FRAME, UISizes.HEIGHT_MY_FRAME); + setIconImage(UIImages.icon); + setLocationRelativeTo(null); + setResizable(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + initUI(); + } + + protected void initUI() { + initComponents(); + doAction(); + } + + @Override + public void initComponents(){ + initMenu(); + initLeft(); + initRight(); + initToggle(); + initContainer(); + } + + private void initMenu() { + jMenu_Play_Here.add(jMenuItem_Go); + jMenuBar.setLayout(new FlowLayout(FlowLayout.LEFT)); + jMenuBar.setBorder(UIBorders.LINE_MENU_BAR); + + jMenu.setCursor(cursor); + jMenu_Play_Here.setCursor(cursor); + + jMenuBar.add(jMenu); + jMenuBar.add(jMenu_Play_Here); + jMenu.add(jMenuItem_AboutMe); + jMenu.add(jMenuItem_Score); + this.setJMenuBar(jMenuBar); + } + + private void initLeft() { + jPanel_Left_Icon.setPreferredSize(new Dimension(100, 100)); + jPanel_Left_Icon.setBackground(UIColors.TEXT_COLOR_L); + + // Use the new image "250-250.png" + jPanel_Left_Icon.add(jLabel_Left_Icon, BorderLayout.CENTER); + + jPanel_Left.setPreferredSize(new Dimension(UISizes.WIDTH_MY_LEFT_FRAME, UISizes.HEIGHT_MY_LEFT_FRAME)); + jPanel_Left.setBackground(UIColors.TEXT_COLOR_L); + jPanel_Left.setBorder(UIBorders.TITLE); + + jPanel_Left.add(jPanel_Left_Icon, BorderLayout.CENTER); + } + + public abstract void initRight(); + + public abstract void initRightTop(); + + public abstract void initRightMiddle(); + + public abstract void initRightBottom(); + + public abstract void initRightPanel(); + + protected void initToggle() { + toggleButton.addEventSelected(selected -> { + Map lightMode = createLightModeMap(); + Map darkMode = createDarkModeMap(); + updateUI(selected ? darkMode : lightMode); + }); + jPanel_Right_Bottom_Option.add(toggleButton, FlowLayout.RIGHT); + } + + //foreground, background + private Map createLightModeMap() { + Map lightMode = new HashMap<>(); + lightMode.put(jLabel_Right_Middle_Email, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jLabel_Right_Middle_FirstName, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jLabel_Right_Middle_LastName, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jTextField_Right_Middle_Email, new Color[]{UIColors.SECONDARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jTextField_Right_Middle_FirstName, new Color[]{UIColors.SECONDARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jTextField_Right_Middle_LastName, new Color[]{UIColors.SECONDARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jLabel_Right_Middle_Password, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPasswordField_Right_Middle_Password, new Color[]{UIColors.SECONDARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Middle_Email, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Middle_FirstName, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Middle_LastName, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Middle_Password, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jButton_Right_Bottom_Submit, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Bottom_Button, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Middle_Data, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jButton_Right_Bottom_Others, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Bottom_Option, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jLabel_Right_Top_Title, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jLabel_Right_Bottom_Option, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right_Top_Title, new Color[]{UIColors.PRIMARY_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Left_Icon, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Left, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Container, new Color[]{UIColors.TEXT_COLOR_L, UIColors.PRIMARY_COLOR_L}); + lightMode.put(jPanel_Right, new Color[]{UIColors.PRIMARY_COLOR_L,UIColors.PRIMARY_COLOR_L}); + return lightMode; + } + + private Map createDarkModeMap() { + Map darkMode = new HashMap<>(); + darkMode.put(jLabel_Right_Middle_Email, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jLabel_Right_Middle_FirstName, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jLabel_Right_Middle_LastName, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jTextField_Right_Middle_Email, new Color[]{UIColors.SECONDARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jTextField_Right_Middle_FirstName, new Color[]{UIColors.SECONDARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jTextField_Right_Middle_LastName, new Color[]{UIColors.SECONDARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jLabel_Right_Middle_Password, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPasswordField_Right_Middle_Password, new Color[]{UIColors.SECONDARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Middle_Email, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Middle_FirstName, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Middle_LastName, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Middle_Password, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jButton_Right_Bottom_Submit, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Bottom_Button, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Middle_Data, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jButton_Right_Bottom_Others, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Bottom_Option, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jLabel_Right_Top_Title, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jLabel_Right_Bottom_Option, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right_Top_Title, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Left_Icon, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Left, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Container, new Color[]{UIColors.TEXT_COLOR_D, UIColors.PRIMARY_COLOR_D}); + darkMode.put(jPanel_Right, new Color[]{UIColors.PRIMARY_COLOR_D, UIColors.PRIMARY_COLOR_D}); + return darkMode; + } + + protected void updateUI(Map mode) { + if(mode != null){ + mode.forEach((component, colors) -> { + component.setForeground(colors[0]); + component.setBackground(colors[1]); + }); + }else{ + System.out.println("Error in change theme"); + } + } + + + + + protected void initContainer() { + jPanel_Container.setLayout(new BorderLayout()); + jPanel_Container.setBackground(UIColors.PRIMARY_COLOR_L); + jPanel_Container.add(jPanel_Left, BorderLayout.WEST); + jPanel_Container.add(jPanel_Right, BorderLayout.CENTER); + this.add(jPanel_Container); + } + + @Override + public void doAction() { + jMenuItem_Go.addActionListener(new ClickPlayNow()); + jMenuItem_AboutMe.addActionListener(new Info()); + jMenuItem_Score.addActionListener(new ScoreController(new ScoreView())); + } + + public class ClickOtherOption implements ActionListener { + @Override + public void actionPerformed(java.awt.event.ActionEvent e) { + try { + // Specify the URL of the website + URI uri = new URI("http://localhost:3000/users/register"); + // Open the website in the default browser + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(uri); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + + public class PressEnter implements ActionListener { + + @Override + public void actionPerformed(java.awt.event.ActionEvent e) { + jButton_Right_Bottom_Submit.doClick(); + } + + } + + protected class ClickPlayNow implements ActionListener { + + @Override + public void actionPerformed(java.awt.event.ActionEvent e) { + new MenuView().setVisible(true); + dispose(); + } + } + +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Apartment.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Apartment.java new file mode 100644 index 0000000..579ed20 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Apartment.java @@ -0,0 +1,173 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Graphics; +import java.awt.Image; +import java.io.InputStream; +import javax.swing.ImageIcon; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class Apartment extends Board { + protected int wallThickness = 20; + private Image wall; + + public Apartment(){ + super(); + } + + @Override + public void loadImages() { + super.loadImages(); + wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + //draw the horizontal walls at y = 0, from x = 0 to x 80 + for (int i = 0; i < 80; i += 20) { + g.drawImage(wall, i, 0, this); + } + //draw the horizontal walls at y = 120, from x = 160 to x 340 + for (int i = 160; i < UISizes.WIDTH_BOARD - 80; i += 20) { + g.drawImage(wall, i, 0, this); + } + //draw the vertical walls at x = 0, from y = 0 to y 120 + for (int i = 0; i < 100; i += 20) { + g.drawImage(wall, 0, i, this); + } + //draw the horizontal walls at y = 140, from x = 0 to x 200 + for (int i = 0; i < 200; i += 20) { + g.drawImage(wall, i, 140, this); + } + //draw the horizontal walls at y = 140, from x = 300 to max width + for (int i = 300; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 140, this); + } + //draw the vertical walls at x = 200, from y = 0 to y 160 + for (int i = 0; i < 160; i += 20) { + g.drawImage(wall, 200, i, this); + } + //draw the horizontal walls at y = 400, from x = 0 to max width + for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 260, this); + } + //draw the vertical walls at x = 300, from y = 260 to y 500 + for (int i = 260; i < UISizes.HEIGHT_BOARD - 50; i += 20) { + g.drawImage(wall, 300, i, this); + } + } + + @Override + protected void checkCollision() { + + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + // snake can go through the walls north + if (y[0] < 0) { + y[0] = UISizes.HEIGHT_BOARD - 50 - DOT_SIZE; + } + + // snake can go through the walls south + if(y[0] >= UISizes.HEIGHT_BOARD - 50){ + y[0] = 0; + } + + // snake can go through the walls east + if (x[0] >= UISizes.WIDTH_BOARD) { + x[0] = 0; + } + + // snake can go through the walls west + if (x[0] < 0) { + x[0] = UISizes.WIDTH_BOARD - DOT_SIZE; + } + + // snake hit the top left horizontal wall + if(x[0] < 80 && y[0] < 20){ + inGame = false; + } + + // snake hit the top left vertical wall + if(x[0] <= 20 && y[0] < 100){ + inGame = false; + } + + // snake hit the top middle horizontal wall (from x = 160 to 420, y = 0) + if(x[0] >= 160 && x[0] < 420 && y[0] < 20){ + inGame = false; + } + + // snake hit the first left horizontal middle chunk wall + if(x[0] < 11*20 && y[0] >= 7*20 && y[0] < 8*20){ + inGame = false; + } + + // snake hit the second right horizontal middle chunk wall + if(x[0] >= UISizes.WIDTH_BOARD - 200 && y[0] >= 7*20 && y[0] < 8*20){ + inGame = false; + } + + // snake hit the third horizontal middle wall + if(y[0] >= UISizes.HEIGHT_BOARD - 50 - 12*20 && y[0] <= UISizes.HEIGHT_BOARD - 50 - 11*20){ + inGame = false; + } + + // snake hit the second vertical wall + if(x[0] >= 10*20 && x[0] < 11*20 && y[0] >= 20 && y[0] <= 140){ + inGame = false; + } + + // snake hit the third vertical wall + if(x[0] >= 15*20 && x[0] <= 16*20 && y[0] >= UISizes.HEIGHT_BOARD - 50 - 12*20){ + inGame = false; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_x = ((r + wallThickness) * DOT_SIZE); + + bigApple_y = -100; + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_y = ((r + wallThickness) * DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_x = ((r + wallThickness) * DOT_SIZE); + apple_x = -100; + + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Box.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Box.java new file mode 100644 index 0000000..3dc0181 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Box.java @@ -0,0 +1,104 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Graphics; +import java.awt.Image; +import java.io.InputStream; +import javax.swing.ImageIcon; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class Box extends Board { + protected int wallThickness = 20; + private Image wall; + + public Box() { + super(); + } + + @Override + protected void loadImages() { + super.loadImages(); + wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 0, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + } + for (int i = 0; i < UISizes.HEIGHT_BOARD - 70; i += 20) { + g.drawImage(wall, 0, i, this); + g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); + } + } + + @Override + protected void checkCollision() { + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + if (y[0] >= UISizes.HEIGHT_BOARD - 70) { + inGame = false; + } + + if (y[0] < 20) { + inGame = false; + } + + if (x[0] >= UISizes.WIDTH_BOARD) { + inGame = false; + } + + if (x[0] < 20) { + inGame = false; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_x = ((r + wallThickness) * DOT_SIZE); + + bigApple_y = -100; + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_y = ((r + wallThickness) * DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_x = ((r + wallThickness) * DOT_SIZE); + apple_x = -100; + + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Campaign.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Campaign.java new file mode 100644 index 0000000..0b118ae --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Campaign.java @@ -0,0 +1,20 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.views.base.Board; + +public class Campaign extends Board { + @Override + protected void checkCollision() { + + } + + @Override + protected void locateApple() { + + } + + @Override + protected void locateBigApple() { + + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Mill.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Mill.java new file mode 100644 index 0000000..ae3b523 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Mill.java @@ -0,0 +1,143 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Graphics; +import java.awt.Image; +import java.io.InputStream; +import javax.swing.ImageIcon; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class Mill extends Board { + protected int wallThickness = 20; + private Image wall; + + public Mill() { + super(); + + //adjust to prevent hitting the side wall + leftDirection = false; + rightDirection = false; + upDirection = false; + downDirection = true; + } + + @Override + public void loadImages() { + super.loadImages(); + wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + //draw the left horizontal wall from x = 0 to x = 140 at y = 420 + for (int i = 0; i < 160; i += 20) { + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 80, this); + } + //draw the right horizontal wall from x = 340 to x = 500 at y = 60 + for (int i = 340; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 60, this); + } + //draw the vertical walls at x = 140, from y = 0 to y = 340 + for (int i = 0; i < 340; i += 20) { + g.drawImage(wall, 140, i, this); + } + //draw the vertical walls at x = 340, from y = 500 to y = 160 + for (int i = 160; i < 500; i += 20) { + g.drawImage(wall, 340, i, this); + } + + } + + @Override + protected void checkCollision() { + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + // snake can go through the walls north + if (y[0] < 0) { + y[0] = UISizes.HEIGHT_BOARD - 50 - DOT_SIZE; + } + + // snake can go through the walls south + if(y[0] >= UISizes.HEIGHT_BOARD - 50){ + y[0] = 0; + } + + // snake can go through the walls east + if (x[0] >= UISizes.WIDTH_BOARD) { + x[0] = 0; + } + + // snake can go through the walls west + if (x[0] < 0) { + x[0] = UISizes.WIDTH_BOARD - DOT_SIZE; + } + + // snake hit the vertical wall at x = 140, from y = 0 to y = 340 + if(x[0] == 140 && y[0] < 340 && y[0] > 0){ + inGame = false; + } + + // snake hit the vertical wall at x = 340, from y = 500 to y = 160 + if(x[0] == 340 && y[0] < 500 && y[0] > 160){ + inGame = false; + } + + // snake hit the horizontal wall from x = 0 to x = 140 at y = 420 + if(y[0] == UISizes.HEIGHT_BOARD - 50 - 80 && x[0] < 140 && x[0] > 0){ + inGame = false; + } + + // snake hit the horizontal wall from x = 340 to x = 500 at y = 60 + if(y[0] == 60 && x[0] < UISizes.WIDTH_BOARD && x[0] > 340){ + inGame = false; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_x = ((r + wallThickness) * DOT_SIZE); + + bigApple_y = -100; + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_y = ((r + wallThickness) * DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_x = ((r + wallThickness) * DOT_SIZE); + apple_x = -100; + + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/NoMaze.java b/src/main/java/com/lcaohoanq/formhandling/views/game/NoMaze.java new file mode 100644 index 0000000..8a691ea --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/NoMaze.java @@ -0,0 +1,87 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.io.InputStream; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class NoMaze extends Board { + + public NoMaze() { + super(); + } + + @Override + protected void loadImages() { + super.loadImages(); + } + + @Override + protected void checkCollision() { + + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + if (y[0] >= UISizes.HEIGHT_BOARD - 50) { + y[0] = 0; + } + + if (y[0] < 0) { + y[0] = UISizes.HEIGHT_BOARD - 50 - DOT_SIZE; + } + + if (x[0] >= UISizes.WIDTH_BOARD) { + x[0] = 0; + } + + if (x[0] < 0) { + x[0] = UISizes.WIDTH_BOARD - DOT_SIZE; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * RAND_POS); + apple_x = ((r * DOT_SIZE)); + + bigApple_y = -100; + r = (int) (Math.random() * RAND_POS); + apple_y = ((r * DOT_SIZE) + DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { +// audioHandler.setAudio(audioHandler.formatAudioPath(getClass().getResource(Paths.URL_BIG_APPLE_APP).getPath())); + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * RAND_POS); + bigApple_x = ((r * DOT_SIZE)); + apple_x = -100; + + r = (int) (Math.random() * RAND_POS); + bigApple_y = ((r * DOT_SIZE)); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Rails.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Rails.java new file mode 100644 index 0000000..8ed5704 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Rails.java @@ -0,0 +1,151 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Graphics; +import java.awt.Image; +import java.io.InputStream; +import javax.swing.ImageIcon; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class Rails extends Board { + + private final int wallThickness = 20; + private Image wall; + + public Rails() { + super(); + } + + @Override + protected void loadImages() { + super.loadImages(); + wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + //draw the top and bottom horizontal walls + for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 0, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + } + //draw the 2 left vertical walls from y = 0 to y = 200 and from y = 300 to y = 480 + for (int i = 0; i < 200; i += 20) { + g.drawImage(wall, 0, i, this); + g.drawImage(wall, 0, UISizes.HEIGHT_BOARD - 70 - i, this); + } + //draw the 2 right vertical walls from + for (int i = 0; i < 200; i += 20) { + g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(wall, UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); + } + //draw the 2 middle horizontal walls + for (int i = 100; i < 400; i += 20) { + g.drawImage(wall, i, 140, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 160, this); + } + } + + @Override + protected void checkCollision() { + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + // snake hit the walls north + if (y[0] <= 20) { + inGame = false; + } + + // snake hit the walls south + if(y[0] >= UISizes.HEIGHT_BOARD - 70){ + inGame = false; + } + + // snake hit the top left vertical wall + if (x[0] <= 20 && y[0] <= 200) { + inGame = false; + } + + // snake hit the bottom left vertical wall + if (x[0] <= 20 && y[0] >= UISizes.HEIGHT_BOARD - 50 - 200) { + inGame = false; + } + + // snake hit the top right vertical wall + if (x[0] >= UISizes.WIDTH_BOARD - 20 && y[0] <= 200) { + inGame = false; + } + + // snake hit the bottom right vertical wall + if (x[0] >= UISizes.WIDTH_BOARD - 20 && y[0] >= UISizes.HEIGHT_BOARD - 50 - 200) { + inGame = false; + } + + // snake hit the first horizontal wall length 400 at x = 100, y = 140, each block is 20x20 + if (y[0] >= 140 && y[0] <= 160 && x[0] >= 100 && x[0] <= 390) { + inGame = false; + } + + // snake hit the second horizontal wall length 400 at x = 100, y = 340, each block is 20x20 + if (y[0] >= UISizes.HEIGHT_BOARD - 50 - 160 && y[0] <= UISizes.HEIGHT_BOARD - 50 - 140 && x[0] >= 100 && x[0] <= 390) { + inGame = false; + } + + // snake can go through the walls east + if (x[0] >= UISizes.WIDTH_BOARD && y[0] >= 200 && y[0] <= UISizes.HEIGHT_BOARD - 50 - 200) { + x[0] = 0; + } + + // snake can go through the walls west + if (x[0] < 0 && y[0] >= 200 && y[0] <= UISizes.HEIGHT_BOARD - 50 - 200) { + x[0] = UISizes.WIDTH_BOARD - DOT_SIZE; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_x = ((r + wallThickness) * DOT_SIZE); + + bigApple_y = -100; + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_y = ((r + wallThickness) * DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_x = ((r + wallThickness) * DOT_SIZE); + apple_x = -100; + + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Snake.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Snake.java new file mode 100644 index 0000000..570d8fb --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Snake.java @@ -0,0 +1,79 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.controllers.MenuController; +import com.lcaohoanq.formhandling.controllers.PlayController; +import java.awt.FlowLayout; +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import com.lcaohoanq.formhandling.styles.UIBorders; +import com.lcaohoanq.formhandling.styles.UIImages; + +public class Snake extends JFrame { + private final JMenuBar jMenuBar = new JMenuBar(); + private final JMenu jMenu = new JMenu("HELP"); + private final JMenuItem jMenuItem_Back_To_Main_Menu = new JMenuItem("Back to main menu"); + + public Snake(String mode) { + initMenu(); + initUI(mode); + } + + private void initUI(String mode) { + checkMode(mode); + setResizable(false); + pack(); + setTitle("Snake"); + setIconImage(UIImages.icon); + setJMenuBar(jMenuBar); + MenuController.menuView.dispose(); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(false); + } + + private void initMenu() { + jMenuBar.setLayout(new FlowLayout(FlowLayout.LEFT)); + jMenuBar.setBorder(UIBorders.LINE_MENU_BAR); + jMenuBar.add(jMenu); + jMenu.add(jMenuItem_Back_To_Main_Menu); + jMenuItem_Back_To_Main_Menu.addActionListener(new PlayController(this)); + this.setJMenuBar(jMenuBar); + } + + private void checkMode(String mode) { + if (mode.equals("Classic")) { + add(new NoMaze()); // NoMaze is the default mode + } + if (mode.equals("NoMaze")) { + add(new NoMaze()); // NoMaze is the default mode + } + if (mode.equals("Box")) { + add(new Box()); + } + if (mode.equals("Tunnel")) { + add(new Tunnel()); + } + if (mode.equals("Mill")) { + add(new Mill()); + } + if (mode.equals("Rails")) { + add(new Rails()); + } + if (mode.equals("Apartment")) { + add(new Apartment()); + } + if (mode.equals("Campaign")) { + add(new Campaign()); + } + } + + public void startGame() { + setVisible(true); + } + + public void stopGame() { + dispose(); + } +} diff --git a/src/main/java/com/lcaohoanq/formhandling/views/game/Tunnel.java b/src/main/java/com/lcaohoanq/formhandling/views/game/Tunnel.java new file mode 100644 index 0000000..61a6db7 --- /dev/null +++ b/src/main/java/com/lcaohoanq/formhandling/views/game/Tunnel.java @@ -0,0 +1,185 @@ +package com.lcaohoanq.formhandling.views.game; + +import com.lcaohoanq.formhandling.constants.ResourcePaths; +import java.awt.Graphics; +import java.awt.Image; +import java.io.InputStream; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import com.lcaohoanq.formhandling.styles.UISizes; +import com.lcaohoanq.formhandling.views.base.Board; + +public class Tunnel extends Board { + protected int wallThickness = 20; + private Image wall; + + public Tunnel() { + super(); + } + + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.add(new Tunnel()); + frame.pack(); + frame.setSize(UISizes.WIDTH_BOARD, UISizes.HEIGHT_BOARD); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } + + @Override + public void loadImages() { + super.loadImages(); + wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + //draw the 2 left horizontal walls + for (int i = 0; i < 100; i += 20) { + g.drawImage(wall, i, 0, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + } + //draw the 2 right horizontal walls + for (int i = 400; i < UISizes.WIDTH_BOARD; i += 20) { + g.drawImage(wall, i, 0, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + } + //draw the 2 left vertical walls + for (int i = 0; i < 100; i += 20) { + g.drawImage(wall, 0, i, this); + g.drawImage(wall, 0, UISizes.HEIGHT_BOARD - 70 - i, this); + } + //draw the 2 right vertical walls + for (int i = 0; i < 100; i += 20) { + g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(wall, UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); + } + //draw the 2 middle horizontal walls + for (int i = 100; i < 400; i += 20) { + g.drawImage(wall, i, 205, this); + g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 100 - 105, this); + } + } + + @Override + protected void checkCollision() { + for (int z = dots; z > 0; z--) { + + if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { + inGame = false; + break; + } + } + + //check if the snake hit the first middle horizontal walls (done) + if (y[0] >= 205 && y[0] <= 225 && x[0] >= 100 && x[0] <= 390) { + inGame = false; + } + + //check if the snake hit the second middle horizontal walls (done) + if(y[0] >= UISizes.HEIGHT_BOARD - 50 - 100 - 105 && y[0] <= UISizes.HEIGHT_BOARD - 50 - 100 - 85 && x[0] >= 100 && x[0] <= 390){ + inGame = false; + } + + //check if the snake hit the top left vertical walls (done) + if (x[0] >= 0 && x[0] <= 20 && y[0] >= 0 && y[0] <= 90) { + inGame = false; + } + + //check if the snake hit the bottom left vertical walls + if (x[0] >= 0 && x[0] <= 20 && y[0] >= UISizes.HEIGHT_BOARD - 50 - 100 && y[0] <= UISizes.HEIGHT_BOARD - 50) { + inGame = false; + } + + //check if the snake hit the top right vertical walls (done) + if (x[0] >= UISizes.WIDTH_BOARD - 20 && x[0] <= UISizes.WIDTH_BOARD && y[0] >= 0 && y[0] <= 90) { + inGame = false; + } + + //check if the snake hit the bottom right vertical walls + if (x[0] >= UISizes.WIDTH_BOARD - 20 && x[0] <= UISizes.WIDTH_BOARD && y[0] >= UISizes.HEIGHT_BOARD - 50 - 100 && y[0] <= UISizes.HEIGHT_BOARD - 70) { + inGame = false; + } + + //check if the snake hit the top left horizontal walls (done) + if (y[0] >= 0 && y[0] <= 20 && x[0] >= 0 && x[0] <= 90) { + inGame = false; + } + + //check if the snake hit the bottom left horizontal walls + if (y[0] >= UISizes.HEIGHT_BOARD - 70 && y[0] <= UISizes.HEIGHT_BOARD - 70 && x[0] >= 0 && x[0] <= 90) { + inGame = false; + } + + //check if the snake hit the top right horizontal walls + if (y[0] >= 0 && y[0] <= 20 && x[0] >= UISizes.WIDTH_BOARD - 100 && x[0] <= UISizes.WIDTH_BOARD) { + inGame = false; + } + + //check if the snake hit the bottom right horizontal walls + if (y[0] >= UISizes.HEIGHT_BOARD - 70 && y[0] <= UISizes.HEIGHT_BOARD - 50 && x[0] >= UISizes.WIDTH_BOARD - 100 && x[0] <= UISizes.WIDTH_BOARD) { + inGame = false; + } + + // snake go through the wall south + if (y[0] >= UISizes.HEIGHT_BOARD - 50) { + y[0] = 0; + } + + // snake go through the wall north + if (y[0] < 0 && x[0] >= 100 && x[0] <= UISizes.WIDTH_BOARD - 100) { + y[0] = UISizes.HEIGHT_BOARD - 50 - DOT_SIZE; + } + + // snake go through the wall east + if (x[0] >= UISizes.WIDTH_BOARD && y[0] >= 100 && y[0] <= UISizes.HEIGHT_BOARD - 100) { + x[0] = 0; + } + + // snake go through the wall west + if (x[0] < 0) { + x[0] = UISizes.WIDTH_BOARD - DOT_SIZE; + } + + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioHandler.playAudio(inputStream); + } + timer.stop(); + } + } + + @Override + protected void locateApple() { + if (apple_count % 5 == 0 && apple_count != 0) { + locateBigApple(); + } else { + bigApple_x = -100; + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_x = ((r + wallThickness) * DOT_SIZE); + + bigApple_y = -100; + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + apple_y = ((r + wallThickness) * DOT_SIZE); + } + } + + @Override + protected void locateBigApple() { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_BIG_APPLE_APP); + audioHandler.playAudio(inputStream); + } + int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_x = ((r + wallThickness) * DOT_SIZE); + apple_x = -100; + + r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); + bigApple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = -100; + setBigAppleTime(); + renderProgressBar(); + } +} diff --git a/src/main/resources/com/lcaohoanq/formhandling/register.fxml b/src/main/resources/com/lcaohoanq/formhandling/register.fxml deleted file mode 100644 index 499d6fc..0000000 --- a/src/main/resources/com/lcaohoanq/formhandling/register.fxml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -