使用JavaFX画一个五角星
时间: 2024-01-02 21:55:25 浏览: 32
下面是使用JavaFX画一个五角星的代码:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class FivePointedStar extends Application {
@Override
public void start(Stage primaryStage) {
// 画一个五角星
Polygon polygon = new Polygon();
double centerX = 150;
double centerY = 150;
double radius = 100;
double angle = Math.PI / 2;
for (int i = 0; i < 5; i++) {
double x = centerX + radius * Math.cos(angle);
double y = centerY - radius * Math.sin(angle);
polygon.getPoints().addAll(x, y);
angle += 2 * Math.PI / 5;
}
polygon.setFill(Color.YELLOW);
polygon.setStroke(Color.BLACK);
// 将五角星添加到面板上
Pane root = new Pane();
root.getChildren().add(polygon);
// 创建场景并显示
Scene scene = new Scene(root, 300, 300);
primaryStage.setTitle("Five-pointed Star");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
运行该程序可以在窗口中看到一个黄色的五角星。