JavaFX Tutorial - JavaFX Line

In order to render graphics on the JavaFX scene, we need basic shapes and colors.

The Node class is a fundamental base class for all JavaFX scene graph nodes. It provides the ability to transform, translate, and apply effects to any node.

The javafx.scene.shape.Shape class is a descendant of the Node class.

All older JavaFX 2.x Builder classes are deprecated in JavaFX 8.

JavaFX Lines

When drawn on the JavaFX scene graph, lines are rendered using the screen coordinate space (system).

The screen coordinate system places (0, 0) at the upper-left corner.

The x coordinate moves a point along the x axis. The y coordinate's value increases when moving a point from top to bottom.

The following image illustrates the screen coordinate system on the right.


In JavaFX, the scene graph objects, such as lines, circles, and rectangles are derived classes of the Shape class.

All shape objects can perform geometric operations between two shaped areas, such as subtract, intersect and union.

To draw lines in JavaFX, we will use the javafx.scene.shape.Line class.

To create a Line object, we need to specify a start (x, y) coordinate and an end coordinate.

There are two ways to set the start and end points when creating Line nodes.

The first method uses a constructor with the parameters startX, startY, endX, and endY. The data types of all the parameters are double.

The following code creates a line with a start point (100, 10) and end point (10, 110) using constructor.

Line line = new Line(100, 10,   10,   110);

The second method to create a line node is to instantiate a Line class using the empty constructor and then setting each attribute using setter methods.

The following code shows how to create a line object and set a line's start and end points using setter methods.

Line line = new Line(); 

Line nodes drawn on the scene graph default to a stroke width of 1.0 and a stroke color of black.

All shapes have a stroke color of null, which means no color except Line, Polyline, and Path nodes.

To create different kinds of lines we can set properties inherited from the parent javafx.scene.shape.Shape class.

The following table shows the properties we can set on a line.

To retrieve or modify each property, you will use its appropriate getter and setter methods.

PropertyData Type / Description
A color to fill inside a shape.
True to turn on anti-aliasing, false to turn off anti-aliasing.
Set the distance into the dashed pattern.
Set the cap style on the end of a line or path. There are three styles:
  • StrokeLineCap.BUTT
  • StrokeLineCap.ROUND
  • StrokeLineCap.SQUARE
Set the decoration when lines meet. There are three types:
  • StrokeLineJoin.MITER
  • StrokeLineJoin.BEVEL
  • StrokeLineJoin.ROUND
Set the limit of a miter joint along with the miter join decoration StrokeLineJoin.MITER.
Set color for the shape's line stroke.
Set where to draw the stroke around the boundary of a Shape node. There are three types:
  • StrokeType.CENTERED
  • StrokeType.INSIDE
  • StrokeType.OUTSIDE
Set a line's width.


The following code creates a Line object and uses setter method to set the start and end coordinates.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
/*from   w w w . ja  v a  2 s . co  m*/
public class Main extends Application {

    public void start(Stage stage) {
        VBox box = new VBox();
        final Scene scene = new Scene(box,300, 250);
        Line line = new Line();

    public static void main(String[] args) {

The code above generates the following result.


Example 2

The following code sets more line properties including the stroke color, stroke width, and line cap.

After that it also set the dash style for the line.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;
//from  w  w w.j  a va2s . c  om
public class Main extends Application {
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Drawing Lines");

    Group root = new Group();
    Scene scene = new Scene(root, 300, 150, Color.GRAY);

    Line redLine = new Line(10, 10, 200, 10);


    redLine.getStrokeDashArray().addAll(15d, 5d, 15d, 15d, 20d);


  public static void main(String[] args) {

The code above generates the following result.