Question: This programming challenge is also easier than CH_11A. Here you will modify the JukeBox program (Listing 11.11) JukeBox.zip so that the user can select one
This programming challenge is also easier than CH_11A. Here you will modify the JukeBox program (Listing 11.11) JukeBox.zip so that the user can select one or the other of two .wav files to play. Both files are from operas and both are music associated with the arrival of guests. Specification: No more than 2 selections are to be available.
The first "entry of the guests music" is from Offenbach's 'Les contes d'Hoffmann' (English translation?????). It is played when guests arrive at the home of the scientist Spalanzani to see a demonstration of his latest invention, a robot. The robot Spalanzani has created is a human-like, life-size mechanical doll (Olympia).
The other music is actually called "Entrance of the Guests" or "Entry of the Guests" and is from the opera ???? by Richard Wagner. Here the "guests" arrive in the great Hall of the Warburg (a castle in Eisenach, Germany) for a singing contest.
When you create the combo box (see p. 565) you should think about how event processing in a Java GUI relates to polymorphism (chapter 10, p. 521) and see if there is an opportunity for the use of polymorphism, since both pieces of music are associated with the arrival of guests and could use methods or interfaces that have the same name. For 20 extra points name the 2 operas ( the ???? above) and the city and year where each had their premier. Put this information in the header annotation of your program.
//******************************************************************** // JukeBox.java Author: Lewis/Loftus // // Demonstrates the use of a combo box. //********************************************************************
import javax.swing.*;
public class JukeBox { //----------------------------------------------------------------- // Creates and displays the controls for a juke box. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Java Juke Box"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JukeBoxControls controlPanel = new JukeBoxControls();
frame.getContentPane().add(controlPanel); frame.pack(); frame.setVisible(true); } }
//******************************************************************** // JukeBoxControls.java Author: Lewis and Loftus // // Represents the control panel for the juke box. //********************************************************************
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.AudioClip; import java.net.URL;
public class JukeBoxControls extends JPanel { private JComboBox musicCombo; private JButton stopButton, playButton; private AudioClip[] music; private AudioClip current;
//----------------------------------------------------------------- // Sets up the GUI for the juke box. //----------------------------------------------------------------- public JukeBoxControls() { URL url1, url2, url3, url4, url5, url6; url1 = url2 = url3 = url4 = url5 = url6 = null;
// Obtain and store the audio clips to play try { url1 = new URL("file", "localhost", "westernBeat.wav"); url2 = new URL("file", "localhost", "classical.wav"); url3 = new URL("file", "localhost", "jeopardy.au"); url4 = new URL("file", "localhost", "newAgeRythm.wav"); url5 = new URL("file", "localhost", "eightiesJam.wav"); url6 = new URL("file", "localhost", "hitchcock.wav"); } catch (Exception exception) {}
music = new AudioClip[7]; music[0] = null; // Corresponds to "Make a Selection..." music[1] = JApplet.newAudioClip(url1); music[2] = JApplet.newAudioClip(url2); music[3] = JApplet.newAudioClip(url3); music[4] = JApplet.newAudioClip(url4); music[5] = JApplet.newAudioClip(url5); music[6] = JApplet.newAudioClip(url6);
JLabel titleLabel = new JLabel("Java Juke Box"); titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Create the list of strings for the combo box options String[] musicNames = {"Make A Selection...", "Western Beat", "Classical Melody", "Jeopardy Theme", "New Age Rythm", "Eighties Jam", "Alfred Hitchcock's Theme"};
musicCombo = new JComboBox(musicNames); musicCombo.setAlignmentX(Component.CENTER_ALIGNMENT);
// Set up the buttons playButton = new JButton("Play", new ImageIcon("play.gif")); playButton.setBackground(Color.white); playButton.setMnemonic('p'); stopButton = new JButton("Stop", new ImageIcon("stop.gif")); stopButton.setBackground(Color.white); stopButton.setMnemonic('s');
JPanel buttons = new JPanel(); buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); buttons.add(playButton); buttons.add(Box.createRigidArea(new Dimension(5,0))); buttons.add(stopButton); buttons.setBackground(Color.cyan);
// Set up this panel setPreferredSize(new Dimension(300, 100)); setBackground(Color.cyan); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(Box.createRigidArea(new Dimension(0,5))); add(titleLabel); add(Box.createRigidArea(new Dimension(0,5))); add(musicCombo); add(Box.createRigidArea(new Dimension(0,5))); add(buttons); add(Box.createRigidArea(new Dimension(0,5)));
musicCombo.addActionListener(new ComboListener()); stopButton.addActionListener(new ButtonListener()); playButton.addActionListener(new ButtonListener());
current = null; }
//***************************************************************** // Represents the action listener for the combo box. //***************************************************************** private class ComboListener implements ActionListener { //-------------------------------------------------------------- // Stops playing the current selection (if any) and resets // the current selection to the one chosen. //-------------------------------------------------------------- public void actionPerformed(ActionEvent event) { if (current != null) current.stop();
current = music[musicCombo.getSelectedIndex()]; } }
//***************************************************************** // Represents the action listener for both control buttons. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Stops the current selection (if any) in either case. If // the play button was pressed, start playing it again. //-------------------------------------------------------------- public void actionPerformed(ActionEvent event) { if (current != null) current.stop();
if (event.getSource() == playButton) if (current != null) current.play(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
