go to previous page   go to home page   go to next page highlighting

Answer:

No. If you try to add the same object twice you will get a run-time error.


Nested Rectangles

Three Nested Rectangles

Here is a program that draws three nested rectangles.

 
import javafx.application.*;  
import javafx.stage.*;        
import javafx.scene.Scene;  
import javafx.scene.Group;  
import javafx.scene.layout.*;  
import javafx.scene.shape.*; 
import javafx.scene.paint.*;  

public class NestedRectangle extends Application  
{
  public void start( Stage primaryStage ) 
  {
    double sceneWidth=300, sceneHeight=sceneWidth*0.75, 
           sceneCenterX=sceneWidth/2, sceneCenterY=sceneHeight/2;
           
    double rectWidth, rectHeight;
    double x, y;
    
    Pane pane =   new Pane( );   
 
    // Create the first Rectangle
    rectWidth=sceneWidth*0.8;
    rectHeight=sceneHeight*0.8;
    x = (sceneWidth-rectWidth)/2;
    y = (sceneHeight-rectHeight)/2;
    Rectangle rectA = new Rectangle( x, y, rectWidth, rectHeight );
    rectA.setFill(  Color.YELLOW );  
    rectA.setStroke( Color.GREY );
    pane.getChildren().add( rectA ); // Put it in the Pane

    // Create the second Rectangle
    rectWidth=rectWidth*0.6; 
    rectHeight=rectHeight*0.6;
    x = (sceneWidth-rectWidth)/2;
    y = (sceneHeight-rectHeight)/2;
    Rectangle rectB = new Rectangle( x, y, rectWidth, rectHeight );
    rectB.setFill(  Color.GREEN );  
    rectB.setStroke( Color.BLACK );
    pane.getChildren().add( rectB );  // Add it to the Pane

    // Create the third Rectangle
    rectWidth=rectWidth*0.6; 
    rectHeight=rectHeight*0.6;
    x = (sceneWidth-rectWidth)/2;
    y = (sceneHeight-rectHeight)/2;
    Rectangle rectC = new Rectangle( x, y, rectWidth, rectHeight );
    rectC.setFill(  Color.BLUE);  
    rectC.setStroke( Color.DARKGREEN );
    pane.getChildren().add( rectC );  // Add it to the Pane

    // Add the pane to the scene
    Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.GHOSTWHITE ); 

    primaryStage.setTitle("Nested Rectangles");
    primaryStage.setScene( scene );   
    primaryStage.show();
  }
}

Several things are new in this program:

1. The Shape objects are Rectangles. Here is the constructor used (one of several available):

Rectangle(double x, double y, double width, double height)

Create a rectangle with specified width and height with upper left corner at (x,y)

2. The Rectangles are added to the scene graph one-by-one:

pane.getChildren().add( rectA ); // Put it in the Pane
...
pane.getChildren().add( rectB );  // Add it to the Pane
...
pane.getChildren().add( rectC );  // Add it to the Pane
...

The Pane is the root of the scene graph. A Child node can be added to it by first getting its list of children with getChildren() and then adding a Node to that list with add()

3. The sizes of the rectangles are based on the size of the Scene.

double sceneWidth=300, sceneHeight=sceneWidth*0.75, . . .
. . .
rectWidth=sceneWidth*0.8;
rectHeight=sceneHeight*0.8;
x = (sceneWidth-rectWidth)/2;
y = (sceneHeight-rectHeight)/2; 

If you change the value for sceneWidth, everything scales proportionally without any more changes. This is much better than hard-coded literals.

In the previous programs, the size of the window was based on the size of the Shapes. This is the opposite approach: the size of the Shapes is based on the size of the window.


QUESTION 15:

After the start() method has finished, does the scene graph continue to exist?


go to previous page   go to home page   go to next page