double radius = 100.0, yCenter = radius*3/2, xCenter = radius*3/2 ; Circle circle = new Circle( xCenter, yCenter, radius); circle.setFill( Color.YELLOW ); circle.setStroke( Color.DARKCYAN ); Pane pane = new Pane( circle ); Scene scene = new Scene( pane, radius*3, radius*3, Color.GHOSTWHITE );
This puts the circle centered in the window with a margin of half the radius. You could squeeze the circle into a smaller window:
double radius = 100.0, yCenter = radius*5/4, xCenter = radius*5/4 ; Circle circle = new Circle( xCenter, yCenter, radius); circle.setFill( Color.YELLOW ); circle.setStroke( Color.DARKCYAN ); Pane pane = new Pane( circle ); Scene scene = new Scene( pane, radius*5/2, radius*5/2, Color.GHOSTWHITE );
Part of the job of start()
is to create a scene graph.
This is a data structure that organizes the Node
s of the Application
.
Node
s are the Shape
s, GUI controls, Pane
s and several
other types of objects.
We have already seen Circle
which is a kind of Shape
.
Here (again) is the code that creates the scene graph for the previous program:
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 CircleDarker extends Application { public void start( Stage primaryStage ) { Color myFill = new Color( 0.9, 0.7, 0.9, 1.0 ); double rad = 50.0, yCenter = rad*3/2, xCenter = rad*3/2, shift = rad ; Circle circleA = new Circle( xCenter, yCenter, rad); circleA.setFill( myFill ); circleA.setStroke( Color.PURPLE ); xCenter += shift; myFill = myFill.darker(); Circle circleB = new Circle( xCenter, yCenter, rad); circleB.setFill( myFill ); circleB.setStroke( Color.PURPLE ); xCenter += shift; myFill = myFill.darker(); Circle circleC = new Circle( xCenter, yCenter, rad); circleC.setFill( myFill ); circleC.setStroke( Color.PURPLE ); Pane pane = new Pane( circleA, circleB, circleC ); Scene scene = new Scene( pane, rad*5, rad*3, Color.AQUA.brighter() ); primaryStage.setTitle("Darker Circles"); primaryStage.setScene( scene ); primaryStage.show(); } }
Here is the scene graph that it creates:
In this scene graph, the root node is a Pane
referenced by the variable pane
.
The three Circle
s are children of the root node.
In general (in more complex scenes), a node in the graph has one parent and may have several children. No node has more than one parent. A root is the node that has no parent. (You may recognize this as a tree data structure.)
The Java run time system uses the information the the scene graph to draw the window on the monitor screen.
(Thought Question: ) Could the same object occur two or more times a scene graph?