如何使用 JavaFX 建立多邊形?
多邊形是使用存在於同一平面中的 n 條線形成的閉合圖形。在 JavaFX 中,多邊形由javafx.scene.shape.Polygon類表示。
要建立多邊形,需要執行以下操作 -
例項化此類。
將線段的起始點和終點傳遞給類,以便繪製多邊形,方法是將它們作為引數傳遞給建構函式,或使用 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.Polygon;
public class DrawingPolygon extends Application {
public void start(Stage stage) {
//Drawing a polygon
Polygon polygon1 = new Polygon();
//Setting the properties of the ellipse
polygon1.getPoints().addAll(new Double[]{
250.0, 50.0, 175.0, 150.0,
175.0, 250.0, 200.0, 250.0,
350.0, 150.0
});
//Setting other properties
polygon1.setFill(Color.DARKCYAN);
polygon1.setStrokeWidth(8.0);
polygon1.setStroke(Color.DARKSLATEGREY);
//Drawing a polygon
Polygon polygon2 = new Polygon();
//Setting the properties of the ellipse
polygon2.getPoints().addAll(new Double[]{
410.0, 160.0, 430.0, 130.0, 470.0, 130.0,
490.0, 160.0, 470.0, 200.0, 430.0, 200.0
});
//Setting other properties
polygon2.setFill(Color.CHOCOLATE);
polygon2.setStrokeWidth(8.0);
polygon2.setStroke(Color.BROWN);
//Setting the Scene
Group root = new Group(polygon1, polygon2);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
stage.setTitle("Drawing Polygon");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP