Home
Categories
Dictionary
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: 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.

These methods can be computationally intensive because what they are doing is loading the SVG file again with a scale parameter. If you don't want to modify the content of the image when scaling it, or you want to have a not computationally intensive method, you can also use the setScaleX(double scaleX) and setScaleY(double scaleY) to apply a scale transformation to the SVGImage. In that case the image content will not be modified.

Examples

   SVGImage img = SVGLoader.load(<my SVG file>);
      
   // a new SVGImage will be created with a scale of 2
   SVGImage newImg = img.scale(2); 
   // the initial SVGImage will be scaled with a factor of 2
   img.scale(2, false);  
      
   // a new SVGImage will be created with a width of 200
   SVGImage newImg = img.scaleTo(200); 
   // the initial SVGImage will have a new width of 200
   img.scaleTo(200, false);        

Producing a snapshot

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

For example:
   SVGImage img = SVGLoader.load(<my SVG file>);
   img.snapshot("png", <my PNG file>));

Producing a JavaFX image

The SVGImage.toImage() method 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:
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.

Examples

   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); 

SVGImage viewport

The SVGImage.getViewport() return the viewport of the SVGImage. The most useful methods of the Viewport are:

Integrating the SVGImage in a layout container

Main Article: SVGImageRegion

The SVGImage is a Group class. As a Group has no size by itself, it can not be integrated efficiently in a Layout containers (such as a BorderPane)[1]
a Group is not directly resizable
.

To overcomme this problem and allow to integrate a SVGImage in a Layout container, you can use the SVGImage.createRegion() which will create a SVGImageRegion wrapping the SVGImage.

As the SVGImageRegion is a Region, it can be resized and be fully integrated in a Layout container.

Notes

  1. ^ a Group is not directly resizable

See also


Categories: fromsvg

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