⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 radio.java

📁 java多媒体技术 7.1 播放声音文件 7.2 收音机 7.3 电子琴 7.4 吃豆游戏 7.5 简易壁球 7.6 弹球游戏 7.7 拯救生命 7.8 扫雷 7.9 双向飞碟 7
💻 JAVA
字号:
import java.applet.*;import java.awt.*;public class Radio extends Applet implements Runnable{	private Thread	 m_radio = null;	AudioClip sound;	AudioClip[] sounds = new AudioClip[7];    Image origImage, origImage1, origImage2, origImage3;    boolean onoff_switch;    int playing=0, counter=0, TOP_POS=40;	String AudioClip0;	String AudioClip1;	String AudioClip2;	String AudioClip3;	String AudioClip4;	String AudioClip5;	String AudioClip6;public Radio(){  Panel p;  setBackground(Color.black);  onoff_switch = false;  p = new Panel();  p.setLayout(new FlowLayout(FlowLayout.CENTER,4,4));  p.add(new Button("  1  "));  p.add(new Button("  2  "));  p.add(new Button("  3  "));  p.add(new Button("  4  "));  p.add(new Button("  5  "));  p.add(new Button("  6  "));  Button onoff = new Button("开/关");  onoff.setForeground(Color.red);  onoff.setBackground(Color.yellow);  onoff.repaint();  p.add("West",onoff);  add("South",p);}public String getAppletInfo(){return "Name: Radio\r\n" +       "Author: Ali";}public void init(){	AudioClip0 = "onoff.au";	AudioClip1 = getParameter("button1");	AudioClip2 = getParameter("button2");	AudioClip3 = getParameter("button3");	AudioClip4 = getParameter("button4");	AudioClip5 = getParameter("button5");	AudioClip6 = getParameter("button6");    String imageName = getParameter("image");	if ( imageName == null ) imageName = "images\\music002.gif";	origImage = getImage(getDocumentBase(), imageName);	MediaTracker mt = new MediaTracker(this);	mt.addImage(origImage, 0);	try {		mt.waitForAll();	}	catch (Exception e) {	}	String imageName1 = getParameter("image1");	if ( imageName1 == null ) imageName1 = "images\\speak1.gif";	origImage1 = getImage(getDocumentBase(), imageName1);	mt.addImage(origImage1, 0);	try {		mt.waitForAll();	}	catch (Exception e) {	}	String imageName2 = getParameter("image2");	if ( imageName2 == null ) imageName2 = "images\\speak2.gif";	origImage2 = getImage(getDocumentBase(), imageName2);	mt.addImage(origImage2, 0);	try {		mt.waitForAll();	}	catch (Exception e) {	}	String imageName3 = getParameter("image3");	if ( imageName3 == null ) imageName3 = "images\\speak3.gif";	origImage3 = getImage(getDocumentBase(), imageName3);	mt.addImage(origImage3, 0);	try {		mt.waitForAll();	}	catch (Exception e) {	}	        loadAudioClip(0,AudioClip0);	loadAudioClip(1,AudioClip1);        loadAudioClip(2,AudioClip2);	loadAudioClip(3,AudioClip3);	loadAudioClip(4,AudioClip4);	loadAudioClip(5,AudioClip5);	loadAudioClip(6,AudioClip6);}    public void update(Graphics g){	paint(g);}public void paint(Graphics g){     g.drawImage(origImage, 52, TOP_POS, this);     g.setColor(Color.gray);     g.drawLine(401,3,401,117);     g.drawLine(402,2,402,118);     g.drawLine(403,1,403,119);     g.drawLine(3,117,401,117);     g.drawLine(2,118,402,118);     g.drawLine(1,119,403,119);     g.drawLine(3,3,3,117);     g.drawLine(3,3,401,3);     	 if ( onoff_switch == true )        g.setColor(Color.green);	 g.fillArc(370,15,7,7,0,360);     switch (counter) {            case 0:           		 g.drawImage(origImage3, 2, TOP_POS, this);                 g.drawImage(origImage3, 352, TOP_POS, this);				 counter++;				 break;			case 1:           		 g.drawImage(origImage1, 2, TOP_POS, this);                 g.drawImage(origImage1, 352, TOP_POS, this);				 counter++;				 break;            case 2:          		 g.drawImage(origImage2, 2, TOP_POS, this);                 g.drawImage(origImage2, 352, TOP_POS, this);				 counter++;	    		 break;            case 3:           		 g.drawImage(origImage3, 2, TOP_POS, this);                 g.drawImage(origImage3, 352, TOP_POS, this);				 counter++;				 break;                 }	if ( counter == 4 ) counter=1;						}public void start(){	if (m_radio == null)	{		m_radio = new Thread(this);		m_radio.start();	}}public void stop(){		if (m_radio != null)		{			m_radio.stop();			m_radio = null;		}}public void run(){while (true) {	try	{	if ( playing != 0 ) {		if ( counter == 0 )		    counter = 1;		repaint();		Thread.sleep(500);		}	Thread.sleep(50);	}	catch (InterruptedException e)	{		stop();	}}}public boolean action(Event evt, Object arg)  {  if (arg.equals("  6  ") && ( onoff_switch == true )) {	  playAudioLoop(6);     }  if (arg.equals("  5  ") && ( onoff_switch == true )) {	  playAudioLoop(5);     }  if (arg.equals("  4  ") && ( onoff_switch == true )) {	  playAudioLoop(4);     }  if (arg.equals("  3  ") && ( onoff_switch == true )) {	  playAudioLoop(3);     }  if (arg.equals("  2  ") && ( onoff_switch == true )) {	  playAudioLoop(2);     }  if (arg.equals("  1  ") && ( onoff_switch == true )) {	  playAudioLoop(1);  }  if (arg.equals("开/关")) {      if ( onoff_switch == true ) {		  sounds[playing].stop();		  onoff_switch = false;		  }	  else {		  onoff_switch = true;	  }	  sounds[0].play();	  counter = 0;	  playing = 0;	  repaint();	  }  else return super.action(evt, arg);  return true;    }public void playAudio(String clip){try {sound = getAudioClip(getCodeBase(), clip);}catch (Exception e) {	return;}sound.play();  }public void playAudioLoop(int i){if ( i != 0 ) sounds[playing].stop();playing = i;playAudio("click.au");try {  Thread.sleep(1000);  } catch (InterruptedException e)	{	stop();	}  sounds[i].loop();  }public void loadAudioClip(int i, String clip){try {sounds[i] = getAudioClip(getCodeBase(), clip);}catch (Exception e) {	return;}}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -