Home
Categories
Dictionnary
Download
Project Details
FAQ
License

SVG to JavaFX conversion Usage



The most simple way to use the library is just by calling one of the SVGLoader.load static methods. For example:

      SVGImage img = SVGLoader.load(<my SVG file>);

The SVGImage result is a JavaFX Group.

Parameters

Main Article: LoaderParameters

By default the result has the same size as the input SVG image. However several parameters allow to configure the result:
  • It is possible to specify the width of the result (the resulting JavaFX result will have the same form factor as the SVG input
  • It is possible to specify the scale of the result
  • It is possible to specify a stylesheet to use for the result
This can be achieved through one of the load static methods. You can also specify how you want to load the SVG content by using the LoaderParameters class.

Converting to an image


The SVGImage result can be converted to a JavaFX Image. The simplest way to perform the conversion is by using the toImage method. For example:

      SVGImage img = SVGLoader.load(<my SVG file>);
      Image fxImg = img.toImage();

Several conversion methods allow to specify the width of the resulting image (keeping the form factor), or scaling the image. For example:

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

It is also possible to use the JavaFX SnapshotParameters to customize the resulting image.

Accessing the named nodes

The SVG nodes which have an id attribute can be accessed through the getNode(id) method in the SVGImage result.

Threading usage

You can use the library 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.

See also


Categories: fromsvg

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