Load JavaFX Images in IconUtil, display Envoy logo

This commit is contained in:
Kai S. K. Engelbart 2020-06-06 10:50:23 +02:00
parent 0dece66542
commit 6ffa144daf
3 changed files with 20 additions and 10 deletions

View File

@ -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 <T extends Enum<T>> EnumMap<T, ImageIcon> loadByEnum(Class<T> enumClass, int size) throws IOException {
var icons = new EnumMap<T, ImageIcon>(enumClass);
public static <T extends Enum<T>> EnumMap<T, Image> loadByEnum(Class<T> enumClass, int size) throws IOException {
var icons = new EnumMap<T, Image>(enumClass);
var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
for (var e : EnumSet.allOf(enumClass))
icons.put(e, load(path + e.toString().toLowerCase() + ".png", size));

View File

@ -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<Void> {
loader.setController(this);
final var dialogPane = loader.<DialogPane>load();
((Stage) getDialogPane().getScene().getWindow()).getIcons().add(IconUtil.load("/icons/envoy_logo.png"));
// Configure dialog buttons
dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK);

View File

@ -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