Class ScalableImageIcon

java.lang.Object
javax.swing.ImageIcon
com.nomagic.ui.ScalableImageIcon
All Implemented Interfaces:
ResizableIcon, Serializable, Cloneable, Accessible, Icon
Direct Known Subclasses:
DoubleSizeImageIcon, ResizableIconImageIcon

@OpenApiAll public class ScalableImageIcon extends ImageIcon implements ResizableIcon
HiDPI(Retina) friendly ImageIcon implementation.

This class dynamically chooses a right image by current screen dpi and/or scaling factor defined in the system (Retina and etc). If scaling is needed and svg icon with the same name is available at given location, svg icon will be used instead of a bitmap icon.

Dynamic choosing will work only if two icons are provided at the same location. For example:

  • /com/product/icons/a.png
  • /com/product/icons/a.svg
  • ScalableImageIcon(Product.class, "/com/product/icons/a.png") will load a.png on a regular dpi monitor, but a.svg will be loaded on HiDPI monitor. Keep in mind that svg icon size can be any. Svg icon will be re-sized according to the size of a.png icon and scaling factor. For example if size of a.png is 16x16 and scaling factor is 2, loaded svg icon will be re-sized to 32x32 if needed.

    Original icon will be loaded and scaled if svg icon is not provided, but HiDPI with scaling is used.

    Icon image loading is lazy. Image will be loaded only if icon is painted, getImage() is called or icon width/height is retrieved.

    See Also:
    • Constructor Details

      • ScalableImageIcon

        public ScalableImageIcon(Image image)
        Wrap given image and use it for painting.
        Parameters:
        image - image
      • ScalableImageIcon

        public ScalableImageIcon(byte[] imageData)
        Load icon from given bytes.
        Parameters:
        imageData - image bytes
      • ScalableImageIcon

        public ScalableImageIcon(byte[] imageData, URI uri)
        Load icon from given bytes.
        Parameters:
        imageData - image bytes
        uri - uri describing the bytes (format and etc)
      • ScalableImageIcon

        public ScalableImageIcon(URL url)
        Load icon from a given url.
        Parameters:
        url - url
      • ScalableImageIcon

        public ScalableImageIcon(Class clazz, String location)
        Load icon from a resources of given class.
        Parameters:
        clazz - class
        location - resources location relative to a given class
      • ScalableImageIcon

        public ScalableImageIcon(Class clazz, String location, boolean silent)
        Load icon from a resources of given class.
        Parameters:
        clazz - class
        location - resources location relative to a given class
        silent - if true, does not report to log file about missing icon at given location
    • Method Details

      • create

        @CheckForNull public static ScalableImageIcon create(String fileName)
        Load icon from a file.
        Parameters:
        fileName - file name
      • paintIcon

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

        public void paintIcon(@CheckForNull Component c, Graphics g, int x, int y, int w, int h)
        Description copied from interface: ResizableIcon
        Draw the icon in the given bounds. Icon implementations may use the Component argument to get properties useful for painting, e.g. the foreground or background color.
        Specified by:
        paintIcon in interface ResizableIcon
        Parameters:
        c - the given component.
        g - the graphics to paint on.
        x - x coordinate.
        y - y coordinate.
        w - width to paint.
        h - height to paint.
      • getIconHeight

        public int getIconHeight()
        Specified by:
        getIconHeight in interface Icon
        Overrides:
        getIconHeight in class ImageIcon
      • getIconWidth

        public int getIconWidth()
        Specified by:
        getIconWidth in interface Icon
        Overrides:
        getIconWidth in class ImageIcon
      • setImage

        public void setImage(@CheckForNull Image image)
        Overrides:
        setImage in class ImageIcon
      • getImage

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

        @CheckForNull public String getLocation()
        Returns:
        location of resource if icon is loaded from resources of some class. Null otherwise
      • getURL

        public URL getURL()
        Returns:
        url if icon is loaded from url. Null otherwise
      • clone

        public Object clone()
        Description copied from interface: ResizableIcon
        Returns cloned instance of this icon.
        Specified by:
        clone in interface ResizableIcon
        Overrides:
        clone in class Object
        Returns:
        cloned instance.
      • doNotScale

        public void doNotScale()
        Do not scale this icon by screen dpi. This methods has effect only if ScalableImageIcon was just created and image was not loaded yet.
      • toImageIcon

        @CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull ResizableIcon icon, int width, int height)
        Convert given icon to a ImageIcon of given width and height. Same icon is returned if it is already ImageIcon and has same size.
        Parameters:
        icon - icon to convert
        width - icon width
        height - icon height
        Returns:
        converted icon or same icon if it is already suitable
      • toImageIcon

        @CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull Icon icon)
        Convert given icon to a ImageIcon. Same icon is returned if it is already ImageIcon.
        Parameters:
        icon - icon to convert
        Returns:
        converted icon or same icon if it is already suitable
      • toImageIcon

        @CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull ResizableIcon icon)
        Convert given icon to a ImageIcon. Same icon is returned if it is already ImageIcon.
        Parameters:
        icon - icon to convert
        Returns:
        converted icon or same icon if it is already suitable