How it works...
Creating shapes using the Shape API is a straightforward process. You simply declare an
instance of the shape class that you want to draw, specify its properties, and then place
it as a node inside the stage's scene so that it can be rendered. Let's see how the shapes
presented in this recipe work:
F Line —when drawing a line, you declare an instance of the Line class specifying,
at minimum, the line's starting coordinates with properties startX and startY
and the ending coordinates using properties endX and endY .
startX:10 startY: 10
endX: 10 endY: 10
F Rectangle —for drawing a rectangle, you use the Rectangle class from the
Shape API. At a minimum, you must specify the coordinates of the upper-left
corner using properties x and y , and the size of the rectangle using properties
width and height :
x: 50 y: 20
width: 300 height: 200
arcWidth: 20 arcHeight : 20
arcWidth and arcHeight —optionally, you can use these properties to specify
vertical and horizontal diameters for rounded corners of the rectangle.
F Circle —to draw a circle in JavaFX, you declare an instance of the Circle class.
You must, at a minimum, specify the circle's center coordinates using centerX
and centerY properties and the circle's radius using the radius property. When
placing a circle on stage, keep in mind that the circle's center is used as its point
of reference. Be sure to adjust your coordinates to avoid drawing circles partially
centerX: 50 centerY: 50
F Ellipse —both ellipse and circle work similarly. To draw the ellipse, you declare an
instance of the Ellipse class specifying the x and y coordinates of the center using
properties centerX and centerY . Additionally, you must specify the horizontal and
vertical length of the radius for the ellipse using properties radiusX and radiusY :
centerX: 100 centerY: 150
radiusX: 100 radiusY: 20