Java Reference
In-Depth Information
The program is given in Listing 16.15.
L ISTING 16.15
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
VideoNote
Audio and image
private final static int NUMBER_OF_NATIONS = 7 ;
19
private final static String URLBase =
20
"http://cs.armstrong.edu/liang/common" ;
URLBase for image and audio
track current image/audio
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];
image array
media player array
26
MediaPlayer[] mp = new MediaPlayer[NUMBER_OF_NATIONS];
27
28 // Load images and audio
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].pause();
40 } else {
41 btPlayPause.setText( ">" );
42
load image
load audio
create play button
handle button action
pause audio
mp[currentIndex].play();
play audio
43 }
44 });
45
46 ImageView imageView = new ImageView(images[currentIndex]);
47 ComboBox<String> cboNation = new ComboBox<>();
48 ObservableList<String> 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 });
create image view
create combo box
create observable list
process combo selection
choose a new nation
play audio
 
Search WWH ::




Custom Search