Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Antonio Silvestri
- // 03/20/17
- // Scalable Red Stripes with No Property Binding
- package chapter14;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- public class Stripes2 extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- final double PANEWIDTH = 570;
- final double PANEHEIGHT = 310;
- // Create a scene and place it in the stage
- Scene scene = new Scene(new FlagPane(), PANEWIDTH, PANEHEIGHT);
- primaryStage.setTitle("Red Stripes");
- primaryStage.setScene(scene);
- primaryStage.setResizable(true);
- 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);
- }
- }
- // Property Binding Technique
- class FlagPane extends Pane {
- private void paint() {
- this.getChildren().clear();
- for (int i = 0; i < 7; i++) {
- Rectangle r = new Rectangle();
- r.setX(10);
- r.setWidth(getWidth() - 20);
- r.setHeight((getHeight() - 20) / 13.0);
- r.setY((getHeight() - 20) / 13.0 * (2 * i) + 10);
- r.setFill(Color.RED);
- this.getChildren().add(r);
- }
- }
- @Override
- public void setWidth(double width) {
- super.setWidth(width);
- paint();
- }
- @Override
- public void setHeight(double height) {
- super.setHeight(height);
- paint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement