-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise15_30.java
66 lines (56 loc) · 2.22 KB
/
Exercise15_30.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ch_15;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
/**
* **15.30 (Slide show) Twenty-five slides are stored as image files (slide0.jpg, slide1
* .jpg, . . . , slide24.jpg) in the image directory downloadable along with the
* source code in the book. The size of each image is 800 * 600.
* <p>
* Write a program that automatically displays the slides repeatedly.
* Each slide is shown for two seconds. The slides are displayed in order. When the last slide finishes, the
* first slide is redisplayed, and so on. Click to pause if the animation is currently
* playing. Click to resume if the animation is currently paused
* <p>
* Note: I used the card images from the book's resources folder instead of the slides as
* I could not find the slides.
*/
public class Exercise15_30 extends Application {
private int index = 0;
@Override
public void start(Stage primaryStage) {
Image[] image = new Image[26];
for (int i = 0; i < 25; i++) {
String imagePath = "resources" + File.separator + "images" + File.separator + "slide" + i + ".jpg";
image[i] = new Image(imagePath);
}
ImageView imageView = new ImageView(image[0]);
StackPane pane = new StackPane();
pane.getChildren().add(imageView);
Scene scene = new Scene(pane, 800, 600);
primaryStage.setTitle(getClass().getName());
primaryStage.setScene(scene);
primaryStage.show();
Timeline animation = new Timeline(
new KeyFrame(Duration.millis(2000), e -> {
imageView.setImage(image[++index % 25]);
}));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.setOnMouseClicked(e -> {
if (animation.getStatus() == Animation.Status.PAUSED) {
animation.play();
} else {
animation.pause();
}
});
}
}