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 "https://liveexample.pearsoncmg.com/common";
21 private int currentIndex = 0;
22
23 @Override
24 public void start(Stage primaryStage) {
25 Image[] images = new Image[NUMBER_OF_NATIONS];
26 MediaPlayer[] mp = new MediaPlayer[NUMBER_OF_NATIONS];
27
28
29 for (int i = 0; i < NUMBER_OF_NATIONS; 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].play();
40 }
41 else {
42 btPlayPause.setText(">");
43 mp[currentIndex].pause();
44 }
45 });
46
47 ImageView imageView = new ImageView(images[currentIndex]);
48 ComboBox<String> cboNation = new ComboBox<>();
49 ObservableList<String> items = FXCollections.observableArrayList
50 ("Denmark", "Germany", "China", "India", "Norway", "UK", "US");
51 cboNation.getItems().addAll(items);
52 cboNation.setValue(items.get(0));
53 cboNation.setOnAction(e -> {
54 mp[currentIndex].stop();
55 currentIndex = items.indexOf(cboNation.getValue());
56 imageView.setImage(images[currentIndex]);
57 mp[currentIndex].play();
58 btPlayPause.setText("||");
59 });
60
61 HBox hBox = new HBox(10);
62 hBox.getChildren().addAll(btPlayPause,
63 new Label("Select a nation: "), cboNation);
64 hBox.setAlignment(Pos.CENTER);
65
66
67 BorderPane pane = new BorderPane();
68 pane.setCenter(imageView);
69 pane.setBottom(hBox);
70
71
72 Scene scene = new Scene(pane, 350, 270);
73 primaryStage.setTitle("FlagAnthem");
74 primaryStage.setScene(scene);
75 primaryStage.show();
76 mp[currentIndex].play();
77 }
78
79
83 public static void main(String[] args) {
84 launch(args);
85 }
86 }