Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Lu Liu
- * Date: 4/5/2016
- * Course Number: CSC-112
- * Course Name: Intermediate Topics in Java Programming
- * Email: lliu0001@student.stcc.edu
- *
- * Assignment: HW # 13
- * Programe Description:
- * Event Processing Homework Control Polygon program
- */
- import javafx.application.Application;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Polygon;
- import javafx.stage.Stage;
- public class ControlPolygon extends Application {
- PolygonPane polygonPane = new PolygonPane();
- @Override
- // Override the start method in the Application class
- public void start(Stage primaryStage) {
- // Hold two buttons in an HBox
- HBox hBox = new HBox();
- hBox.setSpacing(10);
- hBox.setAlignment(Pos.CENTER);
- Button btEnlarge = new Button("+");
- Button btShrink = new Button("-");
- hBox.getChildren().add(btEnlarge);
- hBox.getChildren().add(btShrink);
- // Create and register the handler
- btEnlarge.setOnAction(new EnlargeHandler());
- btShrink.setOnAction(new ShrinkHandler());
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(polygonPane);
- borderPane.setBottom(hBox);
- BorderPane.setAlignment(hBox, Pos.CENTER);
- // Create a scene and place it in the stage
- Scene scene = new Scene(borderPane, 300, 250);
- primaryStage.setTitle("ControlPolygon"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- }
- class EnlargeHandler implements EventHandler<ActionEvent> {
- @Override
- // Override the handle method
- public void handle(ActionEvent e) {
- polygonPane.enlarge();
- }
- }
- class ShrinkHandler implements EventHandler<ActionEvent> {
- @Override
- // Override the handle method
- public void handle(ActionEvent e) {
- polygonPane.shrink();
- }
- }
- /**
- * The main method is only needed for the IDE with limited JavaFX support.
- * Not needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
- class PolygonPane extends Pane {
- private int n = 6;
- public int getN() {
- return n;
- }
- public void setN(int n) {
- this.n = n;
- this.paint();
- }
- public void paint() {
- // Create a polygon and place polygon to pane
- Polygon polygon = new Polygon();
- polygon.setFill(Color.WHITE);
- polygon.setStroke(Color.BLACK);
- ObservableList<Double> list = polygon.getPoints();
- double centerX = getWidth() / 2, centerY = getHeight() / 2;
- double radius = Math.min(getWidth(), getHeight()) * 0.4;
- // Add points to the polygon list
- for (int i = 0; i < n; i++) {
- list.add(centerX + radius * Math.cos(2 * i * Math.PI / n));
- list.add(centerY - radius * Math.sin(2 * i * Math.PI / n));
- }
- getChildren().clear();
- getChildren().add(polygon);
- }
- public void enlarge() {
- this.setN(n + 1);
- }
- public void shrink() {
- this.setN(this.getN() <= 3 ? 3 : n - 1);
- }
- @Override
- public void setWidth(double width) {
- super.setWidth(width);
- paint();
- }
- @Override
- public void setHeight(double height) {
- super.setHeight(height);
- paint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement