Java Example 1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MBC
*/
public class ImageAudioAnimation extends JApplet
{
private final static int NUMBER_OF_NATIONS = 7;
private int current = 0;
private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS];
private AudioClip[] audioClips = new AudioClip[NUMBER_OF_NATIONS];
private AudioClip currentAudioClip;
private int[] delays =
{48000, 54000, 59000, 54000, 59000, 31000, 68000};
private Timer timer = new Timer(delays[0], new TimerListener());
private JLabel jlblImageLabel = new JLabel();
private JButton jbtResume = new JButton("resume");
private JButton jbtSuspend = new JButton("Suspend");
private JComboBox jcbNations = new JComboBox(new Object[]
{"Denmark", "Germany", "China", "India", "Norway", "UK", "US"})
;
public ImageAudioAnimation()
{
for (int i = 0; i < NUMBER_OF_NATIONS; i++) {
icons[i] = new ImageIcon(getClass().getResource(
"flag" + i + ".gif"));
audioClips[i] = Applet.newAudioClip(
getClass().getResource("anthem" + i + ".mid"));
}
JPanel panel = new JPanel();
panel.add(jbtResume);
panel.add(jbtSuspend);
panel.add(new JLabel("Select"));
panel.add(jcbNations);
add(jlblImageLabel, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
jbtResume.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
start();
}
});
jbtSuspend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
}
});
jcbNations.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
current = jcbNations.getSelectedIndex();
presentNation(current);
timer.start();
}
});
timer.start();
jlblImageLabel.setIcon(icons[0]);
jlblImageLabel.setHorizontalAlignment(JLabel.CENTER);
currentAudioClip = audioClips[0];
currentAudioClip.play();
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
current = (current + 1) % NUMBER_OF_NATIONS;
presentNation(current);
}
}
private void presentNation(int index)
{
jlblImageLabel.setIcon(icons[index]);
jcbNations.setSelectedIndex(index);
currentAudioClip = audioClips[index];
currentAudioClip.play();
timer.setDelay(delays[index]);
}
public void start()
{
timer.start();
currentAudioClip.play();
}
public void stop()
{
timer.stop();
currentAudioClip.stop();
}
public static void main(String[] args)
{
// Create a frame
JFrame frame = new JFrame("ImageAudioAnimation");
// Create an instance of the applet
ImageAudioAnimation applet = new ImageAudioAnimation();
applet.init();
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Display the frame
frame.setSize(400, 300);
frame.setVisible(true);
}
}