Java Reference
In-Depth Information
9 import javafx.util.Duration;
10
11 public class FadeTransitionDemo extends Application {
12 @Override // Override the start method in the Application class
13 public void start(Stage primaryStage) {
14 // Place an ellipse to the pane
15 Pane pane = new Pane();
16 Ellipse ellipse = new Ellipse( 10 , 10 , 100 , 50 );
17 ellipse.setFill(Color.RED);
18 ellipse.setStroke(Color.BLACK);
19 ellipse.centerXProperty().bind(pane.widthProperty().divide( 2 ));
20 ellipse.centerYProperty().bind(pane.heightProperty().divide( 2 ));
21 ellipse.radiusXProperty().bind(
22 pane.widthProperty().multiply( 0.4 ));
23 ellipse.radiusYProperty().bind(
24 pane.heightProperty().multiply( 0.4 ));
25
create a pane
create an ellipse
set ellipse fill color
set ellipse stroke color
bind ellipse properties
pane.getChildren().add(ellipse);
add ellipse to pane
26
27
// Apply a fade transition to ellipse
28
FadeTransition ft =
create a FadeTransition
29
new FadeTransition(Duration.millis( 3000 ), ellipse);
30
ft.setFromValue( 1.0 );
set start opaque value
set end opaque value
set cycle count
set auto reverse true
play animation
31
ft.setToValue( 0.1 );
32
ft.setCycleCount(Timeline.INDEFINITE);
33
ft.setAutoReverse( true );
34
ft.play(); // Start animation
35
36
// Control animation
37
ellipse.setOnMousePressed(e -> ft.pause());
pause animation
resume animation
38
ellipse.setOnMouseReleased(e -> ft.play());
39
40 // Create a scene and place it in the stage
41 Scene scene = new Scene(pane, 200 , 150 );
42 primaryStage.setTitle( "FadeTransitionDemo" ); // Set the stage title
43 primaryStage.setScene(scene); // Place the scene in the stage
44 primaryStage.show(); // Display the stage
45 }
46 }
F IGURE 15.19
The FadeTransition animates the change of opacity in the ellipse.
The program creates a pane (line 15) and an ellipse (line 16) and places the ellipse into the
pane (line 25). The ellipse's centerX , centerY , radiusX , and radiusY properties are
bound to the pane's size (lines 19-24).
A fade transition is created with a duration of 3 seconds for the ellipse (line 29). It sets the
start opaque to 1.0 (line 30) and the stop opaque 0.1 (line 31). The cycle count is set to infinite
so the animation is repeated indefinitely (line 32). When the mouse is pressed, the animation
is paused (line 37). When the mouse is released, the animation resumes from where it was
paused (line 38).
 
 
Search WWH ::




Custom Search