Advertisement
Omar_Natour

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

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