home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch09 / Guitar.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  2.4 KB  |  107 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4.  
  5. /*
  6.  * the applet class
  7.  */
  8. public class Guitar extends Applet implements ActionListener {
  9.  
  10. /*
  11.  * the guitar chords
  12.  */
  13. AudioClip chords[] = new AudioClip[7];
  14.  
  15. /*
  16.  * the drum clip
  17.  */
  18. AudioClip drum;
  19. String chordnames[] = {"a", "c", "d", "low_e", "high_e", "g", "chaa"};
  20. String drumbeat  = "drum";
  21. Button chordbuttons[] = new Button[7];
  22. Button btnStart;
  23. Button btnStop;
  24. /*
  25.  * Called when the applet is loaded
  26.  * Load all sounds and add user interface
  27.  */
  28. public void init () {
  29.  
  30.     String name;
  31.     int i;
  32.     Panel keyboard = new Panel();
  33.     Panel controls = new Panel();
  34.  
  35.     this.setLayout(new BorderLayout());
  36.     keyboard.setLayout(new FlowLayout());
  37.     controls.setLayout(new FlowLayout());
  38.     add ("Center", keyboard);
  39.     add ("South", controls);
  40.  
  41.     for (i=0; i<7; i+=1) {
  42.         name = chordnames[i]+".au";
  43.         showStatus ("Getting " + name);
  44.         chords[i] = getAudioClip (getCodeBase(), name);
  45.         chordbuttons[i] = new Button (chordnames[i]);
  46.            keyboard.add (chordbuttons[i]);
  47.         chordbuttons[i].addActionListener(this);
  48.     }
  49.     showStatus ("Getting " + drumbeat + ".au");
  50.     drum = getAudioClip (getCodeBase(), drumbeat + ".au");
  51.     btnStart = new Button ("Start");
  52.     controls.add (btnStart);
  53.     btnStop = new Button ("Stop");
  54.     controls.add (btnStop);
  55.     btnStop.addActionListener(this);
  56.     btnStart.addActionListener(this); 
  57. }
  58.  
  59. /*
  60.  * Handle button presses
  61.  * @param ev - event object
  62.  * @param arg - target object
  63.  */
  64. public void actionPerformed (ActionEvent ev)
  65. {
  66.     Object object1 = ev.getSource();
  67.     if (object1 == chordbuttons[0])
  68.     {
  69.         chords[0].play ();
  70.     }
  71.     else if (object1 == chordbuttons[1])
  72.     {
  73.         chords[1].play ();
  74.     }
  75.     else if (object1 == chordbuttons[2])
  76.     {
  77.         chords[2].play ();
  78.     }
  79.     else if (object1 == chordbuttons[3])
  80.     {
  81.         chords[3].play ();
  82.     }
  83.     else if (object1 == chordbuttons[4])
  84.     {
  85.         chords[4].play ();
  86.     }
  87.     else if (object1 == chordbuttons[5])
  88.     {
  89.         chords[5].play ();
  90.     }
  91.     else if (object1 == chordbuttons[6])
  92.     {
  93.         chords[6].play ();
  94.     }
  95.         else if (object1 == btnStart) {
  96.             drum.loop ();
  97.     }
  98.     else if (object1 == btnStop)
  99.      {
  100.             drum.stop ();
  101.      }
  102. }
  103. }
  104.  
  105.  
  106.  
  107.