Advertisement
Guest User

FlagPane

a guest
Mar 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package flag;
  2.  
  3. import javafx.collections.ObservableList;
  4. import javafx.scene.layout.Pane;
  5. import javafx.scene.paint.Color;
  6. import javafx.scene.shape.Polygon;
  7. import javafx.scene.shape.Rectangle;
  8.  
  9. public class FlagPane extends Pane {
  10.     private void paint() {
  11.         this.getChildren().clear();
  12.        
  13.         // Stripes
  14.         for (int i = 0; i < 7; i++) {
  15.            
  16.             Rectangle stripe = new Rectangle();
  17.             stripe.setX(10);
  18.             stripe.setWidth(getWidth() - 20);
  19.             stripe.setHeight((getHeight() - 20) / 13.0);
  20.             stripe.setY((getHeight() - 20) / 13.0 * (2 * i) + 10);
  21.             stripe.setFill(Color.RED);
  22.            
  23.             this.getChildren().add(stripe);
  24.         }
  25.            
  26.         // Blue Box
  27.         Rectangle box = new Rectangle();
  28.         box.setX(10);
  29.         box.setY(10);
  30.         box.setWidth(2 * (getWidth() - 20) / 5);
  31.         box.setHeight(7 * (getHeight() - 21) / 13);
  32.         box.setFill(Color.DARKBLUE);
  33.        
  34.         this.getChildren().add(box);
  35.        
  36.         // Stars
  37.         double heightChange = 10;
  38.         double heightChange2 = 10;
  39.         double widthChange = 10;
  40.  
  41.         for (int i = 0; i < 5; i++){
  42.             for (int j = 0; j < 6; j++)
  43.                 genStar(heightChange, widthChange + (j * (getWidth()*0.0625)));
  44.             heightChange = heightChange + (getHeight() * 0.0950);
  45.         }
  46.         for (int i = 0; i < 4; i++) {
  47.             for (int j = 0; j < 5; j++)
  48.                 genStar2(heightChange2, widthChange + (j * (getWidth()*0.0625)));
  49.             heightChange2 = heightChange2 + (getHeight() * 0.0950);
  50.         }
  51.     }
  52.    
  53.     private void genStar(Double heightChange, Double widthChange){
  54.         double radius = (getHeight() - 20) * 0.025;
  55.         double radius2 = radius / 2.8;
  56.         Polygon star = new Polygon();
  57.         ObservableList<Double> list = star.getPoints();
  58.         for (int j = 0; j < 9; j ++){
  59.             if (j % 2 == 0){
  60.                 list.add(((getWidth() - 20) * 0.0332) + radius * Math.cos(2*j*Math.toRadians(36)) + widthChange);
  61.                 list.add(((getHeight() - 20) * 0.054) - radius * Math.sin(2*j*Math.toRadians(36)) + heightChange);
  62.             }else{
  63.                 list.add(((getWidth() - 20) * 0.0332) + radius2 * Math.cos(2*j*Math.toRadians(36)) + widthChange);
  64.                 list.add(((getHeight() - 20) * 0.054) - radius2 * Math.sin(2*j*Math.toRadians(36)) + heightChange);
  65.             }
  66.         }
  67.         star.setRotate(-18);
  68.         star.setFill(Color.WHITE);
  69.         this.getChildren().add(star);
  70.     }
  71.    
  72.     private void genStar2(Double heightChange, Double widthChange){
  73.         double radius = (getHeight() - 20) * 0.025;
  74.         double radius2 = radius / 2.8;
  75.         Polygon star = new Polygon();
  76.         ObservableList<Double> list = star.getPoints();
  77.         for (int j = 0; j < 9; j ++){
  78.             if (j % 2 == 0){
  79.                 list.add(((getWidth() - 20) * 0.0664) + radius * Math.cos(2*j*Math.toRadians(36)) + widthChange);
  80.                 list.add(((getHeight() - 20) * 0.108) - radius * Math.sin(2*j*Math.toRadians(36)) + heightChange);
  81.             }else{
  82.                 list.add(((getWidth() - 20) * 0.0664) + radius2 * Math.cos(2*j*Math.toRadians(36)) + widthChange);
  83.                 list.add(((getHeight() - 20) * 0.108) - radius2 * Math.sin(2*j*Math.toRadians(36)) + heightChange);
  84.             }
  85.         }
  86.         star.setRotate(-18);
  87.         star.setFill(Color.WHITE);
  88.         this.getChildren().add(star);
  89.     }
  90.    
  91.     @Override
  92.     public void setWidth(double width) {
  93.         super.setWidth(width);
  94.         paint();
  95.     }
  96.    
  97.     @Override
  98.     public void setHeight(double height) {
  99.         super.setHeight(height);
  100.         paint();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement