如何使用 JavaFX 创建多段线?
多段线是由存在于同一平面中的 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);
}
}输出

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP