Yes.
Group
Here is the program that creates to above picture.
The program defines an Emoji
class
that can be used like any Shape
class.
You can construct several Emoji
objects and put them in a scene graph.
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.layout.*;
class Emoji extends Group
{
double width = 400.0;
public Emoji( double width )
{
// call the constructor of the superclass
super();
// get the parameter
this.width = width;
// create an emoji, based on the width parameter
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 ) );
// add the shape objects to this Group
super.getChildren().addAll( face, mouth, leftEye, rightEye);
}
}
public class EmojiAsGroup extends Application
{
public void start( Stage primaryStage )
{
// Create two Emoji objects
Emoji emojiL = new Emoji( 300 );
Emoji emojiR = new Emoji( 200 );
// Add them to a HBox, a type of Panel
HBox hbx = new HBox( emojiL, emojiR );
// The Panel is the root of the scene graph
Scene scene = new Scene( hbx, Color.LIGHTCYAN );
primaryStage.setTitle("Two Surprised Faces");
primaryStage.setScene( scene );
primaryStage.show();
}
}
To run this program,
copy all of the above to one file EmojiAsGroup.java
,
compile and run it as usual:
% javac EmojiAsGroup.java % java EmojiAsGroup
Better: make two files, one per class:
Emoji.java
and EmojiAsGroup.java
.
Declare public class Emoji
in the first file.
You will need to have the import statements in both files.
Compile and run the files.
% javac Emoji.java EmojiAsGroup.java % java EmojiAsGroup
(Thought Question: )
The two Emoji objects are added to a HBox
,
which is a type of Panel
HBox hbx = new HBox( emojiL, emojiR );
An HBox
itself arranges the nodes placed within it and determines its own size.
Based on the above program and picture
How are the objects in an HBox
arranged?
What size is picked for an HBox
?