Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 4/7/2016
- * Csc-112 Java 2
- * Hw#13
- * Create buttons that increase and decrease the amount of sides a regular polygon has
- * Ojnatour0001@student.stcc.edu
- */
- import javafx.application.Application;
- import java.lang.Math;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.stage.Stage;
- import javafx.scene.layout.*;
- import javafx.scene.shape.*;
- import javafx.scene.paint.Color;
- public class Control extends Application {
- BorderPane pane = new BorderPane();
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- pane.setCenter(Poly.getPolygon());
- pane.setBottom(bottom());
- Scene sce = new Scene(pane, 300, 300);
- primaryStage.setTitle("Polygons");
- primaryStage.setScene(sce);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- public Pane bottom() {
- HBox bottom = new HBox(50);
- Button btPlus = new Button("+");
- btPlus.setPrefHeight(15);
- btPlus.setPrefWidth(50);
- Button btMinus = new Button("-");
- btMinus.setPrefHeight(15);
- btMinus.setPrefWidth(50);
- bottom.getChildren().add(btPlus);
- bottom.getChildren().add(btMinus);
- bottom.setPadding(new Insets(0, 75, 10, 75));
- btPlus.setOnAction(e -> pane.setCenter(Poly.enlarge()));
- btMinus.setOnAction(e -> pane.setCenter(Poly.shrink()));
- return bottom;
- }
- }
- class Poly {
- static int sides = 6;
- static StackPane getPolygon() {
- StackPane shape = new StackPane();
- Polygon regular = new Polygon();
- regular.setFill(Color.TRANSPARENT);
- regular.setStroke(Color.BLACK);
- double theta = 2 * Math.PI / sides;
- double[] x = new double[sides];
- double[] y = new double[sides];
- for (int i = 0; i < sides; i++) {
- x[i] = Math.cos(theta * i) * 105;
- y[i] = Math.sin(theta * i) * 105;
- regular.getPoints().addAll((x[i]), (y[i]));
- }
- shape.getChildren().add(regular);
- return shape;
- }
- public static Pane enlarge() {
- sides += 1;
- return getPolygon();
- }
- public static Pane shrink() {
- if (sides <= 3)
- return getPolygon();
- sides -= 1;
- return getPolygon();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement