luliu

HW #13Event Processing Homework Control Polygon program

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