JavaFX - Drawing a Line



Generally, a line is a geometrical structure which is infinitely long on a multi-dimensional plane (Say, an XY plane). This geometrical figure does not have any start and end points; neither does it have measurable dimensions like width, depth etc. Even though a line is a one-dimensional object, it can still exist in two or higher dimensional planes.

However, a segment of this line with both start and end points, called the "line segment" is used in everyday geometry; which is also the figure that we learn how to create using JavaFX, in this chapter. It is drawn as shown in the image below.

Line

Line in JavaFX

In JavaFX, a line, or a line segment, is represented by a class named Line. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a line node in JavaFX.

This class has 4 properties of the double datatype namely −

  • startX − The x coordinate of the start point of the line.

  • startY − The y coordinate of the start point of the line.

  • endX − The x coordinate of the end point of the line.

  • endY − The y coordinate of the end point of the line.

To draw a line, you need to pass values to these properties, either by passing them to the constructor of this class, in the same order, at the time of instantiation.

Steps to Draw a Line in JavaFX

Follow the steps given below to Draw a Line in JavaFX.

Step 1: Creating a Line

You can create a line in JavaFX by instantiating the class named Line which belongs to a package javafx.scene.shape. Instantiate this class as shown below −

//Creating a line object         
Line line = new Line();

Note that the entire code to implement JavaFX graphics, which includes the instantiation of Line class, must be written within the start() method of Application class, as shown below −

public class ClassName extends Application {    
   public void start(Stage primaryStage) throws Exception {
      // Write code here
      Line line = new Line();
   }
} 

Step 2: Setting Properties to the Line

Specify the coordinates to draw the line on an XY plane by setting the properties startX, startY, endX and endY, using their respective setter methods as shown in the following code block.

line.setStartX(100.0); 
line.setStartY(150.0); 
line.setEndX(500.0); 
line.setEndY(150.0); 

Step 3: Adding Line Object to Group

Instantiate the Group class of package javafx.scene, by passing the Line object as a parameter value to its constructor as follows −

Group root = new Group(line);

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 the program which generates a straight line using JavaFX. Save this code in a file with the name DrawingLine.java.

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

public class DrawingLine extends Application{ 
   @Override
   public void start(Stage stage) { 
      //Creating a line object
      Line line = new Line(); 
         
      //Setting the properties to a line 
      line.setStartX(100.0); 
      line.setStartY(150.0); 
      line.setEndX(500.0); 
      line.setEndY(150.0); 
         
      //Creating a Group 
      Group root = new Group(line); 
         
      //Creating a Scene 
      Scene scene = new Scene(root, 600, 300); 
         
      //Setting title to the scene 
      stage.setTitle("Sample application"); 
         
      //Adding the scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of a scene 
      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 DrawingLine.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DrawingLine

Output

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

Drawing Line

Drawing Multiple Lines

You can also draw multiple lines on a single application by grouping all the lines together using addAll() function. An example demonstrating the same is as follows. In here, we are drawing a square using four lines and coloring the background "pink". Save this code in a file with the name DrawingMultipleLines.java.

Example

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line; 
import javafx.stage.Stage;  

public class DrawingMultipleLines extends Application{ 
   @Override
   public void start(Stage stage) { 
      //Creating a line object
      Line line1 = new Line(); 
         
      //Setting the properties to a line 
      line1.setStartX(100.0); 
      line1.setStartY(150.0); 
      line1.setEndX(200.0); 
      line1.setEndY(150.0);
	  
	  // Setting Properties to other lines
	  // without setter methods
	  Line line2 = new Line(200, 150, 200, 250);
	  Line line3 = new Line(200, 250, 100, 250);
	  Line line4 = new Line(100, 250, 100, 150);
         
      //Creating a Group 
      Group root = new Group();
	  
      root.getChildren().addAll(line1,line2,line3,line4);
         
      //Creating a Scene 
      Scene scene = new Scene(root, 400, 400, Color.PINK); 
         
      //Setting title to the scene 
      stage.setTitle("Sample application"); 
         
      //Adding the scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of a scene 
      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 DrawingMultipleLines.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DrawingMultipleLines

Output

The output window of the program above will be obtained as shown below.

Drawing Multiple Lines
Advertisements