Examine the incomplete application code at Rectangle Bound C
Examine the incomplete application code at Rectangle Bound Code below This application should display a rectangle. As the window is resized, the rectangle should change size as well.
Write the code that will set the fill color of rect to light sky blue (one of the predefined colors in JavaFX). This will replace comment *A*.
Write the code that will set the outline color of rect to deep sky blue (one of the predefined colors in JavaFX). This will replace comment *B*.
Write the code that will set the outline width of rect to 5. This will replace comment *C*.
Write the code that will bind the x property of the rectangle rect to one fourth the width property of the pane p. This will replace comment *D*.
Write the code that will bind the y property of the rectangle rect to one fourth the height property of the pane p. This will replace comment *E*.
Write the code that will bind the width property of the rectangle rect to one half the width property of the pane p. This will replace comment *F*.
Write the code that will bind the height property of the rectangle ret to one half the height property of the pane p. This will replace comment *G*.
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;
/**
* Created by Ben on 3/27/2015.
*/
public class RectangleBound extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Pane p = new Pane();
Rectangle rect = new Rectangle();
// *A* set the fill color of rect to light sky blue
// *B* set the outline color of rect to deep sky blue
// *C* set the width of the outline of rect to 5
p.getChildren().add(rect);
// *D* bind the x property of rect to one fourth the width of the pane p
// *E* bind the y property of rect to one fourth the height of the pane p
// *F* bind the width property of rect to one half the width of the pane p
// *G* bind the height property of rect to one half the width of the pane p
Scene sc = new Scene(p, 300, 300);
primaryStage.setScene(sc);
primaryStage.setTitle(\"Bound Rectangle\");
primaryStage.show();
}
}
Solution
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.setTitle(\"\");
VBox vb = new VBox();
Pane canvas = new Pane();
canvas.setPrefSize(500,500);
canvas.setMaxWidth(800);
canvas.setMaxHeight(800);
Rectangle rectangle = new Rectangle((int)(canvas.getMaxWidth()*1/4),
(int)(canvas.getMaxHeight()*1/4),
(int)(canvas.getMaxWidth()*1/2),(int)(canvas.getMaxWidth()*1/2));
rectangle.setFill(Color.SKYBLUE);
System.out.println(canvas.getMaxHeight());
System.out.println(canvas.getMaxWidth());
rectangle.relocate(70,70);
canvas.getChildren().add(rectangle);
vb.getChildren().add(canvas);
scene.setRoot(vb);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}


