From 354c531f5c0798fc361c05422f7b524cc4366495 Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 2 Aug 2025 15:12:18 +0800 Subject: [PATCH] update --- .../java/org/jackhuang/hmcl/ui/FXUtils.java | 12 ++++------ .../org/jackhuang/hmcl/util/SwingFXUtils.java | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java index 2949fd66b..a2ee26b3b 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -45,10 +45,7 @@ import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.*; -import javafx.scene.layout.ColumnConstraints; -import javafx.scene.layout.Priority; -import javafx.scene.layout.Region; -import javafx.scene.layout.StackPane; +import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; @@ -82,6 +79,7 @@ import javax.imageio.stream.ImageInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import java.awt.image.BufferedImage; import java.io.*; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; @@ -916,14 +914,14 @@ public final class FXUtils { WebPImageReaderSpi spi = new WebPImageReaderSpi(); ImageReader reader = spi.createReaderInstance(null); - + BufferedImage bufferedImage; try (ImageInputStream imageInput = ImageIO.createImageInputStream(Channels.newInputStream(channel))) { reader.setInput(imageInput, true, true); - return SwingFXUtils.toFXImage(reader.read(0, reader.getDefaultReadParam()), - requestedWidth, requestedHeight, preserveRatio, smooth); + bufferedImage = reader.read(0, reader.getDefaultReadParam()); } finally { reader.dispose(); } + return SwingFXUtils.toFXImage(bufferedImage, requestedWidth, requestedHeight, preserveRatio, smooth); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/SwingFXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/SwingFXUtils.java index 4f9d28c15..80c068d44 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/SwingFXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/SwingFXUtils.java @@ -122,11 +122,13 @@ public final class SwingFXUtils { } public static WritableImage toFXImage(BufferedImage bimg, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth) { + if (requestedWidth <= 0. || requestedHeight <= 0.) { + return toFXImage(bimg, null); + } + int width = (int) requestedWidth; int height = (int) requestedHeight; - assert width > 0 && height > 0; - // Calculate actual dimensions if preserveRatio is true if (preserveRatio) { double originalWidth = bimg.getWidth(); @@ -142,17 +144,18 @@ public final class SwingFXUtils { // Create scaled BufferedImage BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = scaledImage.createGraphics(); + try { + if (smooth) { + g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + } - if (smooth) { - g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); - g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.drawImage(bimg, 0, 0, width, height, null); + } finally { + g2d.dispose(); } - g2d.drawImage(bimg, 0, 0, width, height, null); - g2d.dispose(); - - // Convert to JavaFX Image using the existing method return toFXImage(scaledImage, null); } }