diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java index c9b1879b..fc4e85a1 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java @@ -383,6 +383,20 @@ public final class IconLoader { return IconUtil.createFrameIcon(icon, window); } + /** + * Create an derived version of the icon with the given width and height. This method will return + * the best possible result if the given icon implements {@link DerivableIcon} or + * {@link ImageSource}. + * + * @param icon the icon to drive. + * @param w the new width. + * @param h the new height. + * @return the derived icon. + */ + public static Icon createDerivedIcon(final Icon icon, final int w, final int h) { + return IconUtil.createDerivedIcon(icon, w, h); + } + protected URL getResource(final String name) { if (parentClass != null) { return parentClass.getResource(name); diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java index 7d02cc2c..72391a75 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java @@ -102,4 +102,16 @@ public class IconUtil { return image; } } + + public static Icon createDerivedIcon(final Icon icon, final int w, final int h) { + int iconWidth = icon.getIconWidth(); + int iconHeight = icon.getIconHeight(); + if (iconHeight == h && iconWidth == w) return icon; + if (icon instanceof DerivableIcon) { + return ((DerivableIcon) icon).derive(w, h); + } else { + return new DerivableImageIcon( + IconUtil.createScaledImage(icon, iconWidth / (double) w, iconHeight / (double) h)); + } + } }