首页 > 文章列表 > 如何使用JavaFX创建折线?

如何使用JavaFX创建折线?

创建 javafx 折线
196 2023-08-26

折线是使用同一平面中存在的 n 条直线形成的开放图形。即折线与多边形相同,只是它不是封闭的。在 JavaFX 中,折线由 javafx.scene.shape.PolyLine 类表示。

要创建多边形,您需要 -

  • 实例化此类。

  • 将绘制多边形的线段的起点和终点传递给该类,方法是将它们作为参数传递给构造函数或使用 getPoints() 方法作为 -

polygon.getPoints().addAll(new Double[]{ List of XY coordinates separated by commas });
  • 将创建的节点(形状)添加到Group对象中。

示例

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.Polyline;
public class DrawingPolyLine extends Application {
   public void start(Stage stage) {
      //Drawing a polygon
      Polyline poliline = new Polyline();
      //Setting the properties of the ellipse
      poliline.getPoints().addAll(new Double[]{
         150.0, 200.0, 410.0, 200.0, 250.0, 50.0, 250.0, 230.0 });
      //Setting other properties
      poliline.setStrokeWidth(8.0);
      poliline.setStroke(Color.DARKSLATEGREY);
      //Setting the Scene
      Group root = new Group(poliline);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Drawing Polyline");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出

如何使用JavaFX创建折线?