Bnt
December 29th, 2003, 05:22 AM
import java.awt.*;
import java.applet.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
public class Week9Excercise1 extends Applet implements ActionListener
{
private Panel Title, Genre, SongList, PlayControl;
private String Gen = "",P1 = "", P2 = "", P3 = "";
private String[] musicNames;
private Graphics g;
private AudioClip[] music;
private int currentInt = 0;
private JComboBox Song;
private Button Pop, Dance, House,Garage, Play, Stop, Spacer;
//-----------------------------------------------------------------
// Sets up the GUI for the juke box.
//-----------------------------------------------------------------
public void init()
{
URL url1, url2, url3, url4, url5, url6, url7, url8, url9, url10, url11, url12;
url1 = url2 = url3 = url4 = url5 = url6 = url7 = url8 = url9 = url10 = url11 = url12 = null;
// Obtain and store the audio clips to play
try
{
url1 = new URL ("file", "localhost", "1.wav");
url2 = new URL ("file", "localhost", "2.wav");
url3 = new URL ("file", "localhost", "3.wav");
url4 = new URL ("file", "localhost", "4.wav");
url5 = new URL ("file", "localhost", "5.wav");
url6 = new URL ("file", "localhost", "6.wav");
url7 = new URL ("file", "localhost", "7.wav");
url8 = new URL ("file", "localhost", "8.wav");
url9 = new URL ("file", "localhost", "9.wav");
url10 = new URL ("file", "localhost", "10.wav");
url11 = new URL ("file", "localhost", "11.wav");
url12 = new URL ("file", "localhost", "12.wav");
}
catch (MalformedURLException error)
{
getAppletContext().showStatus("Bad URL: "+ currentInt);
return;
}
music = new AudioClip[13];
music[0] = null; // Corresponds to "Make a Selection..."
music[1] = Applet.newAudioClip (url1);
music[2] = Applet.newAudioClip (url2);
music[3] = Applet.newAudioClip (url3);
music[4] = Applet.newAudioClip (url4);
music[5] = Applet.newAudioClip (url5);
music[6] = Applet.newAudioClip (url6);
music[7] = Applet.newAudioClip (url7);
music[8] = Applet.newAudioClip (url8);
music[9] = Applet.newAudioClip (url9);
music[10] = Applet.newAudioClip (url10);
music[11] = Applet.newAudioClip (url11);
music[12] = Applet.newAudioClip (url12);
Title = new Panel();
Genre = new Panel();
SongList = new Panel();
PlayControl = new Panel();
Genre.setLayout(new GridLayout(8,1,10,10));
PlayControl.setLayout(new GridLayout(2,8,20,10));
// Creates the list of strings for the combo box options
musicNames = new String[] {"Make A Selection...", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
JLabel titleLabel = new JLabel ("Java Juke Box");
titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
Song = new JComboBox (musicNames);
setLayout (new BorderLayout(10,10));
this.setBackground(Color.black);
Song.addItemListener(new ComboListener());
// Set up the buttons
Pop = new Button("Pop");
Pop.addActionListener(this);
Dance = new Button("Dance");
Dance.addActionListener(this);
House = new Button("House");
House.addActionListener(this);
Garage = new Button("Garage");
Garage.addActionListener(this);
Play = new Button("Play");
Play.addActionListener(this);
Stop = new Button("Stop");
Spacer = new Button("Spacer");
Spacer.setVisible(false);
Play.setBackground(Color.green);
Stop.setBackground(Color.red);
Stop.addActionListener(this);
Title.setBackground(Color.black);
Genre.setBackground(Color.black);
SongList.setBackground(Color.black);
PlayControl.setBackground(Color.black);
Genre.add(Pop);
Genre.add(Dance);
Genre.add(House);
Genre.add(Garage);
PlayControl.add(Song);
PlayControl.add(Play);
PlayControl.add(Spacer);
PlayControl.add(Stop);
add("North",Title);
add("South",PlayControl);
add("East",SongList);
add("West",Genre);
}
private class ComboListener implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==Song)
{
if (currentInt != 0)
{
music[currentInt].stop();
}
currentInt = Song.getSelectedIndex();
}
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Stop)
{
music[currentInt].stop();
}
if(ae.getSource()==Play)
{
music[currentInt].stop();
music[currentInt].play();
}
if(ae.getSource()==Pop)
{
Gen = "Pop";
P1 = "1. Alex Parks - Maybe That's What It Takes";
P2 = "2. Pop Song 2";
P3 = "3. Pop Song 3";
repaint();
}
if(ae.getSource()==Dance)
{
Gen = "Dance";
P1 = "4. Dance Song 1";
P2 = "5. Dance Song 2";
P3 = "6. Dance Song 3";
repaint();
}
if(ae.getSource()==House)
{
Gen = "House";
P1 = "7. House Song 1";
P2 = "8. House Song 2";
P3 = "9. House Song 3";
repaint();
}
if(ae.getSource()==Garage)
{
Gen = "Garage";
P1 = "10. Garage Song 1";
P2 = "11. Garage Song 2";
P3 = "12. Garage Song 3";
repaint();
}
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(-70,-70,300,300);
g.setColor(Color.white);
g.drawString("Song List - " + currentInt, 60,30);
g.drawString(" "+P1,90,65);
g.drawString(" "+P2,90,85);
g.drawString(" "+P3,90,105);
}
}
import java.applet.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
public class Week9Excercise1 extends Applet implements ActionListener
{
private Panel Title, Genre, SongList, PlayControl;
private String Gen = "",P1 = "", P2 = "", P3 = "";
private String[] musicNames;
private Graphics g;
private AudioClip[] music;
private int currentInt = 0;
private JComboBox Song;
private Button Pop, Dance, House,Garage, Play, Stop, Spacer;
//-----------------------------------------------------------------
// Sets up the GUI for the juke box.
//-----------------------------------------------------------------
public void init()
{
URL url1, url2, url3, url4, url5, url6, url7, url8, url9, url10, url11, url12;
url1 = url2 = url3 = url4 = url5 = url6 = url7 = url8 = url9 = url10 = url11 = url12 = null;
// Obtain and store the audio clips to play
try
{
url1 = new URL ("file", "localhost", "1.wav");
url2 = new URL ("file", "localhost", "2.wav");
url3 = new URL ("file", "localhost", "3.wav");
url4 = new URL ("file", "localhost", "4.wav");
url5 = new URL ("file", "localhost", "5.wav");
url6 = new URL ("file", "localhost", "6.wav");
url7 = new URL ("file", "localhost", "7.wav");
url8 = new URL ("file", "localhost", "8.wav");
url9 = new URL ("file", "localhost", "9.wav");
url10 = new URL ("file", "localhost", "10.wav");
url11 = new URL ("file", "localhost", "11.wav");
url12 = new URL ("file", "localhost", "12.wav");
}
catch (MalformedURLException error)
{
getAppletContext().showStatus("Bad URL: "+ currentInt);
return;
}
music = new AudioClip[13];
music[0] = null; // Corresponds to "Make a Selection..."
music[1] = Applet.newAudioClip (url1);
music[2] = Applet.newAudioClip (url2);
music[3] = Applet.newAudioClip (url3);
music[4] = Applet.newAudioClip (url4);
music[5] = Applet.newAudioClip (url5);
music[6] = Applet.newAudioClip (url6);
music[7] = Applet.newAudioClip (url7);
music[8] = Applet.newAudioClip (url8);
music[9] = Applet.newAudioClip (url9);
music[10] = Applet.newAudioClip (url10);
music[11] = Applet.newAudioClip (url11);
music[12] = Applet.newAudioClip (url12);
Title = new Panel();
Genre = new Panel();
SongList = new Panel();
PlayControl = new Panel();
Genre.setLayout(new GridLayout(8,1,10,10));
PlayControl.setLayout(new GridLayout(2,8,20,10));
// Creates the list of strings for the combo box options
musicNames = new String[] {"Make A Selection...", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
JLabel titleLabel = new JLabel ("Java Juke Box");
titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
Song = new JComboBox (musicNames);
setLayout (new BorderLayout(10,10));
this.setBackground(Color.black);
Song.addItemListener(new ComboListener());
// Set up the buttons
Pop = new Button("Pop");
Pop.addActionListener(this);
Dance = new Button("Dance");
Dance.addActionListener(this);
House = new Button("House");
House.addActionListener(this);
Garage = new Button("Garage");
Garage.addActionListener(this);
Play = new Button("Play");
Play.addActionListener(this);
Stop = new Button("Stop");
Spacer = new Button("Spacer");
Spacer.setVisible(false);
Play.setBackground(Color.green);
Stop.setBackground(Color.red);
Stop.addActionListener(this);
Title.setBackground(Color.black);
Genre.setBackground(Color.black);
SongList.setBackground(Color.black);
PlayControl.setBackground(Color.black);
Genre.add(Pop);
Genre.add(Dance);
Genre.add(House);
Genre.add(Garage);
PlayControl.add(Song);
PlayControl.add(Play);
PlayControl.add(Spacer);
PlayControl.add(Stop);
add("North",Title);
add("South",PlayControl);
add("East",SongList);
add("West",Genre);
}
private class ComboListener implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==Song)
{
if (currentInt != 0)
{
music[currentInt].stop();
}
currentInt = Song.getSelectedIndex();
}
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Stop)
{
music[currentInt].stop();
}
if(ae.getSource()==Play)
{
music[currentInt].stop();
music[currentInt].play();
}
if(ae.getSource()==Pop)
{
Gen = "Pop";
P1 = "1. Alex Parks - Maybe That's What It Takes";
P2 = "2. Pop Song 2";
P3 = "3. Pop Song 3";
repaint();
}
if(ae.getSource()==Dance)
{
Gen = "Dance";
P1 = "4. Dance Song 1";
P2 = "5. Dance Song 2";
P3 = "6. Dance Song 3";
repaint();
}
if(ae.getSource()==House)
{
Gen = "House";
P1 = "7. House Song 1";
P2 = "8. House Song 2";
P3 = "9. House Song 3";
repaint();
}
if(ae.getSource()==Garage)
{
Gen = "Garage";
P1 = "10. Garage Song 1";
P2 = "11. Garage Song 2";
P3 = "12. Garage Song 3";
repaint();
}
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(-70,-70,300,300);
g.setColor(Color.white);
g.drawString("Song List - " + currentInt, 60,30);
g.drawString(" "+P1,90,65);
g.drawString(" "+P2,90,85);
g.drawString(" "+P3,90,105);
}
}