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 // Override the start method in the Application class
 11    public void start(Stage primaryStage) {   
 12      // Create a scene and place it in the stage
 13      Scene scene = new Scene(new MyPolygon(), 400, 400);
 14      primaryStage.setTitle("ShowPolygon"); // Set the stage title
 15      primaryStage.setScene(scene); // Place the scene in the stage
 16      primaryStage.show(); // Display the stage
 17    }
 18    
 19    /**
 20     * The main method is only needed for the IDE with limited
 21     * JavaFX support. Not needed for running from the command line.
 22     */
 23    public static void main(String[] args) {
 24      launch(args);
 25    }
 26  }
 27  
 28  class MyPolygon extends Pane {
 29    private void paint() {
 30      // Create a polygon and place polygon to pane
 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      // Add points to the polygon list
 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(); // Clear pane
 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  }