mydomain
No ADS
No ADS

JavaFX Line Tutorial with Examples

  1. JavaFX Line
  2. Line Example
  3. The properties of Line

1. JavaFX Line

In JavaFX, The Line class is used to draw a straight line. The same as class Rectangle, Circle, ...they all are extended from Shape class.

2. Line Example

LineDemo.java
package org.o7planning.javafx.line;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class LineDemo extends Application {

    @Override
    public void start(Stage stage) {

        // A line in Ox Axis
        Line oxLine1 = new Line(0, 0, 400, 0);

        // Stroke Width
        oxLine1.setStrokeWidth(5);
        oxLine1.setStroke(Color.BLUE);

        // A line in Oy Axis
        Line oyLine = new Line(0, 0, 0, 200);

        // Stroke Width
        oyLine.setStrokeWidth(5);
        oyLine.setStroke(Color.BLUEVIOLET);

        // An other Line
        Line line = new Line();
        line.setStartX(100.0f);
        line.setStartY(200.0f);
        line.setEndX(300.0f);
        line.setEndY(70.0f);
        line.setStrokeWidth(10);
        line.setStroke(Color.PINK);

        AnchorPane root = new AnchorPane();
        root.setPadding(new Insets(15));

        final Scene scene = new Scene(root, 400, 250);

        scene.setFill(null);
        root.getChildren().addAll(oyLine, oxLine1, line);

        stage.setTitle("JavaFX Line (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

3. The properties of Line

No ADS
smooth
Value true if you want to turn on anti-aliasing, false to turn off anti-aliasing.
strokeWidth
Set a line's width.
stroke
Using setStroke method to setting color for Line.
// Set Line color.

line.setStroke(Color.RED);
Stroke Dash Array
LineStrokeDashArrayDemo.java
package org.o7planning.javafx.line;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class LineStrokeDashArrayDemo extends Application {

    @Override
    public void start(Stage stage) {

        Line line1 = new Line(20, 40, 270, 40);
        line1.getStrokeDashArray().addAll(25.0, 20.0, 5.0, 20.0);

        Line line2 = new Line(20, 60, 270, 60);
        line2.getStrokeDashArray().addAll(50.0, 40.0);

        Line line3 = new Line(20, 80, 270, 80);
        line3.getStrokeDashArray().addAll(25.0, 10.0);

        Line line4 = new Line(20, 100, 270, 100);
        line4.getStrokeDashArray().addAll(2.0);

        Line line5 = new Line(20, 120, 270, 120);
        line5.getStrokeDashArray().addAll(2.0, 21.0);

        AnchorPane root = new AnchorPane();
        root.setPadding(new Insets(5));

        final Scene scene = new Scene(root, 400, 250);

        root.getChildren().addAll(line1, line2, line3, line4, line5);

        stage.setTitle("JavaFX Line (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
strokeDashOffset
In addition to the stroke-dasharray property, there is a possiblity to translate the beginning of the dash. This is controlled by the stroke-dashoffset property.
strokeLineCap
strokeLineCap - specifies the shape to be used at the end of open subpaths when they are stroked. There are three styles:
  • StrokeLineCap.BUTT
  • StrokeLineCap.ROUND
  • StrokeLineCap.SQUARE
strokeLineJoin
stroke-linejoin: specifies the shape to be used at the corners of paths or basic shapes when they are stroked. There are three types:
  • StrokeLineJoin.MITER
  • StrokeLineJoin.BEVEL
  • StrokeLineJoin.ROUND
strokeMiterLimit
strokeMiterLimit - Is a value in range [0,1] that is applied in the case of strokeLineJoin = StoreLineJoin.MITER in order to limit the ratio between miterLengh and strokeWidth
The angle between two straight lines is small (acute angle). The ratio between miterLength and storeWidth can be greater than storeMiterLimit. In this case, the shape of intersection points between two straight lines will be changed from "Miter-Join" to "Bevel-Join".
No ADS

JavaFX Tutorials

Show More
No ADS