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

Answer:

Yes.


Persistent Scene Graph

The start() method is handed a Stage by the JavaFX system:

start( Stage primaryStage ) 

Of course, the JavaFX system itself keeps a reference to that Stage so that it continues to exist and can be displayed after the start() method has finished.

The start() method builds a scene graph and constructs a Scene that contains the root of the graph (pane, in this program.) The Scene is then put on stage:

Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.GHOSTWHITE ); 

primaryStage.setScene( scene );   

IMPORTANT (and somewhat confusing): The scene graph and the objects in it continue to exist after start() has finished because there are still active references to all the objects.

The references can all be reached from the root of the scene graph, which is contained in the Scene, which is contained by the Stage, which remains alive while the program is running. The runtime system can find all the objects. The garbage collector leaves our objects alone. Here is the scene graph for the program:


Scene Graph: root with three Rectangle child nodes

In fact, after the start() method has finished, the variables rectA, rectB, rectC, and pane no longer exist (but the objects they pointed to remain.)


QUESTION 16:

Would the following code fragment work as well for this program? Notice that only one Rectangle reference variable is used.

   // Create the first Rectangle
    rectWidth=sceneWidth*0.8;
    rectHeight=sceneHeight*0.8;
    x = (sceneWidth-rectWidth)/2;
    y = (sceneHeight-rectHeight)/2;
    Rectangle rect = new Rectangle( x, y, rectWidth, rectHeight );
    rect.setFill(  Color.YELLOW );  
    rect.setStroke( Color.GREY );
    pane.getChildren().add( rect ); // 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;
    rect = new Rectangle( x, y, rectWidth, rectHeight );
    rect.setFill(  Color.GREEN );  
    rect.setStroke( Color.BLACK );
    pane.getChildren().add( rect );  // 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;
    rect = new Rectangle( x, y, rectWidth, rectHeight );
    rect.setFill(  Color.BLUE);  
    rect.setStroke( Color.DARKGREEN );
    pane.getChildren().add( rect );  // Add it to the Pane

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

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