Skip to content

Commit

Permalink
MultiResolutionImageSupport optimizations:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
DevCharly committed Sep 4, 2023
1 parent 03f9115 commit 9efb976
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9efb976

Please sign in to comment.