JavaFX - Drawing a Polyline



Polyline is defined as a continuous structure that is formed by combining multiple line segments with some vertices. These vertices are addressed as endpoints. Thus, a polyline can be constructed by specifying these endpoints through which these line segments are to be drawn.

It consists of various properties like Points, width, color, start and end caps, type of join, stroke pattern etc. A Polyline is same as a polygon except that a polyline is not closed in the end. Or, continuous line composed of one or more line segments.

In short, we can say a polygon is an open figure formed by coplanar line segments.

Polyline

Polyline in JavaFX

In JavaFX, a Polyline is represented by a class named Polygon. This class belongs to the package javafx.scene.shape..

By instantiating this class, you can create polyline node in JavaFX. You need to pass the x, y coordinates of the points by which the polyline should be defined in the form of a double array.

You can pass the double array as a parameter of the constructor of this class as shown below −

Polyline polyline = new Polyline(doubleArray);

Or, by using the getPoints() method as follows −

polyline.getPoints().addAll(new Double[]{List of XY coordinates separated by commas }); 

Steps to Draw Polyline

To Draw a Polyline in JavaFX, follow the steps given below.

Step 1: Creating a Polyline

You can create a line in JavaFX by instantiating the class named Line which belongs to a package javafx.scene.shape. You can instantiate this class in start() method as follows.

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

Step 2: Setting Properties to the Polyline

Specify a double array holding the XY coordinates of the points of the required polyline (hexagon in this example) separated by commas. You can do this by using the getPoints() method of the Polyline class as shown in the following code block.

//Adding coordinates to the hexagon 
polyline.getPoints().addAll(new Double[]{        
   200.0, 50.0, 
   400.0, 50.0, 
   450.0, 150.0,          
   400.0, 250.0, 
   200.0, 250.0,                   
   150.0, 150.0, 
}); 

Step 3: Adding Polyline Object to Group

In the start() method, instantiate the class named Group, which belongs to the package javafx.scene, by passing the Polyline object, created in the previous step, as a parameter value to its constructor.

Group root = new Group(polyline);

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 polyline using JavaFX. Save this code in a file with the name PolylineExample1.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.Polyline

public class PolylineExample1 extends Application {  
   @Override 
   public void start(Stage stage) {        
      //Creating a polyline 
      Polyline polyline = new Polyline();  
       
      //Adding coordinates to the polygon 
      polyline.getPoints().addAll(new Double[]{        
         200.0, 50.0, 
         400.0, 50.0, 
         450.0, 150.0,          
         400.0, 250.0, 
         200.0, 250.0,                   
         150.0, 150.0, 
      }); 
          
      //Creating a Group object  
      Group root = new Group(polyline); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Polyline"); 
         
      //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 PolylineExample1.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls PolylineExample1

Output

On executing, the above program generates a JavaFX window displaying a polyline as shown below.

Drawing Polyine

Example

In this example, let us try to draw a polyline of 4 vertices. You can save the file name as PolylineExample2.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Polyline; 
import javafx.stage.Stage;  

public class PolylineExample2 extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Polyline 
      Polyline polyline = new Polyline();  
       
      //Adding coordinates to the polygon 
      polyline.getPoints().addAll(new Double[]{ 
         300.0, 50.0, 
         450.0, 150.0, 
         300.0, 250.0, 
         150.0, 150.0, 
      }); 
          
      //Creating a Group object  
      Group root = new Group(polyline); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Polyline"); 
         
      //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 PolylineExample2.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls PolylineExample2

Output

On executing, the above program generates a JavaFX window displaying a 4 vertices Polyline as shown below.

Drawing Polyline
Advertisements