Java Reference
In-Depth Information
Figure 14-2
.
Drawing text in random places
How It Works
To draw text in JavaFX, you create a
javafx.scene.text.Text
node to be
placed on the scene graph (
javafx.scene.Scene
). In this example you'll notice
text objects with random colors and positions scattered about the
Scene
area.
First, you create a loop to generate random (x,y) coordinates to position
Text
nodes. Second, you create random color components between (0-255 RGB) to be ap-
plied to the
Text
nodes. Third, the rotation angle (in degrees) is a randomly generated
value between (0-360 degrees) to cause the text to be slanted. The following code cre-
ates random values that will be assigned to a
Text
node's position, color, and rotation:
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);
int rot = rand.nextInt(360);