Explain the stroke Dash Offset property of 2D shapes in JavaFX


If the stroke used is a dashing pattern. the strokeDashOffset property specifies the offset into the dashing pattern. i.e. the dash phase defines the point in the dashing pattern that will correspond to the beginning of the stroke.

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.scene.shape.Polygon;
import javafx.stage.Stage;
public class StrokeDashOffset extends Application {
   public void start(Stage stage) {
      Line shape1 = new Line(25.0, 50.0, 565.0, 50.0);
      shape1.setStroke(Color.BROWN);
      shape1.setStrokeWidth(10);
      shape1.getStrokeDashArray().addAll(80.0, 70.0, 60.0, 50.0, 40.0);
      shape1.setStrokeDashOffset(5);
      Polygon shape2 = new Polygon(25.0, 150.0, 565.0, 150.0);
      shape2.setStroke(Color.CRIMSON);
      shape2.setStrokeWidth(10.0);
      shape2.getStrokeDashArray().addAll(80.0, 70.0, 60.0, 50.0, 40.0);
      shape2.setStrokeDashOffset(40);
      //Creating a Group object
      Group root = new Group(shape1, shape2);
      //Creating a scene object
      Scene scene = new Scene(root, 595, 200);
      //Setting title to the Stage
      stage.setTitle("Stroke Dash Offset Example");
      //Adding scene to the stage
      stage.setScene(scene);
      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 14-Apr-2020

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements