Mastering JavaFX 10
上QQ阅读APP看书,第一时间看更新

Lines

Line is as simple as it sounds. You write start and end coordinates, and they get connected:

Line line = new Line(10, 10, 100, 50);

Polyline is a set of consecutive lines. You need to provide several pairs of coordinates where the end of each line is the start of the next one. Make sure you are providing an even number of parameters:

// chapter2/shapes/Polylines.java
Polyline polyline = new Polyline();
polyline.getPoints().addAll(
0.0, 0.0,
50.0, 30.0,
10.0, 60.0);

Note that despite not always having a full border, line-type shapes can have a background. If you assign it using the setFill() method, these shapes will use invisible edge, connecting the first and last points of the line. Here is an example of the same polylines with and without a background:

The third shape is a Polygon with the same points set. The only difference between Polygon and Polyline is that the former automatically adds a line between the first and the last points to create a closed figure.