1 import javafx.application.Application;
2 import javafx.collections.ObservableList;
3 import javafx.scene.Scene;
4 import javafx.scene.layout.Pane;
5 import javafx.scene.paint.Color;
6 import javafx.stage.Stage;
7 import javafx.scene.shape.Polygon;
8
9 public class ShowPolygon extends Application {
10 @Override
11 public void start(Stage primaryStage) {
12
13 Scene scene = new Scene(new MyPolygon(), 400, 400);
14 primaryStage.setTitle("ShowPolygon");
15 primaryStage.setScene(scene);
16 primaryStage.show();
17 }
18
19
23 public static void main(String[] args) {
24 launch(args);
25 }
26 }
27
28 class MyPolygon extends Pane {
29 private void paint() {
30
31 Polygon polygon = new Polygon();
32 polygon.setFill(Color.WHITE);
33 polygon.setStroke(Color.BLACK);
34 ObservableList<Double> list = polygon.getPoints();
35
36 double centerX = getWidth() / 2, centerY = getHeight() / 2;
37 double radius = Math.min(getWidth(), getHeight()) * 0.4;
38
39
40 for (int i = 0; i < 6; i++) {
41 list.add(centerX + radius * Math.cos(2 * i * Math.PI / 6));
42 list.add(centerY - radius * Math.sin(2 * i * Math.PI / 6));
43 }
44
45 getChildren().clear();
46 getChildren().add(polygon);
47 }
48
49 @Override
50 public void setWidth(double width) {
51 super.setWidth(width);
52 paint();
53 }
54
55 @Override
56 public void setHeight(double height) {
57 super.setHeight(height);
58 paint();
59 }
60 }