//sound
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

public class sound extends Applet implements ActionListener,ItemListener
{
	JFrame f=new JFrame("sound");
	JButton BtnWelcome=new JButton("welcome");  //按了發出歡迎聲
	JButton BtnReceive=new JButton("receive");  //按了發出收到水球聲
	JButton BtnLeave=new JButton("leave");      //按了發出收到離開聲
	JLabel lblMusic=new JLabel("請選擇背景音樂");  //JLabel
	Choice MusicMenu=new Choice();                 //歌曲選擇單
	AudioClip voiceGet,song;                       //定義兩種撥放源
	URL urlWelcome,urlReceive,urlLeave;
	URL urlOne,urlTwo,urlThree,urlFour,urlFive,urlSix,urlSeven,urlEight; //定義歌曲來源
	//One=卡農，cl6_18   Two=鱒魚，ac3cl_18  Three=土耳其進行曲，ac3cl_1
	//Four=阿萊城姑娘，cl5_19  Five=仙劍奇俠傳，ct11_17
	//Six=命中註定，ch45_6  Seven=新不了情，ch46_18
	//Eight=還是會寂寞，ch32_7
	String s;                                     //歌曲名稱，用以判斷目前選項
	int record=0;                                 //歌曲代號，用以判斷目前選項
		
	sound()
	{
		f.getContentPane().setLayout(null);
		f.setBounds(50,50,400,400);
		BtnWelcome.setBounds(200,50,100,100);     //擺設位置
		BtnReceive.setBounds(200,150,100,100);
		BtnLeave.setBounds(200,250,100,100);
		lblMusic.setBounds(0,50,100,50);
		MusicMenu.setBounds(0,100,100,25);
		
		f.addWindowListener(
			new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
			}
		);
		
		MusicMenu.add("請選擇歌曲");           //加歌曲
		MusicMenu.add("卡農");
		MusicMenu.add("鱒魚");
		MusicMenu.add("土耳其進行曲");
		MusicMenu.add("阿萊城姑娘");
		MusicMenu.add("仙劍奇俠傳");
		MusicMenu.add("命中註定");
		MusicMenu.add("新不了情");
		MusicMenu.add("還是會寂寞");
		MusicMenu.add("關掉");
		MusicMenu.addItemListener(this);
		
		
		BtnWelcome.addActionListener(this);
		BtnReceive.addActionListener(this);
		BtnLeave.addActionListener(this);
		
		f.getContentPane().add(BtnWelcome);    //擺入
		f.getContentPane().add(BtnReceive);
		f.getContentPane().add(BtnLeave);
		f.getContentPane().add(lblMusic);
		f.getContentPane().add(MusicMenu);
		
		f.show();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		if(e.getSource()==BtnWelcome) //回應歡迎聲  
		    {
		    	try{
			    	urlWelcome = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/welcome.wav");
			    	voiceGet=Applet.newAudioClip(urlWelcome);  //指定歌曲給撥放源
			    	voiceGet.play();   //撥一次
			    	}catch(Exception e1){}
		    	
		    }
		if(e.getSource()==BtnReceive)   //回應收到聲
		    {
		    	try{
			    	urlReceive = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/message.wav");
			    	voiceGet=Applet.newAudioClip(urlReceive);  //指定歌曲給撥放源
			    	voiceGet.play();  //撥一次
			    	}catch(Exception e2){}
		    	
		    }
		if(e.getSource()==BtnLeave)   
		    {
		    	try{
			    	urlLeave = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/leave.wav");
			    	voiceGet=Applet.newAudioClip(urlLeave);
			    	voiceGet.play();
			    	}catch(Exception e3){}
		    	
		    }
		
		
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		 s = e.getItem().toString();  //s=歌名
        
        	if(s=="卡農")
        	{
        		if(record!=0)   //如果之前撥放源已有歌在play，關掉它
        		{song.stop();}
        		try{
			    	urlOne = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/cl6_18.mid");
			    	song=Applet.newAudioClip(urlOne);   //指定歌曲給撥放源
			    	song.loop(); //循環撥
			    	record=1;
			    	}catch(Exception e4){}
        	}
      	    if(s=="鱒魚")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlTwo = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ac3cl_18.mid");
			    	song=Applet.newAudioClip(urlTwo);
			    	song.loop();
			    	record=2;
			    	}catch(Exception e5){}
        	}
        	if(s=="土耳其進行曲")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlThree = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ac3cl_1.mid");
			    	song=Applet.newAudioClip(urlThree);
			    	song.loop();
			    	record=3;
			    	}catch(Exception e6){}
        	}
        	if(s=="阿萊城姑娘")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlFour = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/cl5_19.mid");
			    	song=Applet.newAudioClip(urlFour);
			    	song.loop();
			    	record=4;
			    	}catch(Exception e7){}
        	}
        	if(s=="仙劍奇俠傳")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlFive = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ct11_17.mid");
			    	song=Applet.newAudioClip(urlFive);
			    	song.loop();
			    	record=5;
			    	}catch(Exception e8){}
        	}
        	if(s=="命中註定")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlSix = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ch45_6.mid");
			    	song=Applet.newAudioClip(urlSix);
			    	song.loop();
			    	record=6;
			    	}catch(Exception e9){}
        	}
        	if(s=="新不了情")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlSeven = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ch46_18.mid");
			    	song=Applet.newAudioClip(urlSeven);
			    	song.loop();
			    	record=7;
			    	}catch(Exception e10){}
        	}
        	if(s=="還是會寂寞")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlEight = new URL("file:///C:/Documents and Settings/ntuee/桌面/sound/music/ch32_7.mid");
			    	song=Applet.newAudioClip(urlEight);
			    	song.loop();
			    	record=8;
			    	}catch(Exception e11){}
        	}
        	if(s=="關掉")
        	{
        		song.stop();   //關掉，reset record
        	    record=0;
        	}
	  
	}
	
	public static void main(String args[])
	{
		sound sTest=new sound();
	}
}