Question: Write a program that displays a flag rising up, as shown in Figure 15.14. As the national flag rises, play the national anthem. (You may

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

FagRising Animation O| FlagRisingAnimation FagRisingAnimation 1 import javafx.application.Application; 2 import javafx.collections.FXCollections; 3

The animation simulates a flag rising.

Listing

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.contro1.ComboBox; 9 import javafx.scene.control.Label; 10 import javafx.scene.image.Image;

FagRising Animation O| FlagRisingAnimation FagRisingAnimation 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.contro1.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 19 20 21 22 private final static int NUMBER_OF_NATIONS private final static String URLBase = "http://cs.armstrong.edu/liang/common"; private int currentIndex = 0; 7; @0verride // Override the start method in the Application class public void start(Stage primaryStage) { Image [] images = new Image [NUMBER_OF_NATIONS]; MediaPlayer[] mp = new MediaPlayer[NUMBER_OF_NATIONS]; 23 24 25 26 27 28 29 30 31 32 33 // Load images and audio for (int i = 0; i < NUMBER_OF_NATIONS; i++) { images [i] mp[i] = new MediaPlayer(new Media( URLBase + "/audio/anthem/anthem" + i + ".mp3")); = new Image (URLBase + "/image/flag" + i + ".gif"); 34 Button btPlayPause - new Button (">"); btPlayPause.setOnAction (e -> { if (btPlayPause.getText().equals(">")) { btPlayPause.setText ("||"); mp[currentIndex]. pause(); } else { btPlayPause.setText(">"); mp[currentIndex].play(); 35 36 37 38 39 40 41 42 43 44 }); 45 46 ImageView imageView = new ImageView(images[currentIndex]); ComboBox cboNation = new ComboBox (); Observablelist items = ("Denmark", "Germany", "China", "India", "Norway", "UK", "US"); cboNation.getItems ().addA11(items); cboNation.setValue(items.get(0)); cboNation.setOnAction(e -> { mp[currentIndex].stop(); currentIndex = items.indexOf(cboNation.getValue()); imageView.setImage(images [currentIndex]); mp [currentIndex].play(); }); 47 FXCo1lections.observableArrayList 48 49 50 51 52 53 54 55 56 57

Step by Step Solution

3.38 Rating (179 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan The program creates an interface that displays a flag rising up that is with reference ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!