CS116      Lab 8 - Graphical User Interfaces

Objectives:

  1. Open DrJava
     
  2. Open up the source code for the class SoundPlayer.java and try running this application. 
    Let's take a closer look at this class which defines a GUI:

    The SoundPlayer class extends the JPanel class.  A JPanel is simply a container for various widgets like buttons, labels, etc.

    The class contains instance variables which include JButtons, JLabel, and JPanel.  These widgets are defined in the javax.swing package..  Check out the Java API documentation and see what other widgets are available. 

    In the SoundPlayer constructor these widgets are added to the panel.  Notice that we can control how things get arranged to a certain degree by using a Layout.  SoundPlayer uses a BorderLayout.  Other layouts include a FlowLayout, and a GridLayout.  You can read about these layouts in the Java API documentation.  Also notice that the buttons are all placed in another JPanel and then the JPanel is added to the SoundPlayer panel.  This allows us to again control how things get placed to some degree.

     
  3. When we press on a button it produces an event.  Different types of widgets may produce different types of events.  Buttons produce action events.  In order for our program to be able to do something when a button is pressed, we have to direct it to "listen" for action events.   There are three steps involved with this.  First we specify that the class implements an ActionListener in the class definition at the top.  Secondly, we register each button in the constructor to say that the button's action events will be handled by this listener.  Thirdly, we must include a method called actionPerformed.  This method will be executed every time an action event is heard. 
     

  4. The actionPerformed method has an ActionEvent e as a parameter.  We can check to see which button produced that event by using e.getSource().  In this case the method is checking to see if the open button or the play button was pressed and then either creates a new Sound object from the provided file, or plays the sound.
     


  5.  

    Assignment:
    Add two more buttons to the SoundPlayer:  A button which will cause the sound to play with an echo, and a button which will cause the sound to play in reverse.  These buttons should not modify the original sound but only play the altered sound.

    Hints:  Modify the reverse method on p305 so that it returns a new reversed sound which you can play.  Modify the echo method on p316 to return and echoed sound.  Use any appropriate delay value, such as 10000, for this method. 


     

  6. A JSlider is a widget that produces change events when you move the slider.  The constructor for a slider can take two integer parameters to specify the low and high values on the slider bar.  To use change events you must import javax.swing.event.* and have your class implement ChangeListener.  Then the class must have a method
        public void stateChanged(ChangeEvent e)
    which is called whenever the slider is changed. The JSlider method getValue() will return the new value of the slider.

     
    Assignment:
    Add a slider to the SoundPlayer to control the volume.  You will want to use the setVolume method that you have written previously to set the new volume of your sound.


     

  7. Send your modified Sound.java and SoundPlayer.java  to me using GoucherLearn.