Source of SwedishFlagApplication.java


  1: import javafx.application.Application;
  2: import javafx.stage.Stage;
  3: import javafx.scene.Scene;
  4: import javafx.scene.layout.Pane;
  5: import javafx.scene.canvas.Canvas;
  6: import javafx.scene.canvas.GraphicsContext;
  7: import javafx.scene.paint.Color;

  9: /**
 10:  * A JavaFX Application to draw the Swedish flag.
 11:  *
 12:  * @author Mark Young (A00000000)
 13:  */
 14: public class SwedishFlagApplication extends Application {

 16:     @Override
 17:     public void start(Stage applicationStage) {
 18:         Pane pane = makePane();
 19:         Scene scene = new Scene(pane);          // A scene containing the pane
 20:         applicationStage.setTitle("Swedish Flag");  // Set window's title
 21:         applicationStage.setScene(scene);           // Set window's scene
 22:         applicationStage.show();                    // Display window
 23:     }

 25:     /**
 26:      * Create a Pane containing a picture of the Swedish flag.
 27:      *
 28:      * @return new Pane with flag image
 29:      */
 30:     private Pane makePane() {
 31:         Pane pane = new Pane();                 // An empty pane    
 32:         Canvas canvas = makeFlag();
 33:         
 34:         // put it all together, give it a title, and show it
 35:         pane.getChildren().add(canvas);             // Add canvas to pane 
 36:         return pane;
 37:     }

 39:     // flag constants
 40:     private static final int WIDTH = 900;
 41:     private static final int HEIGHT = WIDTH * 5 / 8;
 42:     
 43:     private static final int[] WIDTHS = {5, 2, 9};
 44:     private static final int[] HEIGHTS = {4, 2, 4};

 46:     /**
 47:      * Create a picture of the Swedish flag.
 48:      *
 49:      * @return a new Canvas with flag image
 50:      */
 51:     private Canvas makeFlag() {
 52:         Canvas result = new Canvas(WIDTH, HEIGHT);
 53:         double totalWidth, leftEdge, width, totalHeight, topEdge, height;
 54:         GraphicsContext drawing = result.getGraphicsContext2D();
 55:         Color blue = Color.rgb(0, 106, 167);
 56:         Color yellow = Color.rgb(254, 204, 0);
 57:         
 58:         // calculate the edges of the cross
 59:         totalWidth = WIDTHS[0] + WIDTHS[1] + WIDTHS[2];
 60:         leftEdge = (double)WIDTHS[0] / totalWidth;
 61:         width = (double)WIDTHS[1] / totalWidth;
 62:         totalHeight = HEIGHTS[0] + HEIGHTS[1] + HEIGHTS[2];
 63:         topEdge = (double)HEIGHTS[0] / totalHeight;
 64:         height = (double)HEIGHTS[1] / totalHeight;
 65:         
 66:         // Get the canvas' graphics context to draw
 67:         
 68:         // draw the background -- blue
 69:         drawing.setFill(blue);
 70:         drawing.fillRect(0, 0, WIDTH, HEIGHT);
 71:         
 72:         // draw vertical stripe - yellow
 73:         drawing.setFill(yellow);
 74:         drawing.fillRect(WIDTH * leftEdge, 0, WIDTH * width, HEIGHT);
 75:         
 76:         // draw the horizontal stripe -- yellow
 77:         drawing.fillRect(0, HEIGHT * topEdge, WIDTH, HEIGHT * height);
 78:         
 79:         return result;
 80:     }

 82:     public static void main(String[] args) {
 83:         launch(args); // Launch application
 84:     }
 85: }