From 9efb9761c68d8265a026e39e6bbe728123e36d5f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 4 Sep 2023 13:08:21 +0200 Subject: [PATCH] MultiResolutionImageSupport optimizations: - ProducerMultiResolutionImage: avoid creation of base image to get image width and height, because if screen is scaled then the base image would be never painted, but consumes memory and takes time to create it - MappedMultiResolutionImage: delegate getting width/height/source/property to original image, to defer/avoid creation of mapped image --- .../util/MultiResolutionImageSupport.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/flatlaf-core/src/main/java9/com/formdev/flatlaf/util/MultiResolutionImageSupport.java b/flatlaf-core/src/main/java9/com/formdev/flatlaf/util/MultiResolutionImageSupport.java index 74e5c776f..3bc29abd1 100644 --- a/flatlaf-core/src/main/java9/com/formdev/flatlaf/util/MultiResolutionImageSupport.java +++ b/flatlaf-core/src/main/java9/com/formdev/flatlaf/util/MultiResolutionImageSupport.java @@ -20,6 +20,8 @@ import java.awt.Image; import java.awt.image.AbstractMultiResolutionImage; import java.awt.image.BaseMultiResolutionImage; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; import java.awt.image.MultiResolutionImage; import java.util.ArrayList; import java.util.Collections; @@ -117,6 +119,26 @@ protected Image getBaseImage() { return mapAndCacheImage( mrImage ); } + @Override + public int getWidth( ImageObserver observer ) { + return mrImage.getWidth( observer ); + } + + @Override + public int getHeight( ImageObserver observer ) { + return mrImage.getHeight( observer ); + } + + @Override + public ImageProducer getSource() { + return mrImage.getSource(); + } + + @Override + public Object getProperty( String name, ImageObserver observer ) { + return mrImage.getProperty( name, observer ); + } + private Image mapAndCacheImage( Image image ) { return cache.computeIfAbsent( image, img -> { // using ImageIcon here makes sure that the image is loaded @@ -160,6 +182,16 @@ protected Image getBaseImage() { return produceAndCacheImage( dimensions[0] ); } + @Override + public int getWidth( ImageObserver observer ) { + return dimensions[0].width; + } + + @Override + public int getHeight( ImageObserver observer ) { + return dimensions[0].height; + } + private Image produceAndCacheImage( Dimension size ) { return cache.computeIfAbsent( size, size2 -> { // using ImageIcon here makes sure that the image is loaded