Home
Categories
Dictionnary
Download
Project Details
FAQ
License

SVGImage



The SVGImage is a a JavaFX Group which is the result of the SVGLoader load static method.

Overview

The SVGImage can be used directly in any JavaFX node graph. This class has also several additional methods which allows you to:
  • Get the JavaFX implementations of named SVG nodes
  • Convert the resulting SVGImage to a JavaFX Image
  • Scale a SVGImage
  • Produce a snapshot in a File in an ImageIO supported output format

Limitations

You will be able to use the direct methods to generate a snapshot only if you have swing available. If not, you will still be able to save the image using an alternate third party library to save the converted JavaFX Image on the disk.

Threading usage

As for the SVGLoader class, you can use this class in the JavaFX Platform Thread or in any other Thread. The library will make sure that the conversion will be performed in the JavaFX Platform Thread, and will return the result in the calling Thread.

Scaling a SVGImage

To scale a SVGImage, you can use one of the following methods:
  • The scale(double scale) methods creates a new SVGImage scaled compared to the initial image
  • The scaleTo(double with) method creates a new SVGImage scaled with a new width compared to the initial image
Note that these two methods will no just call setScaleX and setScaleY on the initial image, but will create a new SVGImage with the same SVG input and a scale parameter.

Producing a snapshot

The snapshot(String format, File file) method allows to save a snapshot of the initial image.

Producing a JavaFX image

The toImage() allows to create a JavaFX image from the initial image. It is possible to specify the width of the result by using one of the following methods:
  • The toImage(double width) will create a scaled JavaFX image from the initial image, using the width parameter as the width of the resulting image
  • The toImage(short quality, double width) does the same thing, and allows to specify the quality of the result
  • The toImageScaled(double scaleX, double scaleY) will create a scaled JavaFX image from the initial image
  • The toImageScaled(short quality, double scaleX, double scaleY) will create a scaled JavaFX image from the initial image, and allows to specify the quality of the result
If you use the ScaleQuality.RENDER_QUALITY value for the quality parameter, the result will be scaled at the SVG level before being converted to a JavaFX image.

Example

      SVGImage img = SVGLoader.load(<my SVG file>);
      // the resulting image will have a width of 100 pixels
      Image fxImg = img.toImage(100); 
      // the resulting image will be scaled by a 0.5 factor    
      Image fxImg2 = img.toImageScaled(0.5d); 
      // the resulting image will have a width of 50 pixels, but the image will be scaled at the SVG level before creating
      // the image
      Image fxImg2 = img.toImage(ScaleQuality.RENDER_QUALITY, 50); 

See also


Categories: fromsvg

Copyright 2021-2022 Herve Girod. All Rights Reserved. Documentation and source under the BSD-3-Clause License