16.11. (Optional) Case Study: Multimedia Animations |
This case study presents a multimedia animation with images and audio. The images are for seven national flags, named flag0.gif , flag1.gif , flag6.gif for Denmark, Germany, China, India, Norway, the UK, and the US. 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 presents the nations, starting from the first one. For each nation, it displays its flag and plays its anthem. When the audio finishes, the next nation is presented, and so on. When the last nation is presented, the program starts to present all the nations again. You may suspend animation by clicking the Suspend button and resume it by clicking the Resume button, as shown in Figure 16.16. You can also directly select a nation from a combo box.
The program is given in Listing 16.13. A timer is created to control the animation (line 15). The timer delay for each presentation is the play time for the anthem. You can find the play time for an audio file using RealPlayer or Windows Media on Windows. The delay times are stored in an array named delays (lines 13 “14). The delay time for the first audio file (the Danish anthem) is 48 seconds.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import java.applet.*; 5 6 public class ImageAudioAnimation extends JApplet { 7 private final static int NUMBER_OF_NATIONS = 7 ; 8 private int current = ; 9 private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS]; 10 private AudioClip[] audioClips = new AudioClip[NUMBER_OF_NATIONS]; 11 private AudioClip currentAudioClip; 12 13 private int [] delays = 14 { 48000 , 54000 , 59000 , 54000 , 59000 , 31000 , 68000 }; 15 private Timer timer = new Timer(delays[ ], new TimerListener()); 16 17 private JLabel jlblImageLabel = new JLabel(); 18 private JButton jbtResume = new JButton( "Resume" ); 19 private JButton jbtSuspend = new JButton( "Suspend" ); 20 private JComboBox jcboNations = new JComboBox( new Object[] 21 { "Denmark" , "Germany" , "China" , "India" , "Norway" , "UK" , "US" }); 22 23 public ImageAudioAnimation() { 24 // Load image icons and audio clips 25 for ( int i = ; i < NUMBER_OF_NATIONS; i++) { 26 icons[i] = new ImageIcon(getClass().getResource( 27 "image/flag" + i + ".gif" )); 28 audioClips[i] = Applet.newAudioClip( 29 getClass().getResource( "audio/anthem" + i + ".mid" )); 30 } 31 32 JPanel panel = new JPanel(); 33 panel.add(jbtResume); 34 panel.add(jbtSuspend); 35 panel.add( new JLabel( "Select" )); 36 panel.add(jcboNations); 37 this .getContentPane().add(jlblImageLabel, BorderLayout.CENTER); 38 this .getContentPane().add(panel, BorderLayout.SOUTH); 39 40 jbtResume.addActionListener( new ActionListener() { 41 public void actionPerformed(ActionEvent e) { 42 start(); 43 } 44 }); 45 jbtSuspend.addActionListener( new ActionListener() { 46 public void actionPerformed(ActionEvent e) { 47 stop(); 48 } 49 }); 50 jcboNations.addActionListener( new ActionListener() { 51 public void actionPerformed(ActionEvent e) { 52 stop(); 53 current = jcboNations.getSelectedIndex(); 54 presentNation(current); 55 timer.start(); 56 } 57 }); 58 59 timer.start(); 60 jlblImageLabel.setIcon(icons[ ]); 61 jlblImageLabel.setHorizontalAlignment(JLabel.CENTER); 62 currentAudioClip = audioClips[ ]; 63 currentAudioClip.play(); 64 } 65 66 private class TimerListener implements ActionListener { 67 public void actionPerformed(ActionEvent e) { 68 current = (current + 1 ) % NUMBER_OF_NATIONS; 69 presentNation(current); 70 } 71 } 72 73 private void presentNation( int index) { 74 jlblImageLabel.setIcon(icons[index]); 75 jcboNations.setSelectedIndex(index); 76 currentAudioClip = audioClips[index]; 77 currentAudioClip.play(); 78 timer.setDelay(delays[index]); 79 } 80 81 public void start() { 82 timer.start(); 83 currentAudioClip.play(); 84 } 85 86 public void stop() { 87 timer.stop(); 88 currentAudioClip.stop(); 89 } 90 } |
A label is created in line 17 to display a flag image. An array of flag images for seven nations is created in lines 26 “27. An array of audio clips is created in lines 28 “29. Each audio clip is created for an audio file through the URL of the current class. The audio files are stored in the same directory with the applet class file.
The combo box for country names is created in lines 20 “21. When a new country name in the combo box is selected, the current presentation is stopped and a new selected nation is presented (line 52 “55).
The presentNation(index) method (lines 73 “79) presents a nation with the specified index. It sets a new image in the label (line 74), synchronizes with the combo box by setting the selected index (line 75), plays the new audio, and sets a new delay time (line 78).
The applet's start and stop methods are overridden to resume and suspend the animation (lines 81 “89).