Class FlatSVGIcon

  • All Implemented Interfaces:
    com.formdev.flatlaf.FlatLaf.DisabledIconProvider, Serializable, Accessible, Icon

    public class FlatSVGIcon
    extends ImageIcon
    implements com.formdev.flatlaf.FlatLaf.DisabledIconProvider
    An icon that loads and paints SVG.
    See Also:
    Serialized Form
    • Constructor Detail

      • FlatSVGIcon

        public FlatSVGIcon​(String name)
        Creates an SVG icon from the given resource name.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(String name,
                           ClassLoader classLoader)
        Creates an SVG icon from the given resource name. The SVG file is loaded from the given class loader.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        classLoader - the class loader used to load the SVG resource
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(String name,
                           int width,
                           int height)
        Creates an SVG icon from the given resource name with the given width and height.

        The icon is scaled if the given size is different to the size specified in the SVG file.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        width - the width of the icon
        height - the height of the icon
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(String name,
                           int width,
                           int height,
                           ClassLoader classLoader)
        Creates an SVG icon from the given resource name with the given width and height. The SVG file is loaded from the given class loader.

        The icon is scaled if the given size is different to the size specified in the SVG file.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        width - the width of the icon
        height - the height of the icon
        classLoader - the class loader used to load the SVG resource
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(String name,
                           float scale)
        Creates an SVG icon from the given resource name that is scaled by the given amount.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as base icon size, which is multiplied by the given scale factor.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        scale - the amount by which the icon size is scaled
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(String name,
                           float scale,
                           ClassLoader classLoader)
        Creates an SVG icon from the given resource name that is scaled by the given amount. The SVG file is loaded from the given class loader.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as base icon size, which is multiplied by the given scale factor.

        If using Java modules, the package containing the icon must be opened in module-info.java. Otherwise use FlatSVGIcon(URL).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        name - the name of the SVG resource (a '/'-separated path; e.g. "com/myapp/myicon.svg")
        scale - the amount by which the icon size is scaled
        classLoader - the class loader used to load the SVG resource
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(URL url)
        Creates an SVG icon from the given URL.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        This method is useful if using Java modules and the package containing the icon is not opened in module-info.java. E.g. new FlatSVGIcon( getClass().getResource( "/com/myapp/myicon.svg" ) ).

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        url - the URL of the SVG resource
        Since:
        2
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(URI uri)
        Creates an SVG icon from the given URI.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        uri - the URI of the SVG resource
        Since:
        2
        See Also:
        ClassLoader.getResource(String)
      • FlatSVGIcon

        public FlatSVGIcon​(File file)
        Creates an SVG icon from the given file.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        This is cheap operation because the icon is only loaded when used.

        Parameters:
        file - the SVG file
        Since:
        2
      • FlatSVGIcon

        public FlatSVGIcon​(InputStream in)
                    throws IOException
        Creates an SVG icon from the given input stream.

        The SVG attributes width and height (or viewBox) in the tag <svg> are used as icon size.

        The input stream is loaded, parsed and closed immediately.

        Parameters:
        in - the input stream for reading a SVG resource
        Throws:
        IOException - if an I/O exception occurs
        Since:
        2
      • FlatSVGIcon

        public FlatSVGIcon​(FlatSVGIcon icon)
        Creates a copy of the given icon.

        If the icon has a color filter, then it is shared with the new icon.

        Since:
        2.0.1
      • FlatSVGIcon

        protected FlatSVGIcon​(String name,
                              int width,
                              int height,
                              float scale,
                              boolean disabled,
                              ClassLoader classLoader,
                              URI uri)
    • Method Detail

      • getName

        public String getName()
        Returns the name of the SVG resource (a '/'-separated path).
        Since:
        1.2
      • getScale

        public float getScale()
        Returns the amount by which the icon size is scaled. Usually 1.
        Since:
        1.2
      • isDisabled

        public boolean isDisabled()
        Returns whether the icon is pained in "disabled" state.
        Since:
        1.2
        See Also:
        getDisabledIcon()
      • getClassLoader

        public ClassLoader getClassLoader()
        Returns the class loader used to load the SVG resource.
        Since:
        1.2
      • derive

        public FlatSVGIcon derive​(int width,
                                  int height)
        Creates a new icon with given width and height, which is derived from this icon.

        If the icon has a color filter, then it is shared with the new icon.

        Parameters:
        width - the width of the new icon
        height - the height of the new icon
        Returns:
        a new icon
      • derive

        public FlatSVGIcon derive​(float scale)
        Creates a new icon with given scaling, which is derived from this icon.

        If the icon has a color filter, then it is shared with the new icon.

        Parameters:
        scale - the amount by which the icon size is scaled
        Returns:
        a new icon
      • getDisabledIcon

        public Icon getDisabledIcon()
        Creates a new icon with disabled appearance, which is derived from this icon.

        If the icon has a color filter, then it is shared with the new icon.

        Specified by:
        getDisabledIcon in interface com.formdev.flatlaf.FlatLaf.DisabledIconProvider
        Returns:
        a new icon
      • getColorFilter

        public FlatSVGIcon.ColorFilter getColorFilter()
        Returns the currently active color filter or null.
        Since:
        1.2
      • setColorFilter

        public void setColorFilter​(FlatSVGIcon.ColorFilter colorFilter)
        Sets a color filter that can freely modify colors of this icon during painting.

        This method accepts a FlatSVGIcon.ColorFilter. Usually you would want to use a ColorFilter created using the ColorFilter(Function) constructor.

        This can be used to brighten colors of the icon:

        icon.setColorFilter( new FlatSVGIcon.ColorFilter( color -> color.brighter() ) );

        Using a filter, icons can also be turned monochrome (painted with a single color):

        icon.setColorFilter( new FlatSVGIcon.ColorFilter( color -> Color.RED ) );

        Note: If a filter is already set, it will be replaced.

        Parameters:
        colorFilter - The color filter
        Since:
        1.2
      • hasFound

        public boolean hasFound()
        Returns whether the SVG file was found.
        Returns:
        whether the SVG file was found
      • isDarkLaf

        public static boolean isDarkLaf()
        Checks whether the current look and feel is dark.

        Uses FlatLaf.isLafDark() and caches the result.

        Since:
        1.2