Java Reference
In-Depth Information
2. Add a method called setAudioClip() that has a java.net.URL param-
eter. This URL represents the location of an audio clip. Within the
setAudioClip() method, use the getAudioClip() method of the
AppletContext to initialize the field clip. Use the getFile() method of
the URL class to initialize the clipName field.
3. Add the start() and stop() methods to the PlayClipApplet class,
having them play and stop the audio clip, respectively.
4. Within the start() method, use the showStatus() method of the
AppletContext interface to display the text “Playing clip_name ”,
where clip_name is the value of the field clipName.
5. Save and compile the PlayClipApplet class.
6. Write a class named EnterASongApplet that extends JApplet. Add
two JButton fields—play and stop—and a JTextField field called
songName. Also add a field of type AppletContext.
7. Within the init() method, initialize the three component fields. Use
the labels Play and Stop for the buttons. Add the two buttons and
text field to the content pane of this JApplet, laying them out in a
nice fashion.
8. Also within the init() method, initialize the AppletContext field,
then instantiate a new PlayListener object, passing a reference to the
songName text field and the AppletContext field. (You will write the
PlayListener class next.) Register the PlayListener object with both
buttons.
9. Save your EnterASongApplet class, and write a new class named
PlayListener that implements ActionListener.
10. Add a field of type JTextField and a field of type PlayClipApplet.
11. Add a constructor that has a JTextField parameter and an AppletCon-
text parameter. Initialize the JTextField field with the corresponding
parameter. Use the getApplet() method, passing in “playclip”, of the
given AppletContext to initialize the PlayClipApplet field. You will
need to cast the return value to the appropriate data type.
12. Within actionPerformed(), determine which button was clicked. If the
Play button is clicked, get the text from the JTextField, which will be
the name of an audio file, and use it to create a new URL object. Pass
in this URL object to the setAudioClip() method of the PlayClipAp-
plet field, then invoke the start() method on the PlayClipApplet field.
13. If the Stop button is pressed, invoke the stop() method on the Play-
ClipApplet field.
Search WWH ::




Custom Search