From 74504e068392cc5771bc847693f2577eeb0a0511 Mon Sep 17 00:00:00 2001 From: Peter <pt033@hdm-stuttgart.de> Date: Sun, 21 Jan 2024 19:16:13 +0100 Subject: [PATCH] refactor(Main): seperating main and start method as application won't start if exported to jar --- .../battlearena/Main/Driver.java | 75 ++++++++++++++++++ .../hdm_stuttgart/battlearena/Main/Main.java | 78 +------------------ src/main/resources/player/playerAccount.json | 2 +- 3 files changed, 79 insertions(+), 76 deletions(-) create mode 100644 src/main/java/de/hdm_stuttgart/battlearena/Main/Driver.java diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Main/Driver.java b/src/main/java/de/hdm_stuttgart/battlearena/Main/Driver.java new file mode 100644 index 00000000..66f93ada --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Main/Driver.java @@ -0,0 +1,75 @@ +package de.hdm_stuttgart.battlearena.Main; + + +import de.hdm_stuttgart.battlearena.Model.Sound.SoundManager; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Rectangle2D; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.scene.text.Font; +import javafx.stage.Screen; +import javafx.stage.Stage; + +import javafx.stage.StageStyle; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Objects; + +public class Driver extends Application { + + private static final Logger log = LogManager.getLogger(Driver.class); + Rectangle2D screen = Screen.getPrimary().getVisualBounds(); + + InputHandler inputHandler = InputHandler.getInstance(); + + SoundManager soundManager = new SoundManager(); + + @Override + public void start(Stage stage) throws Exception { + +// loading font in start() because CSS can't handle whitespace in folder names + Font.loadFont(getClass().getResourceAsStream("/fonts/StarshipShadow.ttf"), 50); + Font.loadFont(getClass().getResourceAsStream("/fonts/AmaticSC-Bold.ttf"), 50); + Font.loadFont(getClass().getResourceAsStream("/fonts/Arthemis-mLA22.ttf"), 50); + Font.loadFont(getClass().getResourceAsStream("/fonts/ShadowsIntoLight-Regular.ttf"), 50); + + Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/LoadingScreen.fxml"))); + + Scene scene = new Scene(root); + + scene.setOnKeyPressed(inputHandler::handleKeyPress); + scene.setOnKeyReleased(inputHandler::handleKeyRelease); + + stage.setTitle("BattleArena"); + stage.getIcons().add(new Image("file:src/main/resources/textures/images/icon.png")); + stage.setScene(scene); + stage.minHeightProperty().setValue(400); + stage.minWidthProperty().setValue(600); + stage.setMaximized(true); + stage.setWidth(screen.getWidth()); + stage.setHeight(screen.getHeight()); + stage.initStyle(StageStyle.UNDECORATED); + scene.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/styles/style.css")).toExternalForm()); + stage.setScene(scene); + + stage.setOnCloseRequest(event -> { + if (soundManager != null) { + soundManager.stopMusic(); + } + }); + + stage.show(); + log.debug("Project started successfully!"); + +// Exiting program if an exception occurs +// Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { +// log.error(throwable.getMessage()); +// System.exit(1); +// }); + } +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java index 5bd163f7..77261800 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java @@ -1,81 +1,9 @@ package de.hdm_stuttgart.battlearena.Main; - -import de.hdm_stuttgart.battlearena.Model.Sound.MusicType; -import de.hdm_stuttgart.battlearena.Model.Sound.SFX; -import de.hdm_stuttgart.battlearena.Model.Sound.SoundManager; -import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; - import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.geometry.Rectangle2D; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.image.Image; -import javafx.scene.text.Font; -import javafx.stage.Screen; -import javafx.stage.Stage; - -import javafx.stage.StageStyle; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.Objects; - -public class Main extends Application { - - private static final Logger log = LogManager.getLogger(Main.class); - Rectangle2D screen = Screen.getPrimary().getVisualBounds(); - - InputHandler inputHandler = InputHandler.getInstance(); - - SoundManager soundManager = new SoundManager(); +public class Main { public static void main(String[] args) { - launch(args); - } - - @Override - public void start(Stage stage) throws Exception { - -// loading font in start() because CSS can't handle whitespace in folder names - Font.loadFont(getClass().getResourceAsStream("/fonts/StarshipShadow.ttf"), 50); - Font.loadFont(getClass().getResourceAsStream("/fonts/AmaticSC-Bold.ttf"), 50); - Font.loadFont(getClass().getResourceAsStream("/fonts/Arthemis-mLA22.ttf"), 50); - Font.loadFont(getClass().getResourceAsStream("/fonts/ShadowsIntoLight-Regular.ttf"), 50); - - Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/LoadingScreen.fxml"))); - - Scene scene = new Scene(root); - - scene.setOnKeyPressed(inputHandler::handleKeyPress); - scene.setOnKeyReleased(inputHandler::handleKeyRelease); - - stage.setTitle("BattleArena"); - stage.getIcons().add(new Image("file:src/main/resources/textures/images/icon.png")); - stage.setScene(scene); - stage.minHeightProperty().setValue(400); - stage.minWidthProperty().setValue(600); - stage.setMaximized(true); - stage.setWidth(screen.getWidth()); - stage.setHeight(screen.getHeight()); - stage.initStyle(StageStyle.UNDECORATED); - scene.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/styles/style.css")).toExternalForm()); - stage.setScene(scene); - - stage.setOnCloseRequest(event -> { - if (soundManager != null) { - soundManager.stopMusic(); - } - }); - - stage.show(); - log.debug("Project started successfully!"); - -// Exiting program if an exception occurs -// Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { -// log.error(throwable.getMessage()); -// System.exit(1); -// }); + Application.launch(Driver.class, args); } -} \ No newline at end of file +} diff --git a/src/main/resources/player/playerAccount.json b/src/main/resources/player/playerAccount.json index ad25fb8a..60070b6e 100644 --- a/src/main/resources/player/playerAccount.json +++ b/src/main/resources/player/playerAccount.json @@ -1,5 +1,5 @@ { "playerName": "Player1", "accountPassword": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8", - "accountType": "ONLINE" + "accountType": "LOCAL" } \ No newline at end of file -- GitLab