Problem

(Simulation: raise flag and play anthem) Write a program that displays a flag rising up, a...

(Simulation: raise flag and play anthem) Write a program that displays a flag rising up, as shown in Figure 1. As the national flag rises, play the national anthem. (You may use a flag image and anthem audio file from Listing 1.)

FIGURE 1 The animation simulates a flag rising.

LISTING 1 FlagAnthem.java

1 import javafx.application.Application;

2 import javafx.collections.FXCollections;

3 import javafx.collections.ObservableList;

4 import javafx.stage.Stage;

5 import javafx.geometry.Pos;

6 import javafx.scene.Scene;

7 import javafx.scene.control.Button;

8 import javafx.scene.control.ComboBox;

9 import javafx.scene.control.Label;

10 import javafx.scene.image.Image;

11 import javafx.scene.image.ImageView;

12 import javafx.scene.layout.BorderPane;

13 import javafx.scene.layout.HBox;

14 import javafx.scene.media.Media;

15 import javafx.scene.media.MediaPlayer;

16

17 public class FlagAnthem extends Application {

18 private final static int NUMBER_OF_NATIONS = 7;

19 private final static String URLBase =

20 "http://cs.armstrong.edu/liang/common";

21 private int currentIndex = 0;

22

23 @Override // Override the start method in the Application class

24 public void start(Stage primaryStage) {

25 Image[] images = new Image[NUMBER_OF_NATIONS];

26 MediaPlayer[] mp = new MediaPlayer[NUMBER_OF_NATIONS];

27

28 // Load images and audio

29 for (int i = 0; i

30 images[i] = new Image(URLBase + "/image/flag" + i + ".gif");

31 mp[i] = new MediaPlayer(new Media(

32 URLBase + "/audio/anthem/anthem" + i + ".mp3"));

33 }

34

35 Button btPlayPause = new Button(">");

36 btPlayPause.setOnAction(e -> {

37 if (btPlayPause.getText().equals(">")) {

38 btPlayPause.setText("||");

39 mp[currentIndex].pause();

40 } else {

41 btPlayPause.setText(">");

42 mp[currentIndex].play();

43 }

44 });

45

46 ImageView imageView = new ImageView(images[currentIndex]);

47 ComboBox cboNation = new ComboBox<>();

48 ObservableList items = FXCollections.observableArrayList

49 ("Denmark", "Germany", "China", "India", "Norway", "UK", "US");

50 cboNation.getItems().addAll(items);

51 cboNation.setValue(items.get(0));

52 cboNation.setOnAction(e -> {

53 mp[currentIndex].stop();

54 currentIndex = items.indexOf(cboNation.getValue());

55 imageView.setImage(images[currentIndex]);

56 mp[currentIndex].play();

57 });

58

59 HBox hBox = new HBox(10);

60 hBox.getChildren().addAll(btPlayPause,

61 new Label("Select a nation: "), cboNation);

62 hBox.setAlignment(Pos.CENTER);

63

64 // Create a pane to hold nodes

65 BorderPane pane = new BorderPane();

66 pane.setCenter(imageView);

67 pane.setBottom(hBox);

68

69 // Create a scene and place it in the stage

70 Scene scene = new Scene(pane, 350, 270);

71 primaryStage.setTitle("FlagAnthem"); // Set the stage title

72 primaryStage.setScene(scene); // Place the scene in the stage

73 primaryStage.show(); // Display the stage

74 }

75 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT