19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18.12 Case Study: National Flags and Anthems 695<br />

18.12 Case Study: National Flags and Anthems<br />

This case study presents an applet that displays a nation’s flag and plays its anthem.<br />

The images in the applet are for seven national flags, named flag0.gif, flag1.gif, . . ., flag6.gif<br />

for Denmark, Germany, China, India, Norway, the U.K., and the U.S. They are s<strong>to</strong>red under<br />

the image direc<strong>to</strong>ry in the class path. The audio consists of national anthems for these seven<br />

nations, named anthem0.mid, anthem1.mid, . . ., and anthem6.mid. They are s<strong>to</strong>red under<br />

the audio direc<strong>to</strong>ry in the class path.<br />

The program enables the user <strong>to</strong> select a nation from a <strong>com</strong>bo box and then displays its flag<br />

and plays its anthem. The user can suspend the audio by clicking the Suspend but<strong>to</strong>n and<br />

resume it by clicking the Resume but<strong>to</strong>n, as shown in Figure 18.15.<br />

Key<br />

Point<br />

FIGURE 18.15<br />

The applet displays a sequence of images and plays audio.<br />

The program is given in Listing 18.13.<br />

LISTING 18.13<br />

FlagAnthem.java<br />

1 import java.awt.*;<br />

2 import java.awt.event.*;<br />

3 import javax.swing.*;<br />

4 import java.applet.*;<br />

5<br />

6 public class FlagAnthem extends JApplet {<br />

7 private final static int NUMBER_OF_NATIONS = 7;<br />

8 private int current = 0;<br />

9 private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS];<br />

10 private AudioClip[] audioClips = new AudioClip[NUMBER_OF_NATIONS];<br />

11 private AudioClip currentAudioClip;<br />

12<br />

13 private JLabel jlblImageLabel = new JLabel();<br />

14 private JBut<strong>to</strong>n jbtResume = new JBut<strong>to</strong>n("Resume");<br />

15 private JBut<strong>to</strong>n jbtSuspend = new JBut<strong>to</strong>n("Suspend");<br />

16 private JComboBox jcboNations = new JComboBox(new Object[]<br />

17 {"Denmark", "Germany", "China", "India", "Norway", "UK", "US"});<br />

18<br />

19 public FlagAnthem() {<br />

20 // Load image icons and audio clips<br />

21 for (int i = 0; i < NUMBER_OF_NATIONS; i++) {<br />

22 icons[i] = new ImageIcon(getClass().getResource(<br />

23 "image/flag" + i + ".gif"));<br />

24 audioClips[i] = Applet.newAudioClip(<br />

25<br />

getClass().getResource("audio/anthem" + i + ".mid"));<br />

26 }<br />

27<br />

28 JPanel panel = new JPanel();<br />

VideoNote<br />

Audio and image<br />

image icons<br />

audio clips<br />

current audio clip<br />

GUI <strong>com</strong>ponents<br />

create icons<br />

create audio clips<br />

create UI

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!