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

Basic Stroke

Note that the JavaFX developers decided to not introduce an extra class for strokes; all corresponding methods belong to the Shape class instead:

shape.setStroke(Color.BLACK);
shape.setStrokeWidth(10);
shape.setStrokeType(StrokeType.CENTERED);

The first two are pretty self-explanatory—color and width. Next up is a stroke type that controls positioning relative to shape's edge. See the following image and corresponding sample:

// chapter2/strokes/StrokeTypesDemo.java
hbox.getChildren().add(new VBox(5, new Text("NONE"), new Rectangle(50,50, Color.LIGHTGRAY)));
for (StrokeType type : StrokeType.values()) {
Rectangle rect = new Rectangle(50,50, Color.LIGHTGRAY);
rect.setStrokeType(type);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(10);

hbox.getChildren().add(new VBox(5, new Text(type.toString()), rect));
}

We get the following output:

Some shapes may have no inner area at all, for example, Line. So, to change the color of such a shape you need to use setStroke() rather than setFill().