How to Create the path element horizontal line in JavaFX?

JavaObject Oriented ProgrammingProgramming

This is class represents the path element horizontal line. It helps you to draw a horizontal line form the current coordinates to the specified (new) coordinates.

To create a line path element −

  • Instantiate the HLineTo class.

  • Set values to the properties of this class using setter methods or, bypassing them to the constructor.

  • Instantiate the Path class.

  • Get the observable list object of the above-created Path using the getElements() method.

  • Add the above created HLineTo object to the observable list using the add() method.

  • Finally, add the path to the Group object.


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class HLineExample extends Application {
   public void start(Stage stage) {
      //Creating PathElement objects
      MoveTo moveTo = new MoveTo(208, 50);
      LineTo line1 = new LineTo(421, 120);
      //Creating the HLineTo object
      HLineTo hLineTo1 = new HLineTo();
      LineTo line3 = new LineTo(400, 190);
      HLineTo hLineTo2 = new HLineTo();
      //Creating a Path
      Path path = new Path();
      path.getElements().addAll(moveTo, line1, hLineTo1, line3, hLineTo2);
      //Setting other properties
      //Preparing the Stage object
      Group root = new Group(path);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("JavaFX Example");
   public static void main(String args[]){


Published on 13-Apr-2020 12:05:11