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

📄 musickeyboard.java

📁 java多媒体技术 7.1 播放声音文件 7.2 收音机 7.3 电子琴 7.4 吃豆游戏 7.5 简易壁球 7.6 弹球游戏 7.7 拯救生命 7.8 扫雷 7.9 双向飞碟 7
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.Hashtable;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * 用计算机键盘模拟电子琴的java小应用程序
 */
public class MusicKeyboard extends Applet
{
    /**
     * 在小应用程序顶部出现的标签组件
     */
    private Label readyLabel;
    /**
     * 一幅链接作者网址的图片.
     */
    private Image authorUrlImage;
    /**
     * 用来存放所有键盘与音调对应关系的数据结构
     */
    private Hashtable notes;
    /**
     * 包含所有与音调有关的键盘
     */
    private String keys="Q2W3ER5T6Y7UI9O0P[=]qwertyuiop";
    /**
     * 一幅表示计算机键盘与电子琴键盘相互对应关系的图
     */
    private Image keyboardImage;
    /**
     * 激活小应用程序
     */
    private final String instruction="请用鼠标单击下面的电子琴键盘,以便激活该程序";
    /**
     * 网址:
     */
    private final String myWebPage="http://yahoo.com/programming.html";
	/**
	 * 画小应用程序,画键盘图
	 * 参数 g 用来画图的graphics图像对象
	 */
	public void paint(Graphics g)
	{
	    g.drawImage(keyboardImage, 24, 25, this);
	    g.drawImage(authorUrlImage, 75, bounds().height - 21, this);
	    super.paint(g);
	}
	/**
	 * 播放被用户按下的键盘所相应的音调.
	 * 如果用户按下的键盘没有对应的值,什么也不会发生
	 * 参数 key 被用户按下的键盘所对应的整数值.
	 */	
	private void play(int key)
	{
	    Note note = (Note)notes.get(new Integer(key));
	    if (note == null)
	        return;
	    if (!note.isPlaying())
	        note.play();
	}
	/**
	 * 停止相应键盘所对应的音调.
	 * 参数key 用来停止音调的键盘所对应的整数值.
	 */
	private void silence(int key)
	{
	    Note note = (Note)notes.get(new Integer(key));
	    if (note == null)
	        return;
	    note.stop();
	}
	/**
	 * 使所有的音调停止
	 */
	private void silenceAll()
	{
	    int numKeys = keys.length();
	    for (int i = 0; i < numKeys; i++)
	        silence(keys.charAt(i));
	}
	/**
	 * 获取所有音调所对应的声音文件,并将它们存放在一个数据结构中,
	 */
	private void loadSounds()
	{
	    URL base=getCodeBase();
	    
	    int numkeys=keys.length();	    
	    for (int i=0; i<numkeys; i++)
	    {
	        int thisKey = keys.charAt(i);
	        String soundFile = getFileNameForKey(thisKey);
	        Note note = new Note( getAudioClip(base, "sounds/" + soundFile) );
	        notes.put(new Integer(thisKey), note);
	        
	        //确认它已经被调入内存中
	        note.play();
	        note.stop();
	    }
	}
	/**
	 * 制作包含作者URL的图片
	 */
	private Image makeAuthorUrlImage()
	{
	    Image au;
	    au = createImage(300, 17);
	    Graphics auG = au.getGraphics();
	    
	    auG.setColor(getBackground());
	    auG.fillRect(0, 0, 300, 17);
	    
	    auG.setColor(Color.blue);
	    auG.drawRect(0, 0, 299, 16);
	    auG.setFont(new Font("Courier", Font.PLAIN, 10));
	    auG.drawString(myWebPage, 5, 11);
	    
	    return au;
	}
	/**
	 * 制作一幅表示计算机键盘与电子琴键盘相互对应关系的图
	 */
	private Image makeKeyboard()
	{
	    Image kb;
	    kb = createImage(400, 100);
	    Graphics kbG = kb.getGraphics();
	    
	    kbG.setColor(Color.black);
	    kbG.fillRect(0, 0, 400, 100);
	    //画”主要音调“键盘
	    kbG.setColor(Color.white);
	    for (int n = 0; n < 12; n++)
	    {
	        kbG.fillRect( 3 + (n * 33), 2, 31, 96);
	    }
	    //画"临时音调"键盘
	    kbG.setColor(Color.black);
	    for (int n = 0; n < 11; n++)
	    {
	        switch (n)
	        {
	            case 0:
	            case 1:
	            case 3:
	            case 4:
	            case 5:
	            case 7:
	            case 8:
	            case 10:
	            kbG.fillRect( 24 + (n * 33), 2, 22, 65);
	            break;
	        }
	    }
	    //画字符
	    kbG.setFont(new Font("Courier", Font.BOLD, 24));
	    for (int n = 0, whiteIndex = 0; n < 20; n++)
	    {
	        int thisChar=keys.charAt(n);
	        
	        switch (thisChar)
	        {
	            case 'Q':
	            case 'W':
	            case 'E':
	            case 'R':
	            case 'T':
	            case 'Y':
	            case 'U':
	            case 'I':
	            case 'O':
	            case 'P':
	            case '[':
	            case ']':
	            kbG.setColor(Color.black);
	            kbG.drawString(keys.substring(n, n + 1), 9 + (whiteIndex * 33), 90);
	            whiteIndex++;
	            break;
	            case '2':
	            case '3':
	            case '5':
	            case '6':
	            case '7':
	            case '9':
	            case '0':
	            case '=':
	            kbG.setColor(Color.white);
	            kbG.drawString(keys.substring(n, n + 1), 28 + ((whiteIndex - 1) * 33), 60);
	            break;
	        }
	    }
	    return kb;
	}
	/**
	 * 获取指定字符所对应的声音文件名
	 */	
	private String getFileNameForKey(int key)
	{
	    switch (key)
	    {
	        case 'Q':
	        case 'q':
	         return "c2.au";
	        case 'W':
	        case 'w':
	         return "d2.au";	         
	        case 'E':
	        case 'e':
	         return "e2.au";	         
	        case 'R':
	        case 'r':
	         return "f2.au";	         
	        case 'T':
	        case 't':
	         return "g2.au";	         
	        case 'Y':
	        case 'y':
	         return "a2.au";	         
	        case 'U':
	        case 'u':
	         return "b2.au";	         
	        case 'I':
	        case 'i':
	         return "c3.au";	         
	        case 'O':
	        case 'o':
	         return "d3.au";	         
	        case 'P':
	        case 'p':
	         return "e3.au";	         
	        case '[':
	         return "f3.au";	         
	        case ']':
	         return "g3.au";	         
	        case '2':
	         return "csharp2.au";	         
	        case '3':
	         return "dsharp2.au";	         
	        case '5':
	         return "fsharp2.au";	         
	        case '6':
	         return "gsharp2.au";
	        case '7':
	         return "asharp2.au";
	        case '9':
	         return "csharp3.au";
	        case '0':
	         return "dsharp3.au";
	        case '=':
	         return "fsharp3.au";
	    }
	    return null;
	}
	/**
	 * 将浏览器转到指定的网址上
	 */
	private void visitAuthor()
	{
	    try
		{
		    this.getAppletContext().showDocument(new URL(myWebPage));
		}
		catch (MalformedURLException e)
		{
		}
	}
	/**
	 * 初始化图片,上载声音文件
	 */
	public void init()
	{
	    setLayout(new BorderLayout());
	    
	    keyboardImage = makeKeyboard();
	    authorUrlImage = makeAuthorUrlImage();
		notes=new Hashtable(127);
		loadSounds();
		
		readyLabel=new Label("", Label.CENTER);
		add("North", readyLabel);
		
	    readyLabel.setText(instruction);
	}
	/**
	 * 处理播放声音文件的键盘事件和单击作者网址的鼠标事件
	 */
	public boolean handleEvent(Event evt)
	{
	    if (evt.id == Event.KEY_PRESS)
	    {
	        play(evt.key);
	        return true;
	    }
	    else if (evt.id == Event.KEY_RELEASE)
	    {
	        silence(evt.key);
	        return true;
	    }
	    else if (evt.id == Event.MOUSE_DOWN)
	    {
	        //检测作者的网址链接是否被单击
	        if ( evt.x >= 75 && evt.x <= (75 + authorUrlImage.getWidth(this)) &&
	            evt.y >= (bounds().height - 21) &&
	            evt.y <= (bounds().height - 21 + authorUrlImage.getHeight(this)) )
	        {
	            visitAuthor();
	            return true;
	        }
        }
        else if (evt.id == Event.LOST_FOCUS)
        {
            silenceAll();
            return true;
        }

	    return super.handleEvent(evt);
	}

}

⌨️ 快捷键说明

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