Advertisement
Guest User

Scalable Red Stripes with No Property Binding

a guest
Mar 22nd, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. // Antonio Silvestri
  2. // 03/20/17
  3. // Scalable Red Stripes with No Property Binding
  4.  
  5. package chapter14;
  6.  
  7. import javafx.application.Application;
  8. import javafx.scene.Scene;
  9. import javafx.scene.layout.Pane;
  10. import javafx.scene.paint.Color;
  11. import javafx.scene.shape.Rectangle;
  12. import javafx.stage.Stage;
  13.  
  14. public class Stripes2 extends Application {
  15.     @Override // Override the start method in the Application class
  16.     public void start(Stage primaryStage) {
  17.         final double PANEWIDTH = 570;
  18.         final double PANEHEIGHT = 310;
  19.  
  20.         // Create a scene and place it in the stage
  21.         Scene scene = new Scene(new FlagPane(), PANEWIDTH, PANEHEIGHT);
  22.         primaryStage.setTitle("Red Stripes");
  23.         primaryStage.setScene(scene);
  24.         primaryStage.setResizable(true);
  25.         primaryStage.show();
  26.     }
  27.  
  28.     /**
  29.      * The main method is only needed for the IDE with limited JavaFX support.
  30.      * Not needed for running from the command line.
  31.      */
  32.     public static void main(String[] args) {
  33.         launch(args);
  34.     }
  35. }
  36.  
  37. // Property Binding Technique
  38.  
  39. class FlagPane extends Pane {
  40.     private void paint() {
  41.         this.getChildren().clear();
  42.         for (int i = 0; i < 7; i++) {
  43.             Rectangle r = new Rectangle();
  44.             r.setX(10);
  45.             r.setWidth(getWidth() - 20);
  46.             r.setHeight((getHeight() - 20) / 13.0);
  47.             r.setY((getHeight() - 20) / 13.0 * (2 * i) + 10);
  48.             r.setFill(Color.RED);
  49.             this.getChildren().add(r);
  50.         }
  51.     }
  52.  
  53.     @Override
  54.     public void setWidth(double width) {
  55.         super.setWidth(width);
  56.         paint();
  57.     }
  58.  
  59.     @Override
  60.     public void setHeight(double height) {
  61.         super.setHeight(height);
  62.         paint();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement