Class SvgIcon

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

public final class SvgIcon extends ImageIcon
A specialized ImageIcon subclass that allows you to use SVG based icon images in your GUI. This in essence just a wrapper around the JSVG library, which renders SVG images using the Java2D graphics API.

You may use this like a regular ImageIcon, but have to keep in mind that SVG documents do not really have a fixed size, meaning that the getIconWidth() and getIconHeight() on a freshly loaded SvgIcon will return -1 which causes the icon to be rendered according to the width and height of the component it is rendered into (see paintIcon(Component, java.awt.Graphics, int, int)).

If you want to render the icon with a fixed size, you can use the withIconWidth(int) and withIconHeight(int) or withIconSize(int, int) methods to create a new SvgIcon with the given width and height.

An SvgIcon with an undefined width or height will also be using the UI.FitComponent and UI.Placement policies to determine how the icon should be placed and sized within a component. Use the withFitComponent(UI.FitComponent) and withPreferredPlacement(UI.Placement) methods to create a new SvgIcon with the given policies and use the getFitComponent() and getPreferredPlacement() methods to retrieve the current policies (Not that these will not have any effect if the width and height are both defined).

Also note that the direct use of this class and it's API is discouraged in favour of simply calling the UIFactoryMethods.findIcon(String) or UIFactoryMethods.findSvgIcon(String) methods, which will automatically load and cache all of the icons for you.

See Also:
  • Constructor Details

    • SvgIcon

      public SvgIcon(String path)
      Parameters:
      path - The path to the SVG document.
    • SvgIcon

      public SvgIcon(URL svgUrl)
      Parameters:
      svgUrl - The URL to the SVG document.
    • SvgIcon

      public SvgIcon(InputStream stream)
      Parameters:
      stream - The input stream supplying the text data of the SVG document.
    • SvgIcon

      public SvgIcon(com.github.weisj.jsvg.SVGDocument svgDocument)
      Parameters:
      svgDocument - The already loaded SVG document, which will be used to render the icon.
    • SvgIcon

      public SvgIcon(String path, Dimension size)
      Parameters:
      path - The path to the SVG document.
      size - The size of the icon in the form of a Dimension.
    • SvgIcon

      public SvgIcon(URL svgUrl, Dimension size)
      Parameters:
      svgUrl - The URL to the SVG document.
      size - The size of the icon in the form of a Dimension.
    • SvgIcon

      public SvgIcon(InputStream stream, Dimension size)
      Parameters:
      stream - The input stream supplying the text data of the SVG document.
      size - The size of the icon in the form of a Dimension.
    • SvgIcon

      public SvgIcon(com.github.weisj.jsvg.SVGDocument svgDocument, Dimension size)
      Parameters:
      svgDocument - The already loaded SVG document, which will be used to render the icon.
      size - The size of the icon in the form of a Dimension.
  • Method Details

    • getIconWidth

      public int getIconWidth()
      Specified by:
      getIconWidth in interface Icon
      Overrides:
      getIconWidth in class ImageIcon
      Returns:
      The width of the icon, or -1 if the icon should be rendered according to the width of a given component or the width of the SVG document itself.
    • withIconWidth

      public SvgIcon withIconWidth(int width)
      Creates an updated SvgIcon with the given width returned by getIconWidth().
      Parameters:
      width - The width of the icon, or -1 if the icon should be rendered according to the width of a given component or the width of the SVG document itself.
      Returns:
      A new SvgIcon with the given width. If the width is -1, the icon will be rendered according to the width of a given component or the width of the SVG document itself.
    • getIconHeight

      public int getIconHeight()
      Exposes the height of the icon, or -1 if the icon should be rendered according to the height of a given component or the height of the SVG document itself. (...or other policies such as UI.FitComponent and UI.Placement).
      Specified by:
      getIconHeight in interface Icon
      Overrides:
      getIconHeight in class ImageIcon
      Returns:
      A new SvgIcon with the given width and height. If the width or height is -1, the icon will be rendered according to the width or height of a given component or the width or height of the SVG document itself.
    • withIconHeight

      public SvgIcon withIconHeight(int height)
      Creates an updated SvgIcon with the supplied integer used as the icon height, which you can retrieve using getIconHeight(). If the height is -1, the icon will be rendered according to the height of a given component or the height of the SVG document itself. (...or other policies such as UI.FitComponent and UI.Placement).
      Parameters:
      height - The height of the icon, or -1 if the icon should be rendered according to the height of a given component or the height of the SVG document itself.
      Returns:
      A new SvgIcon with the given height. If the height is -1, the icon will be rendered according to the height of a given component or the height of the SVG document itself.
    • withIconSize

      public SvgIcon withIconSize(int width, int height)
      Creates an updated SvgIcon with the given width and height. Dimensions smaller than 0 are considered "undefined". When the icon is being rendered then these will be determined according to the aspect ratio of the SVG document, the UI.FitComponent / UI.Placement policies or the size of the component the SVG is rendered into.
      Parameters:
      width - The width of the icon, or -1 if the icon should be rendered according to the width of a given component or the width of the SVG document itself.
      height - The height of the icon, or -1 if the icon should be rendered according to the height of a given component or the height of the SVG document itself.
      Returns:
      A new SvgIcon with the given width and height. If the width or height is -1, the icon will be rendered according to the width or height of a given component or the width or height of the SVG document itself.
    • withIconSize

      public SvgIcon withIconSize(Size size)
      Allows you to create an updated SvgIcon with the given size in the form of a Size object containing the width and height. If the width or height is -1, the icon will be rendered according to the width or height of a given component, the width or height of the SVG document and the UI.FitComponent / UI.Placement policies.
      Parameters:
      size - The size of the icon in the form of a Size.
      Returns:
      A new SvgIcon with the given width and height.
    • withIconSizeFromWidth

      public SvgIcon withIconSizeFromWidth(int width)
      Determines the size of the icon (both width and height) using the provided width and the aspect ratio of the SVG document. If the width is -1, the icon will lose its fixed width and will be rendered according to the width of a given component.

      For example, if the SVG document has an aspect ratio of 2:1, and the width is 200, then the height will be 100.

      Also see withIconSizeFromHeight(int).

      Parameters:
      width - The width of the icon, or -1 if the icon should be rendered according to the width of a given component or the width of the SVG document itself.
      Returns:
      A new SvgIcon with the given width and a logical height that is determined by the aspect ratio of the SVG document.
    • withIconSizeFromHeight

      public SvgIcon withIconSizeFromHeight(int height)
      Determines the size of the icon (both width and height) using the provided height and the aspect ratio of the SVG document. If the height is -1, the icon will lose its fixed height and will be rendered according to the height of a given component.

      For example, if the SVG document has an aspect ratio of 2:1, and the height is 100, then the width will be 200.

      Also see withIconSizeFromWidth(int).

      Parameters:
      height - The height of the icon, or -1 if the icon should be rendered according to the height of a given component or the height of the SVG document itself.
      Returns:
      A new SvgIcon with the given height and a logical width that is determined by the aspect ratio of the SVG document.
    • getSvgSize

      public com.github.weisj.jsvg.geometry.size.FloatSize getSvgSize()
      The underlying SVG document contains a size object, which is the width and height of the root SVG element inside the document.
      Returns:
      The size of the SVG document in the form of a FloatSize, a subclass of Dimension2D.
    • getSvgDocument

      public @Nullable com.github.weisj.jsvg.SVGDocument getSvgDocument()
      Allows you to access the underlying SVGDocument that is used to render the icon.
      Returns:
      The underlying SVGDocument that is used to render the icon.
    • getFitComponent

      public UI.FitComponent getFitComponent()
      Allows you to access the UI.FitComponent policy, which determines if and how the icon should be fitted onto a component when rendered through the paintIcon(Component, java.awt.Graphics, int, int, int, int) method.
      The following fit modes are available: See withFitComponent(UI.FitComponent) if you want to create a new SvgIcon with an updated fit policy.
      Returns:
      The UI.FitComponent that determines if and how the icon should be fitted into a any given component (see paintIcon(Component, java.awt.Graphics, int, int, int, int)).
    • withFitComponent

      public SvgIcon withFitComponent(UI.FitComponent fit)
      There are different kinds of strategies to fit an SVG icon onto the component. These strategies are identified using the UI.FitComponent enum which defines the following fit modes:
      Parameters:
      fit - The UI.FitComponent that determines if and how the icon should be fitted into a any given component (see paintIcon(Component, java.awt.Graphics, int, int, int, int)).
      Returns:
      A new SvgIcon with the given UI.FitComponent policy.
    • getPreferredPlacement

      public UI.Placement getPreferredPlacement()
      The preferred placement policy determines where the icon should be placed within a component when rendered through the paintIcon(Component, java.awt.Graphics, int, int, int, int) method.
      Returns:
      The UI.Placement that determines where the icon should be placed within a component (see paintIcon(Component, java.awt.Graphics, int, int, int, int)).
    • withPreferredPlacement

      public SvgIcon withPreferredPlacement(UI.Placement placement)
      Allows you to get an updated SvgIcon with the given UI.Placement policy which determines where the icon should be placed within a component when rendered through the paintIcon(Component, java.awt.Graphics, int, int, int, int) method.
      Parameters:
      placement - The UI.Placement that determines where the icon should be placed within a component (see paintIcon(Component, java.awt.Graphics, int, int, int, int)).
      Returns:
      A new SvgIcon with the given UI.Placement policy.
    • getImage

      public Image getImage()
      Creates a new Image from the SVG document.
      Overrides:
      getImage in class ImageIcon
      Returns:
      A new Image where the SVG document has been rendered into.
    • setImage

      public void setImage(Image image)
      We don't support this. An SVG document is not really an image, it's a vector graphic. Extending ImageIcon is just for compatibility reasons...
      Overrides:
      setImage in class ImageIcon
    • paintIcon

      public void paintIcon(Component c, Graphics g, int x, int y)
      Specified by:
      paintIcon in interface Icon
      Overrides:
      paintIcon in class ImageIcon
      Parameters:
      c - The component to render the icon into.
      g - the graphics context
      x - the X coordinate of the icon's top-left corner
      y - the Y coordinate of the icon's top-left corner
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

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

      public String toString()
      Overrides:
      toString in class ImageIcon