No. You can give it any legal Java name. But in a program with several stages, it is useful to use descriptive names.
Here is a program that draws a circle.
import javafx.application.*; // 1. import javafx.stage.*; // 6. import javafx.scene.Scene; // 5. import javafx.scene.shape.*; // 2. includes Circle class import javafx.scene.paint.*; // 3, 5. includes Color import javafx.scene.layout.*; // 4. includes Pane class public class CircleScene extends Application // 1 { public void start( Stage primaryStage ) // 1 { Circle circle = new Circle( 100.0, 90.1, 50.5 ); // 2 circle.setFill( Color.GREEN ); // 3 circle.setStroke( Color.BLACK ); Pane pane = new Pane( circle ); // 4 Scene scene = new Scene( pane, 300.0, 200.0, Color.LIGHTYELLOW ); // 5 primaryStage.setTitle("Circle"); primaryStage.setScene( scene ); // 6 primaryStage.show(); } }
1. Extend the Application
class and define
a start()
method.
The runtime system creates your Application
,
creates a Stage
and calls your start()
method with
a reference to it.
2. The program constructs a Circle
object.
The parameters are:
Circle(double centerX, double centerY, double radius)
Think of a sheet of graph paper. A location is described as (X, Y) where X and Y are integers. The (0, 0) location is at the top left.
These parameters are of type double
.
There are several constructors for the Circle
class
(see below).
3. Specify the fill color (the color that fills the circle) and the stroke color (the color for the perimeter of the circle).
4. The Circle
is added to a Pane
.
Graphics objects must be part of a Pane
or other container.
There are several classes that work as containers.
(More about this later.)
For now, let's use Pane
.
The parameters for this Pane
constructor are:
public Pane(Node... children)
This says that any number ofNode
s can be added to aPane
. ACircle
is a kind ofNode
, as are otherShape
objects.
5. The Pane
is added to a Scene
.
Every JavaFX program needs one or more Scene
s.
There may be several Scene
s in a program,
but only one is on stage at a time.
The parameters for this Scene
constructor are:
public Scene(Parent root, double width, double height, Paint fill)
The first parameter is the root of the scene graph.
The scene graph is a data structure that contains all the objects in the
Scene
.
(More on this later.)
For this program the root is a Pane
.
The width and height are the preferred size of the Scene
.
If this this is not large enough, the graphics system will make a bigger Scene
.
6. The Scene
is put on Stage
.
A title is provided for the Stage
.
JavaFX roughly follows an analogy to a theater.
A Stage is need for the performance (application).
The primary stage is created and passed to your start()
method.
That method can created additional stages if needed.
A Scene takes place on the Stage. There may be several Scenes used in the program, but only one at a time is on Stage.
Graphics objects are placed in a Pane. There may be several Panes used in a Scene. (The analogy breaks down at this point.)
Could you put the Circle
directly on Stage
without
being part of a Pane
(or other container class)?