Class SvgIcon
- All Implemented Interfaces:
Serializable,Accessible,Icon
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.ImageIcon
ImageIcon.AccessibleImageIcon -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic SvgIconTries to create anSvgIconfrom a resource path defined by the supplied string.static SvgIconCreates anSvgIconfrom a resource path and a custom size.static SvgIconCreates anSvgIconfrom the supplied URL pointing to an SVG document.static SvgIconbooleanintExposes the fixed height defined for the icon, which is the height that was set when the icon was created or updated using thewithIconHeight(int)method.
Note that this height is not scaled according to the currentUI.scale()value.intExposes the fixed width defined for the icon, which is the width that was set when the icon was created or updated using thewithIconWidth(int)method.
Note that this width is not scaled according to the currentUI.scale()value.Allows you to access theUI.FitComponentpolicy, which determines if and how the icon should be fitted onto a component when rendered through thepaintIcon(Component, Graphics, int, int)method.
The following fit modes are available:UI.FitComponent.NO- The image will not be scaled to fit the inner component area.intExposes 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.intExposes 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.getImage()Creates a newImagefrom the SVG document.The preferred placement policy determines where the icon should be placed within a component when rendered through thepaintIcon(Component, Graphics, int, int)method.@Nullable com.github.weisj.jsvg.SVGDocumentAllows you to access the underlyingSVGDocumentthat is used to render the icon.The underlying SVG document contains a size object, which is the width and height of the root SVG element inside the document.inthashCode()static SvgIconof(com.github.weisj.jsvg.SVGDocument svgDocument) Allows you to create anSvgIconfrom an already loadedSVGDocument.static SvgIconAllows you to create anSvgIconfrom an already loadedSVGDocumentwith a custom width and height defined by the suppliedSize.
If you want to customize the layout and placement of the icon, consider creating derived versions using thewithFitComponent(UI.FitComponent)andwithPreferredPlacement(UI.Placement)methods.static SvgIconof(InputStream stream) Reads the SVG document from the supplied input stream and creates anSvgIconfrom it.static SvgIconof(InputStream stream, Size size) static SvgIconAllows you to directly create anSvgIconfrom a string containing an SVG text.static SvgIconvoidvoidWe don't support this.toString()There are different kinds of strategies to fit an SVG icon onto the component.withIconHeight(int height) Creates an updatedSvgIconwith the supplied integer used as the icon height, which you can retrieve usinggetIconHeight().withIconSize(int newWidth, int newHeight) Creates an updatedSvgIconwith the given width and height.withIconSize(Size size) withIconSizeFromHeight(int newHeight) Determines the size of the icon (both width and height) using the provided height and the aspect ratio of the SVG document.withIconSizeFromWidth(int newWidth) Determines the size of the icon (both width and height) using the provided width and the aspect ratio of the SVG document.withIconWidth(int newWidth) Creates an updatedSvgIconwith the given width returned bygetIconWidth().withPreferredPlacement(UI.Placement placement) Allows you to get an updatedSvgIconwith the givenUI.Placementpolicy which determines where the icon should be placed within a component when rendered through thepaintIcon(Component, Graphics, int, int)method.Methods inherited from class javax.swing.ImageIcon
getAccessibleContext, getDescription, getImageLoadStatus, getImageObserver, loadImage, setDescription, setImageObserver
-
Method Details
-
at
Tries to create anSvgIconfrom a resource path defined by the supplied string.- Parameters:
path- The path to the SVG document.
-
at
Creates anSvgIconfrom a resource path and a custom size.- Parameters:
path- The path to the SVG document.size- The size of the icon in the form of aSize.
-
at
Creates anSvgIconfrom the supplied URL pointing to an SVG document. If parsing the SVG document fails, an empty icon will be created, which does not render anything. The resulting icon will have an unknown size, placement and fit policy, meaning that it will be rendered according to the size of the component it is rendered into (seepaintIcon(Component, Graphics, int, int)).- Parameters:
svgUrl- The URL to the SVG document.
-
at
- Parameters:
svgUrl- The URL to the SVG document.size- The size of the icon in the form of aSize.
-
of
Allows you to directly create anSvgIconfrom a string containing an SVG text. -
of
-
of
Reads the SVG document from the supplied input stream and creates anSvgIconfrom it. If parsing the SVG document fails, an empty icon will be created, which does not render anything. The resulting icon will have an unknown size, placement and fit policy, meaning that it will be rendered according to the size of the component it is rendered into (seepaintIcon(Component, Graphics, int, int)). If you want to customize the size, layout and placement of the icon, consider creating derived versions using thewithIconWidth(int),withIconHeight(int),withIconSize(int, int),withFitComponent(UI.FitComponent)andwithPreferredPlacement(UI.Placement)methods.- Parameters:
stream- The input stream supplying the text data of the SVG document.
-
of
Reads the SVG document from the supplied input stream and creates anSvgIconfrom it with a custom width and height defined by the suppliedSize.
If parsing the SVG document fails, an empty icon will be created, which does not render anything. The resulting icon will have the given size, but the default placement and fit policy, meaning that it will be rendered according to the size of the component it is rendered into (seepaintIcon(Component, Graphics, int, int)).
If you want to customize the layout and placement of the icon, consider creating derived versions using thewithFitComponent(UI.FitComponent)andwithPreferredPlacement(UI.Placement)methods.- Parameters:
stream- The input stream supplying the text data of the SVG document.size- The size of the icon in the form of aSize.
-
of
Allows you to create anSvgIconfrom an already loadedSVGDocument. The icon will be created with an unknown size, meaning that it will be rendered according to the size of the component it is rendered into (seepaintIcon(Component, Graphics, int, int)). If you want to customize the size, layout and placement of the icon, consider creating derived versions using thewithIconWidth(int),withIconHeight(int),withIconSize(int, int),withFitComponent(UI.FitComponent)andwithPreferredPlacement(UI.Placement)methods.- Parameters:
svgDocument- The already loaded SVG document, which will be used to render the icon.
-
of
Allows you to create anSvgIconfrom an already loadedSVGDocumentwith a custom width and height defined by the suppliedSize.
If you want to customize the layout and placement of the icon, consider creating derived versions using thewithFitComponent(UI.FitComponent)andwithPreferredPlacement(UI.Placement)methods.- 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 aSize.
-
getIconWidth
public int getIconWidth()Exposes 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. (...or other policies such asUI.FitComponentandUI.Placement).
Note that the returned width is dynamically scaled according to the currentUI.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, usegetBaseWidth().- Specified by:
getIconWidthin interfaceIcon- Overrides:
getIconWidthin classImageIcon- 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
Creates an updatedSvgIconwith the given width returned bygetIconWidth().- Parameters:
newWidth- 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
SvgIconwith 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 asUI.FitComponentandUI.Placement).
Note that the returned height is dynamically scaled according to the currentUI.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, usegetBaseHeight().- Specified by:
getIconHeightin interfaceIcon- Overrides:
getIconHeightin classImageIcon- Returns:
- A new
SvgIconwith 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.
-
getBaseWidth
public int getBaseWidth()Exposes the fixed width defined for the icon, which is the width that was set when the icon was created or updated using thewithIconWidth(int)method.
Note that this width is not scaled according to the currentUI.scale()value. If you want a scaled width, that is more suitable for rendering the icon, use thegetIconWidth()method.- Returns:
- The width of the icon without scaling.
-
getBaseHeight
public int getBaseHeight()Exposes the fixed height defined for the icon, which is the height that was set when the icon was created or updated using thewithIconHeight(int)method.
Note that this height is not scaled according to the currentUI.scale()value. If you want a scaled height, that is more suitable for rendering the icon, use thegetIconHeight()method.- Returns:
- The height of the icon without scaling.
-
withIconHeight
Creates an updatedSvgIconwith the supplied integer used as the icon height, which you can retrieve usinggetIconHeight(). 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 asUI.FitComponentandUI.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
SvgIconwith 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
Creates an updatedSvgIconwith 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, theUI.FitComponent/UI.Placementpolicies or the size of the component the SVG is rendered into.- Parameters:
newWidth- 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.newHeight- 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
SvgIconwith 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
Allows you to create an updatedSvgIconwith the given size in the form of aSizeobject 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 theUI.FitComponent/UI.Placementpolicies. -
withIconSizeFromWidth
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:
newWidth- 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
SvgIconwith the given width and a logical height that is determined by the aspect ratio of the SVG document.
-
withIconSizeFromHeight
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:
newHeight- 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
SvgIconwith the given height and a logical width that is determined by the aspect ratio of the SVG document.
-
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
Size.
-
getSvgDocument
public @Nullable com.github.weisj.jsvg.SVGDocument getSvgDocument()Allows you to access the underlyingSVGDocumentthat is used to render the icon.- Returns:
- The underlying
SVGDocumentthat is used to render the icon.
-
getFitComponent
Allows you to access theUI.FitComponentpolicy, which determines if and how the icon should be fitted onto a component when rendered through thepaintIcon(Component, Graphics, int, int)method.
The following fit modes are available:UI.FitComponent.NO- The image will not be scaled to fit the inner component area.UILayoutConstants.WIDTH(int, int, int)- The image will be scaled to fit the inner component width.UILayoutConstants.HEIGHT(int, int, int)- The image will be scaled to fit the inner component height.UI.FitComponent.WIDTH_AND_HEIGHT- The image will be scaled to fit both the component width and height.UI.FitComponent.MAX_DIM- The image will be scaled to fit the larger of the two dimensions of the inner component area.UI.FitComponent.MIN_DIM- The image will be scaled to fit the smaller of the two dimensions of the inner component area.UI.FitComponent.UNDEFINED- How the image will be scaled to fit the component is unclear. Another property may override this, but typically the behavior is similar toUI.FitComponent.NO.
withFitComponent(UI.FitComponent)if you want to create a newSvgIconwith an updated fit policy.- Returns:
- The
UI.FitComponentthat determines if and how the icon should be fitted into a any given component (seepaintIcon(Component, Graphics, int, int)).
-
withFitComponent
There are different kinds of strategies to fit an SVG icon onto the component. These strategies are identified using theUI.FitComponentenum which defines the following fit modes:UI.FitComponent.NO- The image will not be scaled to fit the inner component area.UILayoutConstants.WIDTH(int, int, int)- The image will be scaled to fit the inner component width.UILayoutConstants.HEIGHT(int, int, int)- The image will be scaled to fit the inner component height.UI.FitComponent.WIDTH_AND_HEIGHT- The image will be scaled to fit both the component width and height.UI.FitComponent.MAX_DIM- The image will be scaled to fit the larger of the two dimension of the inner component area.UI.FitComponent.MIN_DIM- The image will be scaled to fit the smaller of the two dimension of the inner component area.
- Parameters:
fit- TheUI.FitComponentthat determines if and how the icon should be fitted into a any given component (seepaintIcon(Component, Graphics, int, int)).- Returns:
- A new
SvgIconwith the givenUI.FitComponentpolicy.
-
getPreferredPlacement
The preferred placement policy determines where the icon should be placed within a component when rendered through thepaintIcon(Component, Graphics, int, int)method.- Returns:
- The
UI.Placementthat determines where the icon should be placed within a component (seepaintIcon(Component, Graphics, int, int)).
-
withPreferredPlacement
Allows you to get an updatedSvgIconwith the givenUI.Placementpolicy which determines where the icon should be placed within a component when rendered through thepaintIcon(Component, Graphics, int, int)method.- Parameters:
placement- TheUI.Placementthat determines where the icon should be placed within a component (seepaintIcon(Component, Graphics, int, int)).- Returns:
- A new
SvgIconwith the givenUI.Placementpolicy.
-
getImage
Creates a newImagefrom the SVG document. -
setImage
We don't support this. An SVG document is not really an image, it's a vector graphic. ExtendingImageIconis just for compatibility reasons... -
paintIcon
-
hashCode
public int hashCode() -
equals
-
toString
-