解释 JavaFX 中 2D 形状的描边 Dash Offset 属性

javaobject oriented programmingprogramming更新于 2025/10/12 3:24:17

如果使用的描边是虚线图案,则 strokeDashOffset 属性指定虚线图案的偏移量。即虚线相位定义虚线图案中与描边起点相对应的点。

示例

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);
      //创建一个 Group 对象
      Group root = new Group(shape1, shape2);
      //创建一个 scene 对象
      Scene scene = new Scene(root, 595, 200);
      //设置 Stage 标题
      stage.setTitle("Stroke Dash Offset Example");
      //将 scene 添加到 stage
      stage.setScene(scene);
      //显示 stage 的内容
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出


相关文章


有用资源