Subject:Computer SciencePrice: Bought3
1. Raise flags Rewrite Listing 15.13 using a thread to animate a flag being raised. Compare the program with Listing 15.13 by setting the delay time to 10 in both programs. Which one runs the animation faster?
2. Synchronize threads a program that launches 1,000 threads. Each thread adds 1 to a variable sum that initially is 0. You need to pass sum by reference to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.
Listing 15.13
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 path transition
21 PathTransition pt = new PathTransition(Duration.millis(10000),
22 new Line(100, 200, 100, 0), imageView);
23 pt.setCycleCount(5);
24 pt.play(); // Start animation
25
26 // Create a scene and place it in the stage
27 Scene scene = new Scene(pane, 250, 200);
28 primaryStage.setTitle("FlagRisingAnimation"); // Set the stage title
29 primaryStage.setScene(scene); // Place the scene in the stage
30 primaryStage.show(); // Display the stage
31 }
32
33 /**
34 * The main method is only needed for the IDE with limited
35 * JavaFX support. Not needed for running from the command line.
36 */
37 public static void main(String[] args) {
38 launch(args);
39 }
40 }