Yes, the program fragment works fine.
The Rectangle objects are added to the scene graph one by one.
Once an object is in the graph,
the local reference variable rect can be reused for the next object.
Although, if you expect to change an object after it has been added to the graph you need to keep a reference to it.
Rectangle
Here are some of the Rectangle constructors.
See the Oracle documentation for details.
Rectangle()
Creates an empty Rectangle.
Rectangle(double width, double height)
Creates a new Rectangle with the given width and height.
Rectangle(double x, double y, double width, double height)
Creates a new Rectangle with upper left corner at (x, y) with the given width and height.
Rectangle(double width, double height, Paint fill)
Creates a new Rectangle with the given width and height fill color.
The default fill color for Rectangle is black. The default stroke color is null.
Here are some of the Rectangle methods.
You can construct a Rectangle object
with one of the constructors, and then later on adjust its size, color, and location.
double getHeight()
Get height.
double getWidth()
Get width.
double getX()
Get x.
double getY()
Get y.
void setHeight(double value)
Set height.
void setWidth(double value)
Set width.
void setX(double value)
Set x.
void setY(double value)
Set y.
Here are some useful methods inherited from Shape.
public final void setFill(Paint value)
Set the color for the fill.
public final Paint getFill()
Get the color of the fill.
public final void setStroke(Paint value)
Set the color for the stroke
public final Paint getStroke()
Get the color of the stroke
public final void setStrokeWidth(double value)
Set the width of the stroke, in pixels
public final Paint getStrokeWidth()
Gets the width of the stroke, in pixels
Notice that Rectangles are mutable:
the instance variables of a Rectangle object can be changed as
a program is running.
What type of object (do you suppose) the following code constructs:
startX = startY = 0.0; endX = endY = 100.0 ; Line ln = new Line( startX, startY, endX, endY );