Java Reference
In-Depth Information
18.12 Case Study: National Flags and Anthems
This case study presents an applet that displays a nation's flag and plays its anthem.
Key
Point
The images in the applet are for seven national flags, named flag0.gif , flag1.gif , . . ., flag6.gif
for Denmark, Germany, China, India, Norway, the U.K., and the U.S. They are stored under
the image directory in the class path. The audio consists of national anthems for these seven
nations, named anthem0.mid , anthem1.mid , . . ., and anthem6.mid . They are stored under
the audio directory in the class path.
The program enables the user to select a nation from a combo box and then displays its flag
and plays its anthem. The user can suspend the audio by clicking the Suspend button and
resume it by clicking the Resume button, as shown in Figure 18.15.
F IGURE 18.15
The applet displays a sequence of images and plays audio.
The program is given in Listing 18.13.
L ISTING 18.13 FlagAnthem.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import java.applet.*;
5
6 public class FlagAnthem extends JApplet {
7
VideoNote
Audio and image
private final static int NUMBER_OF_NATIONS = 7 ;
8
private int current = 0 ;
9
private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS];
image icons
audio clips
current audio clip
10
11
12
13 private JLabel jlblImageLabel = new JLabel();
14 private JButton jbtResume = new JButton( "Resume" );
15 private JButton jbtSuspend = new JButton( "Suspend" );
16 private JComboBox jcboNations = new JComboBox( new Object[]
17 { "Denmark" , "Germany" , "China" , "India" , "Norway" , "UK" , "US" });
18
19 public FlagAnthem() {
20 // Load image icons and audio clips
21 for ( int i = 0 ; i < NUMBER_OF_NATIONS; i++) {
22 icons[i] = new ImageIcon(getClass().getResource(
23
private AudioClip[] audioClips = new AudioClip[NUMBER_OF_NATIONS];
private AudioClip currentAudioClip;
GUI components
create icons
"image/flag" + i + ".gif" ));
24
25
26 }
27
28 JPanel panel = new JPanel();
audioClips[i] = Applet.newAudioClip(
create audio clips
getClass().getResource( "audio/anthem" + i + ".mid" ));
create UI
 
 
Search WWH ::




Custom Search