Class FlatSVGUtils


  • public class FlatSVGUtils
    extends java.lang.Object
    Utility methods for SVG.
    • Constructor Summary

      Constructors 
      Constructor Description
      FlatSVGUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.awt.Image> createWindowIconImages​(java.lang.String svgName)
      Creates from the given SVG a list of icon images with different sizes that can be used for windows headers.
      static java.util.List<java.awt.Image> createWindowIconImages​(java.net.URL svgUrl)
      Creates from the given SVG a list of icon images with different sizes that can be used for windows headers.
      private static java.net.URL getResource​(java.lang.String svgName)  
      private static java.awt.image.BufferedImage svg2image​(com.github.weisj.jsvg.SVGDocument document, int width, int height)
      Creates a buffered image and renders the given SVGDocument into it.
      static java.awt.image.BufferedImage svg2image​(java.lang.String svgName, float scaleFactor)
      Creates a buffered image and renders the given SVG into it.
      static java.awt.image.BufferedImage svg2image​(java.lang.String svgName, int width, int height)
      Creates a buffered image and renders the given SVG into it.
      static java.awt.image.BufferedImage svg2image​(java.net.URL svgUrl, float scaleFactor)
      Creates a buffered image and renders the given SVG into it.
      static java.awt.image.BufferedImage svg2image​(java.net.URL svgUrl, int width, int height)
      Creates a buffered image and renders the given SVG into it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FlatSVGUtils

        public FlatSVGUtils()
    • Method Detail

      • createWindowIconImages

        public static java.util.List<java.awt.Image> createWindowIconImages​(java.lang.String svgName)
        Creates from the given SVG a list of icon images with different sizes that can be used for windows headers. The SVG should have a size of 16x16, otherwise it is scaled.

        If running on Windows in Java 9 or later and multi-resolution image support is available, then a single multi-resolution image is returned that creates images on demand for requested sizes from SVG. This has the advantage that only images for used sizes are created. Also, if unusual sizes are requested (e.g. 18x18), then they are created from SVG.

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

        Parameters:
        svgName - the name of the SVG resource (a '/'-separated path)
        Returns:
        list of icon images with different sizes (16x16, 20x20, 24x24, 28x28, 32x32, 48x48 and 64x64)
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
        See Also:
        Window.setIconImages(List)
      • createWindowIconImages

        public static java.util.List<java.awt.Image> createWindowIconImages​(java.net.URL svgUrl)
        Creates from the given SVG a list of icon images with different sizes that can be used for windows headers. The SVG should have a size of 16x16, otherwise it is scaled.

        If running on Windows in Java 9 or later and multi-resolution image support is available, then a single multi-resolution image is returned that creates images on demand for requested sizes from SVG. This has the advantage that only images for used sizes are created. Also, if unusual sizes are requested (e.g. 18x18), then they are created from SVG.

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

        Parameters:
        svgUrl - the URL of the SVG resource
        Returns:
        list of icon images with different sizes (16x16, 20x20, 24x24, 28x28, 32x32, 48x48 and 64x64)
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
        Since:
        2
        See Also:
        Window.setIconImages(List)
      • svg2image

        public static java.awt.image.BufferedImage svg2image​(java.lang.String svgName,
                                                             int width,
                                                             int height)
        Creates a buffered image and renders the given SVG into it.

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

        Parameters:
        svgName - the name of the SVG resource (a '/'-separated path)
        width - the width of the image
        height - the height of the image
        Returns:
        the image
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
      • svg2image

        public static java.awt.image.BufferedImage svg2image​(java.net.URL svgUrl,
                                                             int width,
                                                             int height)
        Creates a buffered image and renders the given SVG into it.

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

        Parameters:
        svgUrl - the URL of the SVG resource
        width - the width of the image
        height - the height of the image
        Returns:
        the image
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
        Since:
        2
      • svg2image

        public static java.awt.image.BufferedImage svg2image​(java.lang.String svgName,
                                                             float scaleFactor)
        Creates a buffered image and renders the given SVG into it.

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

        Parameters:
        svgName - the name of the SVG resource (a '/'-separated path)
        scaleFactor - the amount by which the SVG size is scaled
        Returns:
        the image
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
      • svg2image

        public static java.awt.image.BufferedImage svg2image​(java.net.URL svgUrl,
                                                             float scaleFactor)
        Creates a buffered image and renders the given SVG into it.

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

        Parameters:
        svgUrl - the URL of the SVG resource
        scaleFactor - the amount by which the SVG size is scaled
        Returns:
        the image
        Throws:
        java.lang.RuntimeException - if failed to load or render SVG file
        Since:
        2
      • svg2image

        private static java.awt.image.BufferedImage svg2image​(com.github.weisj.jsvg.SVGDocument document,
                                                              int width,
                                                              int height)
        Creates a buffered image and renders the given SVGDocument into it.
        Parameters:
        document - the SVG document
        width - the width of the image
        height - the height of the image
        Returns:
        the image
        Throws:
        java.lang.RuntimeException - if failed to render SVG file
      • getResource

        private static java.net.URL getResource​(java.lang.String svgName)