# What are various path elements in JavaFX?

JavaObject Oriented ProgrammingProgramming

The javafx.scene.shape package provides classes using which you can draw various 2D shapes, but these are just primitive shapes like line, circle, polygon, and ellipse, etc… Therefore, if you want to draw complex custom shapes you need to use the Path class.

## The Path Class

The Path class represents the geometrical outline of a shape using this class you can draw your custom path.

To draw a custom path JavaFX provides various path elements and, all these are available as classes in the javafx.scene.shape package.

• LineTo − This is class represents the path element line. It helps you to draw a straight line from the current coordinates to the specified (new) coordinates.

• HlineTo − 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.

• VLineTo − This is class represents the path element vertical line. It helps you to draw a vertical line from the current coordinates to the specified (new) coordinates.

• QuadCurveTo − This is class represents the path element quadratic curve. It helps you to draw a quadratic curve form the current coordinates to the specified (new) coordinates.

• CubicCurveTo − This is class represents the path element cubic curve. It helps you to draw a cubic curve form the current coordinates to the specified (new) coordinates.

• ArcTo − This is class represents the path element arc. It helps you to draw an arc from the current coordinates to the specified (new) coordinates.

• MoveTo − Using this class you can move the path from the current coordinates to new coordinates.

## Creating a path using the path elements

The Path class contains an observable list which holds the path elements of the current path. Therefore to draw a path −

• Instantiate the required PathElement classes.

• Set the properties of each path using the setter methods or, pass them as arguments to the constructor.

• Instantiate the Path class.

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

• Add all the path element objects to the observable list in the desired order using add() or, addAll() methods.

• Finally, add the path to the Group object.

Note − You can also pass the path elements to the constructor of the Path class.

## Example

Following JavaFX example creates a path using the LineTo path element −

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.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class PathElementsExample extends Application {
public void start(Stage stage) {
//Drawing the shape
MoveTo moveTo = new MoveTo(208, 71);
LineTo line1 = new LineTo(421, 161);
LineTo line2 = new LineTo(226,232);
LineTo line3 = new LineTo(332,52);
LineTo line4 = new LineTo(369, 250);
LineTo line5 = new LineTo(208, 71);
//Creating a Path
Path path = new Path(moveTo, line1, line2, line3, line4, line5);
path.setFill(Color.DARKCYAN);
path.setStrokeWidth(8.0);
path.setStroke(Color.DARKSLATEGREY);
//Preparing the Stage object
Group root = new Group(path);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
stage.setTitle("Drawing an arc through a path");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

## Output

Updated on 13-Apr-2020 12:01:51