Java Reference
In-Depth Information
Creating complex shapes using Path
The simple geometrical shapes offered by the Shape API may not adequately meet your
requirements. JavaFX also offers the Path-API to simplify the creation of complex and irregular
shapes using a step-wise approach of drawing shape segments. In this recipe, you will learn
how to use the Path API and its associated classes to create a complex shape.
Getting ready
Before you can draw complex shapes using JavaFX, you must know how to create a basic
JavaFX application and how to add content to the application's scene. To refresh your memory,
see recipe
Building a JavaFX application
from this chapter. It will also be helpful to review the
recipe
Drawing simple shapes
to get an idea of how the
Shape
API works. To use the
Path
API, you will need to import classes found in the
javafx.scene.shape
package.
How to do it...
The next code snippet is an example of how to use the Path API to create irregular shapes.
You can find the complete listing of this code in
ch002/source-code/src/shapes/
PathDrawing.fx
.
Stage {
title: "Path Drawing"
width: 500
height: 300
x:100;
y:200
scene: Scene {
content: [
Path
{
elements: [
MoveTo { x: 150 y: 75 },
ArcTo {x: 200 y: 50
radiusX:25 radiusY:25 sweepFlag: true}
MoveTo { x: 200 y: 50 },
ArcTo {x: 250 y: 50
radiusX:25 radiusY:25 sweepFlag: true }
MoveTo { x: 250 y: 50 },
ArcTo { x: 300 y: 75
radiusX:25 radiusY:25 sweepFlag: true }
MoveTo { x: 25 y: 75 },
HLineTo {x: 425},
MoveTo { x: 160 y: 85 },
HLineTo {x: 290},