Advertisement
Guest User

JavaFX Draw the American Flag (No Scalability

a guest
Mar 26th, 2017
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. // Antonio Silvestri
  2. // 03/26/17
  3. // HW7 Draw the American Flag (No Scalability)
  4. // CSC-112 Intermediate Java Programming
  5. // silvestri@stcc.edu
  6.  
  7. import javafx.application.Application;
  8. import javafx.collections.ObservableList;
  9. import javafx.scene.Scene;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.paint.Color;
  12. import javafx.scene.shape.Polygon;
  13. import javafx.scene.shape.Rectangle;
  14. import javafx.stage.Stage;
  15.  
  16. public class USFlag extends Application {
  17.     @Override // Override the start method in the Application class
  18.     public void start(Stage primaryStage) {
  19.         Scene scene = new Scene(new USFlagPane());
  20.         primaryStage.setTitle("Make America Great Again!!!");
  21.         primaryStage.setScene(scene);
  22.         primaryStage.setResizable(false);
  23.         primaryStage.show();
  24.     }
  25.  
  26.     /**
  27.      * The main method is only needed for the IDE with limited JavaFX support.
  28.      * Not needed for running from the command line.
  29.      */
  30.     public static void main(String[] args) {
  31.         launch(args);
  32.     }
  33. }
  34.  
  35. // ****************************************************************************
  36.  
  37. class USFlagPane extends Pane {
  38.     private static final double PANEWIDTH = 570;
  39.     private static final double PANEHEIGHT = 310;
  40.     private static final double BLUEWIDTH = 240;
  41.     private static final double BLUEHEIGHT = 160;
  42.  
  43.     public USFlagPane() {
  44.         this.setPrefSize(PANEWIDTH, PANEHEIGHT);
  45.  
  46.         // Draw Red Stripes
  47.         double y = 0;
  48.         for (int i = 0; i < 7; i++) {
  49.             Rectangle r = new Rectangle(10, 10 + y, 550, 160.0 / 7.0);
  50.             r.setFill(Color.RED);
  51.             y += 2 * 160.0 / 7;
  52.             this.getChildren().add(r);
  53.         }
  54.  
  55.         // Draw Sea of Blue
  56.         Rectangle rectangle = new Rectangle(10, 10, BLUEWIDTH, BLUEHEIGHT);
  57.         rectangle.setFill(Color.BLUE);
  58.         this.getChildren().add(rectangle);
  59.  
  60.         // Draw Stars
  61.         int centerX = 30;
  62.         int centerY = 30;
  63.         int radius = 10;
  64.  
  65.         for (int j = 0; j < 5; j++) {
  66.             for (int i = 0; i < 6; i++) {
  67.                 this.getChildren().addAll(getStar(centerX, centerY, radius));
  68.                 centerX += 40;
  69.             }
  70.             centerX = 30;
  71.             centerY += 30;
  72.         }
  73.  
  74.         centerX = 50;
  75.         centerY = 45;
  76.         for (int j = 0; j < 4; j++) {
  77.             for (int i = 0; i < 5; i++) {
  78.                 this.getChildren().addAll(getStar(centerX, centerY, radius));
  79.                 centerX += 40;
  80.             }
  81.             centerX = 50;
  82.             centerY += 30;
  83.         }
  84.     }
  85.  
  86.     private Polygon getStar(double centerX, double centerY, double radius) {
  87.         Polygon polygon = new Polygon();
  88.         polygon.setFill(Color.WHITE);
  89.         polygon.setStroke(Color.BLACK);
  90.  
  91.         double a = 2 * Math.PI / 20;
  92.         double b = 2 * Math.PI / 10;
  93.  
  94.         double h1 = radius * Math.sin(a);
  95.         double r2 = h1 / Math.sin(a + b); // The radius of the inner points
  96.  
  97.         ObservableList<Double> list = polygon.getPoints();
  98.  
  99.         // Add points to the polygon
  100.         double angle = 2 * Math.PI / 20;
  101.         double angle1 = -2 * Math.PI / 20 + 2 * Math.PI / 5;
  102.         for (int i = 0; i < 5; i++) {
  103.             // Add outer point
  104.             list.add(centerX + radius * Math.cos(angle));
  105.             list.add(centerY - radius * Math.sin(angle));
  106.             angle += 2 * Math.PI / 5;
  107.  
  108.             // Add inner point
  109.             list.add(centerX + r2 * Math.cos(angle1));
  110.             list.add(centerY - r2 * Math.sin(angle1));
  111.             angle1 += 2 * Math.PI / 5;
  112.         }
  113.         return polygon;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement