Abstract Windows Toolkit – Sound, Images and GUI

 

In this part of the tutorial I will show how one can re-use code from another’s project to create a new Java creation.  I was surfing the Internet to find how event-based buttons might work on an applet to play a sound file.  After a couple of hours I found the following cool program from a website in France :

 

 

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

 

 

public class Frsound extends Applet implements ActionListener {

 

         AudioClip aburn;

         AudioClip sonar;

         Button baburn=new Button("Afterburn");

         Button bsonar=new Button("Sonar");

         boolean bruitFond=false;

 

public void init() {

          aburn =getAudioClip(getCodeBase(),"afterb.au");

          sonar =getAudioClip(getCodeBase(),"sonar.au");

          add(baburn);

          baburn.addActionListener(this);

          add(bsonar);

          bsonar.addActionListener(this);

         }

 

public void actionPerformed(ActionEvent e) {

          if (e.getSource() == baburn) aburn.play();

          else if (e.getSource() == bsonar) {

           if (bruitFond) {

            sonar.stop();

            bsonar.setLabel("DEPART");

            bruitFond=false;

           }

           else {

            sonar.loop();

            bsonar.setLabel("STOP");

            bruitFond=true;

           }

          }

         }

 

public void stop() {

          if (bruitFond) sonar.stop();

         }

        }

 

This brilliant little code snippet demonstrates how two instances of a Java button are to be encapsulated in the “Frsound” class to manage the playing of our audio files.   The code above implements “ActionListener” which invokes the event-based properties of windows like mouse move, keyboard input, mouse click and so on.   This feature greatly expanded the prospect of creating an interactive multimedia applet.  In addition, the code above introduces decision in Java and allows the user to select which sound to listen to.

 

Therefore, with the idea of reusable code and event-based programming techniques in mind, I decided to create the NewHalloween lab.  I wanted the Java novice to learn that there are all kinds of code snippets on the Internet and it’s up to the programmer to figure out how they might work in his or her program.   In the NewHalloween lab, I wanted the user to see a pumpkin in the default applet canvas and have the option to view another Halloween image file along with an appropriate audio file.  This involved multi-taking in Java and we needed to use threads, a topic that will be discussed in more detail when we look at the “EasterThread” lab in the section entitled “Threads”l.  In addition to the multi-tasking issue we needed to find a way to manage the appearance of the image files and the playing of audio files, discovering after some experimentation that an array of images and audio files was the best solution:

 

 

/*

******************************************************

*                                                                             *

*   St.Stephens  Secondary School                      *

*     Bowmanville, Ontario                                     *

*                                                                             *

*    ICS3M -  Computer and Information Science  *

*    Teacher -  Mr. K. McDaniel                              *

*                                                                                *

*                                                                              *

*                                                                               *

*    Lab #8 :  Halloween         Date:  Oct 31/2002  *

*                                                                               *

******************************************************

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

 

     public class NewHalloween extends Applet implements ActionListener, Runnable

{

 

//ARRAY CODE       

         Image[] images=new Image[3];

       

         AudioClip[] audioclip= new AudioClip[2];

        

         int frame= 2;

         int button= 2;

// Intro of buttons        

         Button bwitch=new Button("WITCH");

         Button bghost=new Button("GHOST");

         volatile Thread thread;

         

         public void init()

         {

// these are the picture files being used as well as the audioclips

          images[0] = getImage(getDocumentBase(),"flyingwitch.jpg");

          images[1] = getImage(getDocumentBase(),"ghost.jpg");

          images[2] = getImage(getDocumentBase(),"jack-o-lantern.jpg");

         

          audioclip[0] =getAudioClip(getCodeBase(),"mywitch.au");

          audioclip[1] =getAudioClip(getCodeBase(),"Lalala.au");

          // audioclip[2] =getAudioClip(getCodeBase(),"PATCH.AU");

         

         

          add(bwitch);

          bwitch.addActionListener(this);

          add(bghost);

          bghost.addActionListener(this);

         }

         public void start()

         {

            (thread = new Thread (this)).start();

         }

         public void stop()

         {

            thread = null;

         }

         public void paint (Graphics g)

         {

         g.drawImage(images[frame],50,50,300,300,this);

         audioclip[frame].play();

         button= 2;

      }

        

         public void actionPerformed(ActionEvent e)

         {

          if (e.getSource()==bwitch)

         {

            button= 0;

         }

          else if (e.getSource()==bghost)

         {

            button = 1;

         }

         }

 

         public void run()

         {

            int delay = 6000;

            try

            {

                        while (thread==Thread.currentThread())

                        {

// changes screen into the picture that was chosen and then repaints the original screen

                                    frame = button;

                                    repaint();

                                    Thread.sleep(delay);

                        }

            }

            catch (Exception e)

            {

            }

      }//end of NewHalloween program

}// Happy Halloween!!

     

        

Next, we will look at threads and how they can be added to your multimedia applets to create animation.