From 6ffa144dafc57bd7152d7f4caa1d29e863da8435 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 6 Jun 2020 10:50:23 +0200 Subject: [PATCH] Load JavaFX Images in IconUtil, display Envoy logo --- src/main/java/envoy/client/ui/IconUtil.java | 24 ++++++++++++------- .../java/envoy/client/ui/LoginDialog.java | 3 +++ src/main/java/envoy/client/ui/Startup.java | 3 +-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/envoy/client/ui/IconUtil.java b/src/main/java/envoy/client/ui/IconUtil.java index 0092483..e2577ec 100644 --- a/src/main/java/envoy/client/ui/IconUtil.java +++ b/src/main/java/envoy/client/ui/IconUtil.java @@ -1,12 +1,10 @@ package envoy.client.ui; -import java.awt.Image; import java.io.IOException; import java.util.EnumMap; import java.util.EnumSet; -import javax.imageio.ImageIO; -import javax.swing.ImageIcon; +import javafx.scene.image.Image; /** * Provides static utility methods for loading icons from the resource @@ -24,7 +22,17 @@ public class IconUtil { private IconUtil() {} /** - * Loads an icon from resource folder and scales it to a given size. + * Loads an icon from the resource folder. + * + * @param path the path to the icon inside the resource folder + * @return the icon + * @throws IOException if the loading process failed + * @since Envoy Client v0.1-beta + */ + public static Image load(String path) throws IOException { return new Image(IconUtil.class.getResource(path).toExternalForm()); } + + /** + * Loads an icon from the resource folder and scales it to a given size. * * @param path the path to the icon inside the resource folder * @param size the size to scale the icon to @@ -32,8 +40,8 @@ public class IconUtil { * @throws IOException if the loading process failed * @since Envoy Client v0.1-beta */ - public static ImageIcon load(String path, int size) throws IOException { - return new ImageIcon(ImageIO.read(IconUtil.class.getResourceAsStream(path)).getScaledInstance(size, size, Image.SCALE_SMOOTH)); + public static Image load(String path, int size) throws IOException { + return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true); } /** @@ -51,8 +59,8 @@ public class IconUtil { * @throws IOException if the loading process failed * @since Envoy Client v0.1-beta */ - public static > EnumMap loadByEnum(Class enumClass, int size) throws IOException { - var icons = new EnumMap(enumClass); + public static > EnumMap loadByEnum(Class enumClass, int size) throws IOException { + var icons = new EnumMap(enumClass); var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/"; for (var e : EnumSet.allOf(enumClass)) icons.put(e, load(path + e.toString().toLowerCase() + ".png", size)); diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index de8d618..cba70c9 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -10,6 +10,7 @@ import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; +import javafx.stage.Stage; import envoy.client.data.Cache; import envoy.client.data.ClientConfig; @@ -75,6 +76,8 @@ public final class LoginDialog extends Dialog { loader.setController(this); final var dialogPane = loader.load(); + ((Stage) getDialogPane().getScene().getWindow()).getIcons().add(IconUtil.load("/icons/envoy_logo.png")); + // Configure dialog buttons dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 8e367a6..b6de986 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -13,7 +13,6 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; -import javafx.scene.image.Image; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; @@ -137,7 +136,7 @@ public final class Startup extends Application { changeScene("/fxml/ChatScene.fxml", new GridPane(), false); Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); }); stage.setTitle("Envoy"); - stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png"))); + stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png")); stage.show(); // TODO: Add capability to change custom CSS. In case of switching to a default // theme, no further action is required