Omar_Natour

Natour, O. 4/7/16 Csc-112 Control Polygon

Apr 7th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 4/7/2016
  4.  * Csc-112 Java 2
  5.  * Hw#13
  6.  * Create buttons that increase and decrease the amount of sides a regular polygon has
  7.  */
  8.  
  9. import javafx.application.Application;
  10. import java.lang.Math;
  11. import javafx.geometry.Insets;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.stage.Stage;
  15. import javafx.scene.layout.*;
  16. import javafx.scene.shape.*;
  17. import javafx.scene.paint.Color;
  18.  
  19. public class Control extends Application {
  20.    BorderPane pane = new BorderPane();
  21.     public static void main(String[] args) {
  22.         launch(args);
  23.     }
  24.  
  25.     @Override
  26.     public void start(Stage primaryStage) {
  27.  
  28.         pane.setCenter(Poly.getPolygon());
  29.         pane.setBottom(bottom());
  30.  
  31.         Scene sce = new Scene(pane, 300, 300);
  32.  
  33.         primaryStage.setTitle("Polygons");
  34.         primaryStage.setScene(sce);
  35.         primaryStage.show();
  36.         primaryStage.setResizable(false);
  37.     }
  38.  
  39.     public Pane bottom() {
  40.  
  41.         HBox bottom = new HBox(50);
  42.  
  43.         Button btPlus = new Button("+");
  44.         btPlus.setPrefHeight(15);
  45.         btPlus.setPrefWidth(50);
  46.  
  47.         Button btMinus = new Button("-");
  48.         btMinus.setPrefHeight(15);
  49.         btMinus.setPrefWidth(50);
  50.  
  51.         bottom.getChildren().add(btPlus);
  52.         bottom.getChildren().add(btMinus);
  53.         bottom.setPadding(new Insets(0, 75, 10, 75));
  54.  
  55.         btPlus.setOnAction(e -> pane.setCenter(Poly.enlarge()));
  56.         btMinus.setOnAction(e -> pane.setCenter(Poly.shrink()));
  57.  
  58.         return bottom;
  59.     }
  60. }
  61.  
  62. class Poly {
  63.      static int sides = 6;
  64.       static StackPane getPolygon() {
  65.  
  66.         StackPane shape = new StackPane();
  67.  
  68.         Polygon regular = new Polygon();
  69.         regular.setFill(Color.TRANSPARENT);
  70.         regular.setStroke(Color.BLACK);
  71.  
  72.         double theta = 2 * Math.PI / sides;
  73.         double[] x = new double[sides];
  74.         double[] y = new double[sides];
  75.  
  76.         for (int i = 0; i < sides; i++) {
  77.             x[i] = Math.cos(theta * i) * 105;
  78.             y[i] = Math.sin(theta * i) * 105;
  79.  
  80.             regular.getPoints().addAll((x[i]), (y[i]));
  81.         }
  82.         shape.getChildren().add(regular);
  83.  
  84.         return shape;
  85.     }
  86.  
  87.     public static  Pane enlarge() {
  88.         sides += 1;
  89.         return getPolygon();
  90.     }
  91.  
  92.     public static  Pane shrink() {
  93.         if (sides <= 3)
  94.             return getPolygon();
  95.         sides -= 1;
  96.         return getPolygon();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment