import java.applet.Applet; import java.applet.*; import java.awt.*; import java.awt.Color; import java.net.URL; import java.awt.event.*; /**User defined class (ImageAndSound) extended from the Applet class to implements musics and images. Also, the methods of the following interfaces (ActionListener, AdjustmentListener, ItemListener) are defined in the ImageAndSound class */ public class ImageAndSound extends Applet implements ActionListener, AdjustmentListener, ItemListener { private int INIT_Y = 50; private int PACE = 40; // adjustable by scrollbar 'speed' private Image image[]; private int totalImage = 2; private int x_pos = 1, // x position of the image y_pos = INIT_Y, // y position of the image sleepTime = 40; // milliseconds to sleep private AudioClip music; private Button b_play_sound; private Button b_stop_resume; private Choice c_music_list; //droplist for choosing music private Scrollbar speed; //scrollbar used for controlling the speed of movement of the image private Label l_speed; private boolean tag = false; //tag for toggle two images private boolean interrupted = false; //indicate if thread if interrupted Panel soundPanel; Panel speedPanel; private work thread; private URL myurl; /**This method is called after the applet instance is first created by the browser. This will initialize the user interface panel for the applet and display it on the browser */ public void init() { //Returns the URL of my applet. myurl = getCodeBase(); //for debug use. message can be found in the Java console System.out.println("myurl: " + myurl); //set the applet layout this.setBackground(new Color(175, 160, 105)); setLayout(new BorderLayout()); b_play_sound = new Button("Play Music"); b_stop_resume = new Button(" Stop "); //Register action listener for the play button b_play_sound.addActionListener(this); //Register action listener for the Stop button b_stop_resume.addActionListener(this); // initialize music list c_music_list = new Choice(); c_music_list.add("Space Music"); c_music_list.add("Yahoo!"); c_music_list.add("Chirp"); c_music_list.add("Hi"); c_music_list.select(0); music = getAudioClip(myurl, "Sound/" + "spacemusic.au"); //Register action listener for the drop down listbox c_music_list.addItemListener(this); // set the layout of soundPanel soundPanel = new Panel(new FlowLayout()); soundPanel.setBackground(Color.lightGray); soundPanel.setSize(433, 50); soundPanel.add(c_music_list); soundPanel.add(b_play_sound); soundPanel.add(b_stop_resume); // set the layout of the speed scrollbar l_speed = new Label("Speed of Animation "); l_speed.setSize(200, 15); speed = new Scrollbar(Scrollbar.HORIZONTAL, PACE, 10, 0, 600); speed.setSize(300, 24); speed.setUnitIncrement(30); //Register action listener for scrollbar speed.addAdjustmentListener(this); // set the layout of the speedPanel speedPanel = new Panel(new BorderLayout()); speedPanel.setBackground(Color.lightGray); speedPanel.add("West", l_speed); speedPanel.add("Center", speed); // add soundPanel and speedPanel to applet add("North", soundPanel); add("South", speedPanel); //get images image = new Image[totalImage]; for (int i=0; i 490) { x_pos = 1; if (y_pos > 180 ) y_pos = INIT_Y; else y_pos += 20; } else x_pos += PACE; } /**Implements the method from the ActionListener Interface This listerner applies to buttons and actionperfomed is invoked when the user clicks on the button. @param ActionEvent Object */ public void actionPerformed(ActionEvent ae) { Object obj= ae.getSource(); System.out.println("Action Performed, source = " + obj.toString()); if ( obj == b_play_sound) music.play(); if (obj == b_stop_resume) { if (interrupted) { b_stop_resume.setLabel("Stop "); interrupted = false; // new instant of work to resume moving images thread = new work(this); thread.start(); } else { b_stop_resume.setLabel("Resume "); this.stop(); } } } /**Implements the method for AdjustmentListener interface This listener applies to scrollbars and adjustmentvaluechanged is invoked when the scrollbar is moved. Therefore, this method will adjust the pace of the multimedia image @param AdjustmentEvent Object */ public void adjustmentValueChanged( AdjustmentEvent event) { // adjust PACE based on the speed scrollbar if (event.getSource() == speed) PACE = speed.getValue(); } /**Implements the method for ItemListener interface. This listerner applies to List box and is invoked when an item is changed. Therefore, this method will capture the selected song. @param ItemEvent Object */ public void itemStateChanged(ItemEvent ie) { System.out.println("itemStateChanged: " + ie.getSource()); if (ie.getSource() == c_music_list) { // for debug use only System.out.println("c_music_list.getSelectedIndex() = " + c_music_list.getSelectedIndex()); System.out.println("c_music_list.getSelectedItem() = " + c_music_list.getSelectedItem()); switch (c_music_list.getSelectedIndex()) { case 0: music = getAudioClip(myurl, "Sound/" + "spacemusic.au"); break; case 1: music = getAudioClip(myurl, "Sound/" + "yahoo1.au"); break; case 2: music = getAudioClip(myurl, "Sound/" + "chirp.au"); break; case 3: music = getAudioClip(myurl, "Sound/" + "hi.au"); break; } System.out.println("music: " + music.toString()); } } /** Method to determine if the process is interrupted */ public boolean is_interrupted() { System.out.println("is_interrupted called: " + interrupted); return interrupted; } }