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

Answer:

Sure!


Nested Rectangles

Many nested rectangles, centered in the window

A for loop is used in start() to draw several nested rectangles. The parameter scale is adjusted to make each new rectangle smaller than the previous.

The double precision variable scale is used to control the loop. Every call to fillGroup() is with a smaller scale, so the rectangles get smaller and smaller.

import javafx.application.*;  
import javafx.stage.*;        
import javafx.scene.*;  
import javafx.scene.shape.*; 
import javafx.scene.paint.*;  

public class NestedRectangles extends Application
{ 
  double sceneWidth=400, sceneHeight= 300;  
  double centerX=sceneWidth/2, centerY=sceneHeight/2;

  private void fillGroup( Group gr, double scale )
  {
    double width=sceneWidth*scale, height=sceneHeight*scale;
    
    Rectangle rect = new Rectangle(  centerX-width/2, centerY-height/2, width, height );
    rect.setStrokeWidth( 2.0 );
    rect.setStroke( Color.RED );
    rect.setFill( Color.TRANSPARENT );
    gr.getChildren().add( rect );      
  }

  public void start(Stage stage) 
  { 
    Group  root = new Group( );  
    
    for ( double scale = 0.9; scale>=0.1; scale -= 0.1 )
    {
      fillGroup( root, scale );
    }
 
    Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.GHOSTWHITE ); 
    stage.setTitle("Nested Rectangles"); 
    stage.setScene(scene); 
    stage.show(); 
  }      

} 

QUESTION 7:

What does the scene graph look like for this?


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