JavaFX - Drawing an SVGPath



SVG (Scalable Vector Graphics) is an XML based language to define vector based graphics. The <path> element in the SVG library is the most powerful while drawing basic shapes. Using paths, you can draw lines, curves, arcs, and also various complex shapes including them.

Even though a path is similar to the polyline element while creating complex shapes, the scale of complex shapes drawn using a polyline element is not larger than shapes drawn using path element.

A path in SVG is defined by only one parameter. This parameter holds series of commands, like line, curve or arc commands. And each of these commands are instantiated using a single letter; for example, the letter 'M' calls the "Move To" command, the letter 'L' calls the "line" command and 'C' calls "Curve" command. And these letters can either be specified as either a lowercase or an uppercase letter. The lowercase letter specifies relative coordinates, while the uppercase letter specifies absolute coordinates.

The same concept of SVGPath is adopted by JavaFX, in order to create objects.

SVG Path in JavaFX

In JavaFX we can construct images by parsing SVG paths. Such shapes are represented by the class named SVGPath. This class belongs to the package javafx.scene.shape.

By instantiating this class, you can create a node which is created by parsing an SVG path in JavaFX.

This class has a property named content of String datatype. This represents the SVG Path encoded string, from which the image should be drawn.

To draw a shape by parsing an SVG path, you need to pass values to this property, using the method named setContent() of this class as follows −

setContent(value);

Steps to Draw SVGPath

To Draw a shape by parsing an SVGPath in JavaFX, follow the steps given below.

Step 1: Creating an Object of the SVGPath Class

You can create a required shape in JavaFX by parsing an SVGPath. To do so, instantiate the class named SVGPath which belongs to a package javafx.scene.shape. You can instantiate this class inside the start() method as follows.

public class ClassName extends Application {     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the class SVGPath         
      SVGPath svgpath = new SVGPath();   
   }
}

Step 2: Setting the SVGPath

Set the content for the SVG object using the method setContent(). To this method, you need to pass the SVGPath. Using which, a shape should be drawn in the form of a string as shown in the following code block.

String path = "M 100 100 L 300 100 L 200 300 z";
//Setting the SVGPath in the form of string 
svgPath.setContent(path);

Step 3: Adding SVGPath to Group

In the start() method, instantiate the Group class by passing the SVGPath object as a parameter value to its constructor −

Group root = new Group(svgpath);

Step 4: Launching Application

Once the 2D object is created, follow the given steps below to launch the application properly −

  • Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.

  • Then, set the title to the stage using the setTitle() method of the Stage class.

  • Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  • Display the contents of the scene using the method named show().

  • Lastly, the application is launched with the help of the launch() method.

Example

Following is a program which generates a 2D shape constructed with lines by parsing SVG path using JavaFX. Save this code in a file with the name SVGExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.SVGPath; 
import javafx.stage.Stage; 
        
public class SVGExample extends Application {  
   @Override 
   public void start(Stage stage) { 
      //Creating a SVGPath object 
      SVGPath svgPath = new SVGPath();       
       
      String path = "M 100 100 L 300 100 L 200 300 z";  
      
      //Setting the SVGPath in the form of string 
      svgPath.setContent(path);             
         
      //Creating a Group object  
      Group root = new Group(svgPath); 
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);
      
      //Setting title to the Stage
      stage.setTitle("Drawing a Triangle"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}    

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls SVGExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SVGExample

Output

On executing, the above program generates a JavaFX window displaying a triangle, which is drawn by parsing the SVG path as shown below.

Drawing Sphere

Example

You can also construct complex shapes from curves and arcs using an SVG path. Following example creates a cubic curve using an SVG Path. Save this code in a file with the name SVGCurveExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.SVGPath; 
import javafx.stage.Stage; 
        
public class SVGCurveExample extends Application {  
   @Override 
   public void start(Stage stage) { 
      //Creating a SVGPath object 
      SVGPath svgPath = new SVGPath();       
       
      String path = "M 70 110 C 70 180, 210 180, 210 110";
      
      //Setting the SVGPath in the form of string 
      svgPath.setContent(path);

      // Setting the stroke and fill of the path
      svgPath.setStroke(Color.BLACK);
      svgPath.setFill(Color.ORANGE);	  
         
      //Creating a Group object  
      Group root = new Group(svgPath); 
               
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);
      
      //Setting title to the Stage
      stage.setTitle("Drawing a Bezier Curve"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls SVGCurveExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SVGCurveExample

Output

On executing, the above program generates a JavaFX window displaying a triangle, which is drawn by parsing the SVG path as shown below.

Drawing Sphere
Advertisements