JavaFX: The Shape Class
The fascinating world of drawing in JavaFX introduces you to drawing two-dimensional objects such as lines, arcs, rectangles, ellipses, and so on. You can set the style used to draw the shape’s outline, and you can fill the shape with a solid color, a gradient fill, or text that’s created from an image.
You can make your shapes solid or transparent, and you can rotate, clip, skew, and do all sorts of other unspeakable things to them.
There are two types of nodes you can add to a scene graph: controls, which inherit the Control class, and layout panes, which inherit the Pane class. In this section, you read about how to work with a third type of node: shapes, which (as you might guess) inherit the Shape class. This table lists some of the most commonly used methods of the Shape class.
|void setFill(Paint value)||Sets the fill color.|
|void setSmooth(boolean value)||If true, anti-aliasing is used to
draw the shape more precisely.
|void setStroke(Paint value)||Sets the color of the stroke used to draw the shape’s
|void setStrokeWidth(double value)||Sets the width of the stroke used to draw the shape’s
|void setStrokeType(StrokeType value)||Sets the position of the stroke relative to the actual outline
of the shape. Allowable values are StrokeType.CENTERED, StrokeType.INSIDE, and StrokeType.OUTSIDE.
|void setStrokeLineJoin(StrokeLineJoin value)||Sets the method used to draw corners. Allowable values are
StrokeLineJoin.MITER, StrokeType.BEVEL, and StrokeType.ROUND.
|void setStrokeEndCap(StrokeEndCap value)||Sets the method used to draw the ends of the stroke line.
Allowable values are StrokeLineCap.BUTT,
StrokeLineCap.ROUND, and StrokeLineCap.SQUARE.
|static Shape intersect(Shape shape1, Shape shape2)||Returns a new shape that consists only of those parts of shape1 and shape2
|static Shape subtract(Shape shape1, Shape shape2)||Returns a new shape that is formed by subtracting the shape2 from shape1.|
|static Shape union(Shape shape1, Shape shape2)||Returns a new shape that is formed by adding shape1 to shape2.|
There are a total of 12 shapes that inherit the Shape class: Arc, Circle, CubicCurve, Ellipse, Line, Path, Polygon, Polyline, QuadCurve, Rectangle, SVGPath, and Text. The table shows a basic constructor for each of these classes, and the figure shows a scene with a sample of several of them.