Advertisement
rjsantiago0001

Show Polygon

Apr 6th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. // Ricardo Santiago
  2. // 4/6/16
  3. // CSCI-112
  4. // Homework13
  5. // display polygon with buttons to add or subtract sides
  6.  
  7. import javafx.application.Application;
  8. import javafx.collections.ObservableList;
  9. import javafx.geometry.Insets;
  10. import javafx.geometry.Pos;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.TextArea;
  14. import javafx.scene.layout.BorderPane;
  15. import javafx.scene.layout.HBox;
  16. import javafx.scene.paint.Color;
  17. import javafx.scene.shape.Polygon;
  18. import javafx.stage.Stage;
  19.  
  20. public class ShowPolygon extends Application {
  21.  
  22.     static int numOfSides = 6;
  23.     final double WIDTH = 200;
  24.     final double HEIGHT = 200;
  25.     double radius = Math.min(WIDTH, HEIGHT) * 0.4;
  26.     double centerX = WIDTH / 2;
  27.     double centerY = HEIGHT / 2;
  28.     Button btnAdd, btnSub;
  29.     ObservableList<Double> list;
  30.     BorderPane root = new BorderPane();
  31.     Polygon polygon = new Polygon();
  32.     TextArea showNumOfSides = new TextArea("" + numOfSides);
  33.  
  34.     @Override
  35.     public void start(Stage primaryStage) throws Exception {
  36.  
  37.         root.setBottom(getBottomHBox());
  38.  
  39.         list = polygon.getPoints();
  40.         polygon.setFill(Color.WHITE);
  41.         polygon.setStroke(Color.BLACK);
  42.         root.setCenter(polygon);
  43.  
  44.         generatePolygon(numOfSides);
  45.  
  46.         Scene scene = new Scene(root, 200, 200);
  47.         primaryStage.setTitle("Show Polygon");
  48.         primaryStage.setScene(scene);
  49.         primaryStage.show();
  50.  
  51.         btnAdd.setOnMouseClicked(e -> {
  52.             generatePolygon(numOfSides++);
  53.         });
  54.  
  55.         btnSub.setOnMouseClicked(e -> {
  56.             generatePolygon(numOfSides--);
  57.         });
  58.     }
  59.  
  60.     public void generatePolygon(int sides) {
  61.         list.clear();
  62.         root.setCenter(polygon);
  63.  
  64.         for (int i = 0; i < numOfSides; i++) {
  65.             list.add(centerX + radius * Math.cos(2 * i * Math.PI / numOfSides));
  66.             list.add(centerY + radius * Math.sin(2 * i * Math.PI / numOfSides));
  67.         }
  68.  
  69.     }
  70.  
  71.     public int getNumOfSides() {
  72.         return numOfSides;
  73.     }
  74.  
  75.     private HBox getBottomHBox() {
  76.         HBox bottomHBox = new HBox();
  77.         bottomHBox.setAlignment(Pos.CENTER);
  78.         bottomHBox.setSpacing(10.0);
  79.         root.setPadding(new Insets(10));
  80.         btnAdd = new Button("+");
  81.         btnSub = new Button("-");
  82.         bottomHBox.getChildren().addAll(btnAdd, btnSub);
  83.  
  84.         return bottomHBox;
  85.  
  86.     }
  87.  
  88.     public static void main(String[] args) {
  89.         Application.launch(args);
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement