19.09.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

696 Chapter 18 Applets and Multimedia<br />

register listener<br />

start audio<br />

register listener<br />

s<strong>to</strong>p audio<br />

register listener<br />

select a nation<br />

present a nation<br />

play a clip<br />

s<strong>to</strong>p audio clip<br />

main method omitted<br />

29 panel.add(jbtResume);<br />

30 panel.add(jbtSuspend);<br />

31 panel.add(new JLabel("Select"));<br />

32 panel.add(jcboNations);<br />

33 add(jlblImageLabel, BorderLayout.CENTER);<br />

34 add(panel, BorderLayout.SOUTH);<br />

35<br />

36 jbtResume.addActionListener(new ActionListener() {<br />

37 @Override<br />

38 public void actionPerformed(ActionEvent e) {<br />

39<br />

40<br />

start();<br />

}<br />

41 });<br />

42 jbtSuspend.addActionListener(new ActionListener() {<br />

43 @Override<br />

44 public void actionPerformed(ActionEvent e) {<br />

45<br />

46<br />

s<strong>to</strong>p();<br />

}<br />

47 });<br />

48 jcboNations.addActionListener(new ActionListener() {<br />

49 @Override<br />

50 public void actionPerformed(ActionEvent e) {<br />

51 s<strong>to</strong>p();<br />

52 current = jcboNations.getSelectedIndex();<br />

53 presentNation(current);<br />

54 }<br />

55 });<br />

56<br />

57 jlblImageLabel.setIcon(icons[0]);<br />

58 jlblImageLabel.setHorizontalAlignment(JLabel.CENTER);<br />

59<br />

60<br />

currentAudioClip = audioClips[0];<br />

currentAudioClip.play();<br />

61 }<br />

62<br />

63 private void presentNation(int index) {<br />

64 jlblImageLabel.setIcon(icons[index]);<br />

65 jcboNations.setSelectedIndex(index);<br />

66 currentAudioClip = audioClips[index];<br />

67<br />

68<br />

currentAudioClip.play();<br />

}<br />

69<br />

70 @Override<br />

71 public void start() {<br />

72<br />

73<br />

currentAudioClip.play();<br />

}<br />

74<br />

75 @Override<br />

76 public void s<strong>to</strong>p() {<br />

77<br />

78<br />

currentAudioClip.s<strong>to</strong>p();<br />

}<br />

79 }<br />

A label is created in line 13 <strong>to</strong> display a flag image. An array of flag images for seven nations<br />

is created in lines 22–23. An array of audio clips is created in lines 24–25. The image files and<br />

audio files are s<strong>to</strong>red in the same direc<strong>to</strong>ry as the applet class file so these files can be located<br />

using the getResource method.<br />

The <strong>com</strong>bo box for country names is created in lines 16–17. When a new country name in<br />

the <strong>com</strong>bo box is selected, the current presentation is s<strong>to</strong>pped and a new selected nation is<br />

presented (lines 51–53).

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

Saved successfully!

Ooh no, something went wrong!