Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Antonio Silvestri
- // 03/26/17
- // HW7 Draw the American Flag (No Scalability)
- // CSC-112 Intermediate Java Programming
- // silvestri@stcc.edu
- import javafx.application.Application;
- import javafx.collections.ObservableList;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Polygon;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- public class USFlag extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- Scene scene = new Scene(new USFlagPane());
- primaryStage.setTitle("Make America Great Again!!!");
- primaryStage.setScene(scene);
- primaryStage.setResizable(false);
- primaryStage.show();
- }
- /**
- * 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 USFlagPane extends Pane {
- private static final double PANEWIDTH = 570;
- private static final double PANEHEIGHT = 310;
- private static final double BLUEWIDTH = 240;
- private static final double BLUEHEIGHT = 160;
- public USFlagPane() {
- this.setPrefSize(PANEWIDTH, PANEHEIGHT);
- // Draw Red Stripes
- double y = 0;
- for (int i = 0; i < 7; i++) {
- Rectangle r = new Rectangle(10, 10 + y, 550, 160.0 / 7.0);
- r.setFill(Color.RED);
- y += 2 * 160.0 / 7;
- this.getChildren().add(r);
- }
- // Draw Sea of Blue
- Rectangle rectangle = new Rectangle(10, 10, BLUEWIDTH, BLUEHEIGHT);
- rectangle.setFill(Color.BLUE);
- this.getChildren().add(rectangle);
- // Draw Stars
- int centerX = 30;
- int centerY = 30;
- int radius = 10;
- for (int j = 0; j < 5; j++) {
- for (int i = 0; i < 6; i++) {
- this.getChildren().addAll(getStar(centerX, centerY, radius));
- centerX += 40;
- }
- centerX = 30;
- centerY += 30;
- }
- centerX = 50;
- centerY = 45;
- for (int j = 0; j < 4; j++) {
- for (int i = 0; i < 5; i++) {
- this.getChildren().addAll(getStar(centerX, centerY, radius));
- centerX += 40;
- }
- centerX = 50;
- centerY += 30;
- }
- }
- private Polygon getStar(double centerX, double centerY, double radius) {
- Polygon polygon = new Polygon();
- polygon.setFill(Color.WHITE);
- polygon.setStroke(Color.BLACK);
- double a = 2 * Math.PI / 20;
- double b = 2 * Math.PI / 10;
- double h1 = radius * Math.sin(a);
- double r2 = h1 / Math.sin(a + b); // The radius of the inner points
- ObservableList<Double> list = polygon.getPoints();
- // Add points to the polygon
- double angle = 2 * Math.PI / 20;
- double angle1 = -2 * Math.PI / 20 + 2 * Math.PI / 5;
- for (int i = 0; i < 5; i++) {
- // Add outer point
- list.add(centerX + radius * Math.cos(angle));
- list.add(centerY - radius * Math.sin(angle));
- angle += 2 * Math.PI / 5;
- // Add inner point
- list.add(centerX + r2 * Math.cos(angle1));
- list.add(centerY - r2 * Math.sin(angle1));
- angle1 += 2 * Math.PI / 5;
- }
- return polygon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement