Class ScalableImageIcon

java.lang.Object
javax.swing.ImageIcon
swingtree.style.ScalableImageIcon
All Implemented Interfaces:
Serializable, Accessible, Icon

public final class ScalableImageIcon extends ImageIcon
A wrapper for ImageIcon that automatically scales the image to the current UI.scale() value defined in the current SwingTree library context singleton.
See Also:
  • Method Details

    • of

      public static ScalableImageIcon of(Size size, ImageIcon icon)
      A factory method that creates a new ScalableImageIcon that will render the supplied ImageIcon using the given base size scaled according to the current DPI settings.

      If the given ImageIcon is already a ScalableImageIcon, a new instance will be created from the existing one using withSize(Size).

      Parameters:
      size - The size to render the icon at.
      icon - The icon to render.
      Returns:
      A new ScalableImageIcon that will render the image scaled according to the current DPI settings.
    • withSize

      public ScalableImageIcon withSize(Size size)
      Returns a new ScalableImageIcon that will render the image at the given size.

      Note that the returned icon will be a new instance and will not affect the current icon.

      Parameters:
      size - The size to render the icon at.
      Returns:
      A new ScalableImageIcon that will render the image at the given size.
    • getIconWidth

      public int getIconWidth()
      Exposes the width of the icon, or -1 if the icon does not have a fixed width.
      Note that the returned width is dynamically scaled according to the current UI.scale() value. This is to ensure that the icon is rendered at the correct size according to the current DPI settings. If you want the unscaled width, use getBaseWidth().
      Specified by:
      getIconWidth in interface Icon
      Overrides:
      getIconWidth in class ImageIcon
      Returns:
      The width of the icon, or -1 if the icon does not have a width.
    • getIconHeight

      public int getIconHeight()
      Exposes the height of the icon, or -1 if the icon does not have a fixed height.
      Note that the returned height is dynamically scaled according to the current UI.scale() value. This is to ensure that the icon is rendered at the correct size according to the current DPI settings. If you want the unscaled height, use getBaseHeight().
      Specified by:
      getIconHeight in interface Icon
      Overrides:
      getIconHeight in class ImageIcon
      Returns:
      The height of the icon, or -1 if the icon does not have a height.
    • getBaseWidth

      public int getBaseWidth()
      Returns the unscaled width of the icon. This is the width of the icon as it was originally loaded and is not affected by the current UI.scale() value.

      If you want a width that is more suited for rendering according to the current DPI settings, use getIconWidth().

      Returns:
      The unscaled width of the icon.
    • getBaseHeight

      public int getBaseHeight()
      Returns the unscaled height of the icon. This is the height of the icon as it was originally loaded and is not affected by the current UI.scale() value.

      If you want a height that is more suited for rendering according to the current DPI settings, use getIconHeight().

      Returns:
      The unscaled height of the icon.
    • paintIcon

      public void paintIcon(Component c, Graphics g, int x, int y)
      Specified by:
      paintIcon in interface Icon
      Overrides:
      paintIcon in class ImageIcon
    • getImage

      public Image getImage()
      Overrides:
      getImage in class ImageIcon
    • setImage

      public void setImage(Image image)
      Overrides:
      setImage in class ImageIcon
    • toString

      public String toString()
      Overrides:
      toString in class ImageIcon
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object