Class FlatSVGIcon
- java.lang.Object
-
- javax.swing.ImageIcon
-
- com.formdev.flatlaf.extras.FlatSVGIcon
-
- All Implemented Interfaces:
FlatLaf.DisabledIconProvider
,java.io.Serializable
,javax.accessibility.Accessible
,javax.swing.Icon
public class FlatSVGIcon extends javax.swing.ImageIcon implements FlatLaf.DisabledIconProvider
An icon that loads and paints SVG.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FlatSVGIcon.ColorFilter
A color filter that can modify colors of a paintedFlatSVGIcon
.private static class
FlatSVGIcon.GraphicsFilter
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ClassLoader
classLoader
private FlatSVGIcon.ColorFilter
colorFilter
private boolean
dark
private static java.lang.Boolean
darkLaf
private boolean
disabled
private com.github.weisj.jsvg.SVGDocument
document
private int
height
private boolean
loadFailed
private static boolean
loggingEnabled
private java.lang.String
name
private float
scale
private static SoftCache<java.lang.String,com.github.weisj.jsvg.SVGDocument>
svgCache
private static boolean
svgCacheEnabled
private static com.github.weisj.jsvg.parser.SVGLoader
svgLoader
private java.net.URL
url
private int
width
-
Constructor Summary
Constructors Modifier Constructor Description FlatSVGIcon(FlatSVGIcon icon)
Creates a copy of the given icon.FlatSVGIcon(java.io.File file)
Creates an SVG icon from the given file.FlatSVGIcon(java.io.InputStream in)
Creates an SVG icon from the given input stream.FlatSVGIcon(java.lang.String name)
Creates an SVG icon from the given resource name.FlatSVGIcon(java.lang.String name, float scale)
Creates an SVG icon from the given resource name that is scaled by the given amount.FlatSVGIcon(java.lang.String name, float scale, java.lang.ClassLoader classLoader)
Creates an SVG icon from the given resource name that is scaled by the given amount.FlatSVGIcon(java.lang.String name, int width, int height)
Creates an SVG icon from the given resource name with the given width and height.protected
FlatSVGIcon(java.lang.String name, int width, int height, float scale, boolean disabled, java.lang.ClassLoader classLoader, java.net.URL url)
FlatSVGIcon(java.lang.String name, int width, int height, java.lang.ClassLoader classLoader)
Creates an SVG icon from the given resource name with the given width and height.FlatSVGIcon(java.lang.String name, java.lang.ClassLoader classLoader)
Creates an SVG icon from the given resource name.FlatSVGIcon(java.net.URI uri)
Creates an SVG icon from the given URI.FlatSVGIcon(java.net.URL url)
Creates an SVG icon from the given URL.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clearSVGDocumentCache()
FlatSVGIcon
derive(float scale)
Creates a new icon with given scaling, which is derived from this icon.FlatSVGIcon
derive(int width, int height)
Creates a new icon with given width and height, which is derived from this icon.java.lang.ClassLoader
getClassLoader()
Returns the class loader used to load the SVG resource.FlatSVGIcon.ColorFilter
getColorFilter()
Returns the currently active color filter ornull
.javax.swing.Icon
getDisabledIcon()
Creates a new icon with disabled appearance, which is derived from this icon.int
getHeight()
Returns the custom icon height specified inFlatSVGIcon(String, int, int)
,FlatSVGIcon(String, int, int, ClassLoader)
orderive(int, int)
.int
getIconHeight()
Returns the scaled height of the icon.private java.net.URL
getIconURL(java.lang.String name, boolean dark)
int
getIconWidth()
Returns the scaled width of the icon.java.awt.Image
getImage()
java.lang.String
getName()
Returns the name of the SVG resource (a '/'-separated path).float
getScale()
Returns the amount by which the icon size is scaled.int
getWidth()
Returns the custom icon width specified inFlatSVGIcon(String, int, int)
,FlatSVGIcon(String, int, int, ClassLoader)
orderive(int, int)
.boolean
hasFound()
Returns whether the SVG file was found.static boolean
isDarkLaf()
Checks whether the current look and feel is dark.boolean
isDisabled()
Returns whether the icon is pained in "disabled" state.static boolean
isLoggingEnabled()
static boolean
isSVGDocumentEnabled()
private static void
lafChanged()
(package private) static com.github.weisj.jsvg.SVGDocument
loadSVG(java.net.URL url)
private static com.github.weisj.jsvg.SVGDocument
loadSVGUncached(java.net.URL url)
void
paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
private void
paintSvg(java.awt.Graphics2D g, int x, int y)
private void
paintSvgError(java.awt.Graphics2D g, int x, int y)
private int
scaleSize(int size)
FlatSVGIcon
setColorFilter(FlatSVGIcon.ColorFilter colorFilter)
Sets a color filter that can freely modify colors of this icon during painting.static void
setLoggingEnabled(boolean loggingEnabled)
(package private) static void
setRenderingHints(java.awt.Graphics2D g)
static void
setSVGDocumentEnabled(boolean svgCacheEnabled)
private void
update()
(package private) static java.net.URL
uri2url(java.net.URI uri)
-
-
-
Field Detail
-
loggingEnabled
private static boolean loggingEnabled
-
svgCacheEnabled
private static boolean svgCacheEnabled
-
svgCache
private static final SoftCache<java.lang.String,com.github.weisj.jsvg.SVGDocument> svgCache
-
svgLoader
private static final com.github.weisj.jsvg.parser.SVGLoader svgLoader
-
name
private final java.lang.String name
-
width
private final int width
-
height
private final int height
-
scale
private final float scale
-
disabled
private final boolean disabled
-
classLoader
private final java.lang.ClassLoader classLoader
-
url
private final java.net.URL url
-
colorFilter
private FlatSVGIcon.ColorFilter colorFilter
-
document
private com.github.weisj.jsvg.SVGDocument document
-
dark
private boolean dark
-
loadFailed
private boolean loadFailed
-
darkLaf
private static java.lang.Boolean darkLaf
-
-
Constructor Detail
-
FlatSVGIcon
public FlatSVGIcon(java.lang.String name)
Creates an SVG icon from the given resource name.The SVG attributes
width
andheight
(orviewBox
) 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, useFlatSVGIcon(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(java.lang.String name, java.lang.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
andheight
(orviewBox
) 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, useFlatSVGIcon(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(java.lang.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, useFlatSVGIcon(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 iconheight
- the height of the icon- See Also:
ClassLoader.getResource(String)
-
FlatSVGIcon
public FlatSVGIcon(java.lang.String name, int width, int height, java.lang.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, useFlatSVGIcon(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 iconheight
- the height of the iconclassLoader
- the class loader used to load the SVG resource- See Also:
ClassLoader.getResource(String)
-
FlatSVGIcon
public FlatSVGIcon(java.lang.String name, float scale)
Creates an SVG icon from the given resource name that is scaled by the given amount.The SVG attributes
width
andheight
(orviewBox
) 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, useFlatSVGIcon(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(java.lang.String name, float scale, java.lang.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
andheight
(orviewBox
) 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, useFlatSVGIcon(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 scaledclassLoader
- the class loader used to load the SVG resource- See Also:
ClassLoader.getResource(String)
-
FlatSVGIcon
public FlatSVGIcon(java.net.URL url)
Creates an SVG icon from the given URL.The SVG attributes
width
andheight
(orviewBox
) 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(java.net.URI uri)
Creates an SVG icon from the given URI.The SVG attributes
width
andheight
(orviewBox
) 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(java.io.File file)
Creates an SVG icon from the given file.The SVG attributes
width
andheight
(orviewBox
) 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(java.io.InputStream in) throws java.io.IOException
Creates an SVG icon from the given input stream.The SVG attributes
width
andheight
(orviewBox
) 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 an SVG resource- Throws:
java.io.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(java.lang.String name, int width, int height, float scale, boolean disabled, java.lang.ClassLoader classLoader, java.net.URL url)
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns the name of the SVG resource (a '/'-separated path).- Since:
- 1.2
-
getWidth
public int getWidth()
Returns the custom icon width specified inFlatSVGIcon(String, int, int)
,FlatSVGIcon(String, int, int, ClassLoader)
orderive(int, int)
. Otherwise-1
is returned.To get the painted icon width, use
getIconWidth()
.- Since:
- 1.2
-
getHeight
public int getHeight()
Returns the custom icon height specified inFlatSVGIcon(String, int, int)
,FlatSVGIcon(String, int, int, ClassLoader)
orderive(int, int)
. Otherwise-1
is returned.To get the painted icon height, use
getIconHeight()
.- Since:
- 1.2
-
getScale
public float getScale()
Returns the amount by which the icon size is scaled. Usually1
.- Since:
- 1.2
-
isDisabled
public boolean isDisabled()
Returns whether the icon is pained in "disabled" state.- Since:
- 1.2
- See Also:
getDisabledIcon()
-
getClassLoader
public java.lang.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 iconheight
- 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 javax.swing.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 interfaceFlatLaf.DisabledIconProvider
- Returns:
- a new icon
-
getColorFilter
public FlatSVGIcon.ColorFilter getColorFilter()
Returns the currently active color filter ornull
.- Since:
- 1.2
-
setColorFilter
public FlatSVGIcon 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 theColorFilter(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
-
update
private void update()
-
loadSVG
static com.github.weisj.jsvg.SVGDocument loadSVG(java.net.URL url)
-
loadSVGUncached
private static com.github.weisj.jsvg.SVGDocument loadSVGUncached(java.net.URL url)
-
getIconURL
private java.net.URL getIconURL(java.lang.String name, boolean dark)
-
hasFound
public boolean hasFound()
Returns whether the SVG file was found.- Returns:
- whether the SVG file was found
-
getIconWidth
public int getIconWidth()
Returns the scaled width of the icon.- Specified by:
getIconWidth
in interfacejavax.swing.Icon
- Overrides:
getIconWidth
in classjavax.swing.ImageIcon
-
getIconHeight
public int getIconHeight()
Returns the scaled height of the icon.- Specified by:
getIconHeight
in interfacejavax.swing.Icon
- Overrides:
getIconHeight
in classjavax.swing.ImageIcon
-
scaleSize
private int scaleSize(int size)
-
paintIcon
public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
- Specified by:
paintIcon
in interfacejavax.swing.Icon
- Overrides:
paintIcon
in classjavax.swing.ImageIcon
-
paintSvg
private void paintSvg(java.awt.Graphics2D g, int x, int y)
-
paintSvgError
private void paintSvgError(java.awt.Graphics2D g, int x, int y)
-
getImage
public java.awt.Image getImage()
- Overrides:
getImage
in classjavax.swing.ImageIcon
-
setRenderingHints
static void setRenderingHints(java.awt.Graphics2D g)
-
uri2url
static java.net.URL uri2url(java.net.URI uri)
-
isDarkLaf
public static boolean isDarkLaf()
Checks whether the current look and feel is dark.Uses
FlatLaf.isLafDark()
and caches the result.- Since:
- 1.2
-
lafChanged
private static void lafChanged()
-
isLoggingEnabled
public static boolean isLoggingEnabled()
- Since:
- 3.4.1
-
setLoggingEnabled
public static void setLoggingEnabled(boolean loggingEnabled)
- Since:
- 3.4.1
-
isSVGDocumentEnabled
public static boolean isSVGDocumentEnabled()
- Since:
- 3.4.1
-
setSVGDocumentEnabled
public static void setSVGDocumentEnabled(boolean svgCacheEnabled)
- Since:
- 3.4.1
-
clearSVGDocumentCache
public static void clearSVGDocumentCache()
- Since:
- 3.4.1
-
-