Java Reference
In-Depth Information
Create Text nodes to be placed on the JavaFX scene graph by utilizing the
javafx.scene.text.Text class. As Text nodes are to be placed on the scene
graph, you decide you want to create randomly positioned Text nodes rotated around
their (x, y) positions scattered about the scene area.
The following code implements a JavaFX application that displays Text nodes
scattered about the scene graph with random positions and colors:
primaryStage.setTitle("Chapter 14-2 Drawing Text");
Group root = new Group();
Scene scene = new Scene(root, 300, 250, Color.WHITE);
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i < 100; i++) {
int x = rand.nextInt((int) scene.getWidth());
int y = rand.nextInt((int) scene.getHeight());
int red = rand.nextInt(255);
int green = rand.nextInt(255);
int blue = rand.nextInt(255);
Text text = new Text(x, y, "Java 8 Recipes");
int rot = rand.nextInt(360);
text.setFill(Color.rgb(red, green, blue, .99));
text.setRotate(rot);
root.getChildren().add(text);
}
primaryStage.setScene(scene);
primaryStage.show();
Figure 14-2 shows random Text nodes scattered about the JavaFX scene graph.
 
Search WWH ::




Custom Search