Java Reference
In-Depth Information
48 primaryStage.setScene(scene); // Place the scene in the stage
49 primaryStage.show(); // Display the stage
50 }
51 }
(a)
(b)
F IGURE 15.17
The PathTransition animates a rectangle moving along the circle.
The program creates a pane (line 16), a rectangle (line 19), and a circle (line 23). The circle
and rectangle are placed in the pane (lines 28 and 29). If the circle was not placed in the pane,
you will see the screen shot as shown in Figure 15.17b.
The program creates a path transition (line 32), sets its duration to 4 seconds for one cycle
of animation (line 33), sets circle as the path (line 34), sets rectangle as the node (line 35), and
sets the orientation to orthogonal to tangent (line 36).
The cycle count is set to indefinite (line 38) so the animation continues forever. The auto
reverse is set to true (line 39) so that the direction of the move is reversed in the alternating
cycle. The program starts animation by invoking the play() method (line 40).
If the pause() method is replaced by the stop() method in line 42, the animation will
start over from the beginning when it restarts.
Listing 15.13 gives the program that animates a flag rising, as shown in Figure 15.14.
L ISTING 15.13
FlagRisingAnimation.java
1 import javafx.animation.PathTransition;
2 import javafx.application.Application;
3 import javafx.scene.Scene;
4 import javafx.scene.image.ImageView;
5 import javafx.scene.layout.Pane;
6 import javafx.scene.shape.Line;
7 import javafx.stage.Stage;
8 import javafx.util.Duration;
9
10 public class FlagRisingAnimation extends Application {
11 @Override // Override the start method in the Application class
12 public void start(Stage primaryStage) {
13 // Create a pane
14 Pane pane = new Pane();
15
16 // Add an image view and add it to pane
17 ImageView imageView = new ImageView( "image/us.gif" );
18 pane.getChildren().add(imageView);
19
20
create a pane
create an image view
add image view to pane
// Create a path transition
21
PathTransition pt = new PathTransition(Duration.millis( 10000 ),
create a path transition
 
Search WWH ::




Custom Search