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
-
Constructor Summary
ConstructorDescriptionSvgIcon
(com.github.weisj.jsvg.SVGDocument svgDocument) SvgIcon
(InputStream stream) SvgIcon
(InputStream stream, Dimension size) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Allows you to access theUI.FitComponent
policy, which determines if and how the icon should be fitted onto a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, int, int)
method.
The following fit modes are available:UI.FitComponent.NO
- The image will not be scaled to fit the inner component area.int
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.int
getImage()
Creates a newImage
from the SVG document.The preferred placement policy determines where the icon should be placed within a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, int, int)
method.@Nullable com.github.weisj.jsvg.SVGDocument
Allows you to access the underlyingSVGDocument
that is used to render the icon.com.github.weisj.jsvg.geometry.size.FloatSize
The underlying SVG document contains a size object, which is the width and height of the root SVG element inside the document.int
hashCode()
void
void
We don't support this.toString()
There are different kinds of strategies to fit an SVG icon onto the component.withIconHeight
(int height) Creates an updatedSvgIcon
with the supplied integer used as the icon height, which you can retrieve usinggetIconHeight()
.withIconSize
(int width, int height) Creates an updatedSvgIcon
with the given width and height.withIconSize
(Size size) 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.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.withIconWidth
(int width) Creates an updatedSvgIcon
with the given width returned bygetIconWidth()
.withPreferredPlacement
(UI.Placement placement) Allows you to get an updatedSvgIcon
with the givenUI.Placement
policy which determines where the icon should be placed within a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, int, int)
method.Methods inherited from class javax.swing.ImageIcon
getAccessibleContext, getDescription, getImageLoadStatus, getImageObserver, loadImage, setDescription, setImageObserver
-
Constructor Details
-
SvgIcon
- Parameters:
path
- The path to the SVG document.
-
SvgIcon
- Parameters:
svgUrl
- The URL to the SVG document.
-
SvgIcon
- 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
- Parameters:
path
- The path to the SVG document.size
- The size of the icon in the form of aDimension
.
-
SvgIcon
- Parameters:
svgUrl
- The URL to the SVG document.size
- The size of the icon in the form of aDimension
.
-
SvgIcon
- Parameters:
stream
- The input stream supplying the text data of the SVG document.size
- The size of the icon in the form of aDimension
.
-
SvgIcon
- 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 aDimension
.
-
-
Method Details
-
getIconWidth
public int getIconWidth()- Specified by:
getIconWidth
in interfaceIcon
- Overrides:
getIconWidth
in 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 updatedSvgIcon
with the given width returned bygetIconWidth()
.- 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 asUI.FitComponent
andUI.Placement
).- Specified by:
getIconHeight
in interfaceIcon
- Overrides:
getIconHeight
in classImageIcon
- 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
Creates an updatedSvgIcon
with 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.FitComponent
andUI.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
Creates an updatedSvgIcon
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, theUI.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
Allows you to create an updatedSvgIcon
with the given size in the form of aSize
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 theUI.FitComponent
/UI.Placement
policies. -
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:
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
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 ofDimension2D
.
-
getSvgDocument
public @Nullable com.github.weisj.jsvg.SVGDocument getSvgDocument()Allows you to access the underlyingSVGDocument
that is used to render the icon.- Returns:
- The underlying
SVGDocument
that is used to render the icon.
-
getFitComponent
Allows you to access theUI.FitComponent
policy, which determines if and how the icon should be fitted onto a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, 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.
withFitComponent(UI.FitComponent)
if you want to create a newSvgIcon
with an updated fit policy.- Returns:
- The
UI.FitComponent
that determines if and how the icon should be fitted into a any given component (seepaintIcon(Component, java.awt.Graphics, int, int, int, int)
).
-
withFitComponent
There are different kinds of strategies to fit an SVG icon onto the component. These strategies are identified using theUI.FitComponent
enum 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.FitComponent
that determines if and how the icon should be fitted into a any given component (seepaintIcon(Component, java.awt.Graphics, int, int, int, int)
).- Returns:
- A new
SvgIcon
with the givenUI.FitComponent
policy.
-
getPreferredPlacement
The preferred placement policy determines where the icon should be placed within a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, int, int)
method.- Returns:
- The
UI.Placement
that determines where the icon should be placed within a component (seepaintIcon(Component, java.awt.Graphics, int, int, int, int)
).
-
withPreferredPlacement
Allows you to get an updatedSvgIcon
with the givenUI.Placement
policy which determines where the icon should be placed within a component when rendered through thepaintIcon(Component, java.awt.Graphics, int, int, int, int)
method.- Parameters:
placement
- TheUI.Placement
that determines where the icon should be placed within a component (seepaintIcon(Component, java.awt.Graphics, int, int, int, int)
).- Returns:
- A new
SvgIcon
with the givenUI.Placement
policy.
-
getImage
Creates a newImage
from the SVG document. -
setImage
We don't support this. An SVG document is not really an image, it's a vector graphic. ExtendingImageIcon
is just for compatibility reasons... -
paintIcon
-
hashCode
public int hashCode() -
equals
-
toString
-