Advertisement
luliu

HW #13Event Processing Homework Control Polygon program

Apr 5th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 4/5/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  * Email: lliu0001@student.stcc.edu
  7.  *
  8.  * Assignment: HW # 13
  9.  * Programe Description:
  10.  * Event Processing Homework Control Polygon program
  11.  */
  12.  
  13. import javafx.application.Application;
  14. import javafx.collections.ObservableList;
  15. import javafx.event.ActionEvent;
  16. import javafx.event.EventHandler;
  17. import javafx.geometry.Pos;
  18. import javafx.scene.Scene;
  19. import javafx.scene.control.Button;
  20. import javafx.scene.layout.StackPane;
  21. import javafx.scene.layout.HBox;
  22. import javafx.scene.layout.Pane;
  23. import javafx.scene.layout.BorderPane;
  24. import javafx.scene.paint.Color;
  25. import javafx.scene.shape.Circle;
  26. import javafx.scene.shape.Polygon;
  27. import javafx.stage.Stage;
  28.  
  29. public class ControlPolygon extends Application {
  30.  
  31.     PolygonPane polygonPane = new PolygonPane();
  32.  
  33.     @Override
  34.     // Override the start method in the Application class
  35.     public void start(Stage primaryStage) {
  36.         // Hold two buttons in an HBox
  37.         HBox hBox = new HBox();
  38.         hBox.setSpacing(10);
  39.         hBox.setAlignment(Pos.CENTER);
  40.         Button btEnlarge = new Button("+");
  41.         Button btShrink = new Button("-");
  42.         hBox.getChildren().add(btEnlarge);
  43.         hBox.getChildren().add(btShrink);
  44.  
  45.         // Create and register the handler
  46.         btEnlarge.setOnAction(new EnlargeHandler());
  47.         btShrink.setOnAction(new ShrinkHandler());
  48.  
  49.         BorderPane borderPane = new BorderPane();
  50.         borderPane.setCenter(polygonPane);
  51.         borderPane.setBottom(hBox);
  52.         BorderPane.setAlignment(hBox, Pos.CENTER);
  53.  
  54.         // Create a scene and place it in the stage
  55.         Scene scene = new Scene(borderPane, 300, 250);
  56.         primaryStage.setTitle("ControlPolygon"); // Set the stage title
  57.         primaryStage.setScene(scene); // Place the scene in the stage
  58.         primaryStage.show(); // Display the stage
  59.     }
  60.  
  61.     class EnlargeHandler implements EventHandler<ActionEvent> {
  62.         @Override
  63.         // Override the handle method
  64.         public void handle(ActionEvent e) {
  65.             polygonPane.enlarge();
  66.         }
  67.     }
  68.  
  69.     class ShrinkHandler implements EventHandler<ActionEvent> {
  70.         @Override
  71.         // Override the handle method
  72.         public void handle(ActionEvent e) {
  73.             polygonPane.shrink();
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * The main method is only needed for the IDE with limited JavaFX support.
  79.      * Not needed for running from the command line.
  80.      */
  81.     public static void main(String[] args) {
  82.         launch(args);
  83.     }
  84. }
  85.  
  86. class PolygonPane extends Pane {
  87.     private int n = 6;
  88.  
  89.     public int getN() {
  90.         return n;
  91.     }
  92.  
  93.     public void setN(int n) {
  94.         this.n = n;
  95.         this.paint();
  96.     }
  97.  
  98.     public void paint() {
  99.         // Create a polygon and place polygon to pane
  100.         Polygon polygon = new Polygon();
  101.         polygon.setFill(Color.WHITE);
  102.         polygon.setStroke(Color.BLACK);
  103.         ObservableList<Double> list = polygon.getPoints();
  104.  
  105.         double centerX = getWidth() / 2, centerY = getHeight() / 2;
  106.         double radius = Math.min(getWidth(), getHeight()) * 0.4;
  107.  
  108.         // Add points to the polygon list
  109.         for (int i = 0; i < n; i++) {
  110.             list.add(centerX + radius * Math.cos(2 * i * Math.PI / n));
  111.             list.add(centerY - radius * Math.sin(2 * i * Math.PI / n));
  112.         }
  113.         getChildren().clear();
  114.         getChildren().add(polygon);
  115.     }
  116.  
  117.     public void enlarge() {
  118.         this.setN(n + 1);
  119.     }
  120.  
  121.     public void shrink() {
  122.         this.setN(this.getN() <= 3 ? 3 : n - 1);
  123.     }
  124.  
  125.     @Override
  126.     public void setWidth(double width) {
  127.         super.setWidth(width);
  128.         paint();
  129.     }
  130.  
  131.     @Override
  132.     public void setHeight(double height) {
  133.         super.setHeight(height);
  134.         paint();
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement