JavaFX - 2D Shapes SVGPath


Advertisements

SVG (Scalable Vector Graphics) is an XML based language to define vector based graphics.

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 a Class

Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class as follows.

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {      
   }    
}

Step 2: 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 as follows.

//Creating an object of the class SVGPath         
SVGPath svgpath = new SVGPath();

Step 3: 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 4: Creating a Group Object

In the start() method, create a group object by instantiating the class named Group, which belongs to the package javafx.scene.

Pass the SVGPath (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −

Group root = new Group(svgpath);

Step 5: Creating a Scene Object

Create a Scene by instantiating the class named Scene which belongs to the package javafx.scene. To this class pass the Group object (root) created in the previous step.

In addition to the root object, you can also pass two double parameters representing height and width of the screen along with the object of the Group class as follows.

Scene scene = new Scene(group ,600, 300);

Step 6: Setting the Title of the Stage

You can set the title to the stage using the setTitle() method of the Stage class. The primaryStage is a Stage object which is passed to the start method of the scene class as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application as follows.

primaryStage.setTitle("Sample Application");

Step 7: Adding Scene to the Stage

You can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method as follows.

primaryStage.setScene(scene);

Step 8: Displaying the Contents of the Stage

Display the contents of the scene using the method named show() of the Stage class as follows.

primaryStage.show();

Step 9: Launching the Application

Launch the JavaFX application by calling the static method launch() of the Application class from the main x method as follows.

public static void main(String args[]){   
   launch(args);      
} 

Example

Following is a program which generates a shape 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 Sphere"); 
         
      //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 SVGExample.java 
java SVGExample

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
javafx_2d_shapes.htm
Advertisements