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

Answer:

shape.setTranslateX( centerX-squareSide/2 );
shape.setTranslateY( centerY-squareSide/2 );
shape.setScaleX( 0.5 );

StackPane

Centered Emoji

The Emoji program could have been written using a Pane rather than a Group. The mouth and eyes could have been placed in a Pane, and the Pane transformed just like the Group. So, what good is a Group? A Group is useful for grouping nodes together that you wish to treat as a unit. For example, you might want to think of the face as a thing, and perhaps create several of them.

In this modified program the eyes are put in their own Group. There is no advantage in doing this in this program, but in larger programs grouping shapes is often useful.

Group eyes = new Group( leftEye, rightEye );

The Pane class has several subclasses called layout panes that automatically position Nodes they contain. In the modified Emoji program, a StackPane automatically positions the face in its center.

A StackPane holds a stack of children, like a stack of pancakes. (In our program, there is just one child, the entire face.) The default behavior is to put each child in the center. The order that children are put in the stack determines the order in which they will be painted on the screen. The last child put in is at the top of the stack and will be displayed on top of the others.

The program of the previous question could be made slightly better by using a StackPane.

Oracle Documentation for StackPane


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

public class EmojiStackPane extends Application
{
  public void start( Stage primaryStage )
  {
    double width = 400.0, height = width*0.75 ;
       
    double radiusFace  = width/3.0; 
    double radiusEye   = radiusFace*0.13;
    double radiusMouth = radiusFace*0.25;
    double mouthX      = 0.0;
    double mouthY      = radiusFace*0.35;
    double eyeY        = -radiusFace*0.26;
    double leftEyeX    = -radiusFace*0.30;
    double rightEyeX   = -leftEyeX;
        
    Circle face = new Circle( radiusFace );
    face.setFill( new Color( 0.98, 0.82, 0.38, 1.0 ) );
    
    Circle mouth = new Circle( mouthX, mouthY, radiusMouth );
    mouth.setFill( new Color( 0.45, 0.35, 0.15, 1.0 ) );
    
    Circle leftEye = new Circle( leftEyeX, eyeY, radiusEye );
    leftEye.setFill( new Color( 0.45, 0.35, 0.15, 1.0 ) );
    Circle rightEye = new Circle( rightEyeX, eyeY, radiusEye );
    rightEye.setFill( new Color( 0.45, 0.35, 0.15, 1.0 ) );
    Group eyes = new Group( leftEye, rightEye );

    Group emoji = new Group( face, mouth, eyes );  
     
    StackPane pane = new StackPane( emoji );  // StackPane puts the emoji in the center
  
    Scene scene = new Scene( pane, width, height, Color.LIGHTCYAN );
    
    primaryStage.setTitle("What! A StackPane can do This?!");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
}

QUESTION 16:

What is the root node of the scene graph in this program?

What is the child of the root node?

What are the children of that child?


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