Java Reference
In-Depth Information
Scene scene = new Scene(root, 800, 600, Color.BLACK);
List<Circle> circles = new ArrayList<>();
for (int i = 0; i < 50; i++) {
final Circle circle = new Circle(150);
circle.setCenterX(Math.random() * 800);
circle.setCenterY(Math.random() * 600);
circle.setFill(new Color(Math.random(), Math.random(), Math.random(), .2));
circle.setEffect(new BoxBlur(10, 10, 3));
circle.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
KeyValue collapse = new KeyValue(circle.radiusProperty(), 0);
new Timeline(new KeyFrame(Duration.seconds(3), collapse)).play();
});
circle.setStroke(Color.WHITE);
circle.strokeWidthProperty().bind(Bindings
.when(circle.hoverProperty())
.then(4)
.otherwise(0));
circles.add(circle);
}
root.getChildren().addAll(circles);
primaryStage.setScene(scene);
primaryStage.show();
Timeline moveCircles = new Timeline();
circles.stream().forEach(circle -> {
KeyValue moveX = new KeyValue(circle.centerXProperty(), Math.random() * 800);
KeyValue moveY = new KeyValue(circle.centerYProperty(), Math.random() * 600);
moveCircles.getKeyFrames().add(new KeyFrame(Duration.seconds(40), moveX, moveY));
});
moveCircles.play();
}
}
Although the code is fairly easy to understand, it is also quite verbose. The basic functionality can be summarized
as follows.
Fifty circles of varying colors are overlaid with a transparent fill.
Those circles are animated in a semirandom pattern around the window.
When the mouse hovers over a circle, the circle gets surrounded by a white border.
Upon clicking a circle, it slowly shrinks and vanishes.
In a very short amount of code this lets us demonstrate many different JavaFX features, including shapes, effects,
animation, binding, streams, and event listeners using lambda expressions. In the next few examples we convert this
exact application to several different languages and representations, letting you see how these features vary in each of
the choices available to you.
Vanishing Circles in Alternative JVM Languages
Now we move on to different JVM languages and show what is possible by using Groovy and Scala. These both make
use of an inner domain-specific language (DSL) written on top of the JavaFX APIs.
 
Search WWH ::




Custom Search