Browse Source

Introduce utility method to rescale an icon.

pull/214/head
weisj 4 years ago
parent
commit
52fc48d52c
  1. 14
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java
  2. 12
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java

14
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); 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) { protected URL getResource(final String name) {
if (parentClass != null) { if (parentClass != null) {
return parentClass.getResource(name); return parentClass.getResource(name);

12
property-loader/src/main/java/com/github/weisj/darklaf/icons/IconUtil.java

@ -102,4 +102,16 @@ public class IconUtil {
return image; 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));
}
}
} }

Loading…
Cancel
Save