// Keyboard.java
//Download:http://www.codefans.net
/*
* Portions of Keyboard's source code were excerpted from Sun's MidiSynth.java
* source file. I've included Sun's original copyright and license, to be fair
* to Sun.
*
* Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
* BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
* MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
* DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
* THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or
* in the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.sound.midi.*;
import javax.swing.*;
/*
This class creates a keyboard component that knows how to play a specific
instrument.
*/
public class Keyboard extends JPanel
{
public final static Color KEY_BLUE = new Color (204, 204, 255);
public final static int KEY_HEIGHT = 80, KEY_WIDTH = 16;
private Key theKey;
private MidiChannel channel;
private Synthesizer synthesizer;
private Vector blackKeys = new Vector ();
private Vector keys = new Vector ();
private Vector whiteKeys = new Vector ();
public Keyboard ()
{
setLayout (new BorderLayout ());
setPreferredSize (new Dimension (42*KEY_WIDTH+1, KEY_HEIGHT+1));
int transpose = 24;
int [] whiteIDs = { 0, 2, 4, 5, 7, 9, 11 };
for (int i = 0, x = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++, x += KEY_WIDTH)
{
int keyNum = i * 12 + whiteIDs [j] + transpose;
whiteKeys.add (new Key (x, 0, KEY_WIDTH, KEY_HEIGHT, keyNum));
}
}
for (int i = 0, x = 0; i < 6; i++, x += KEY_WIDTH)
{
int keyNum = i * 12 + transpose;
blackKeys.add (new Key ((x += KEY_WIDTH)-4, 0, KEY_WIDTH/2,
KEY_HEIGHT/2, keyNum+1));
blackKeys.add (new Key ((x += KEY_WIDTH)-4, 0, KEY_WIDTH/2,
KEY_HEIGHT/2, keyNum+3));
x += KEY_WIDTH;
blackKeys.add (new Key ((x += KEY_WIDTH)-4, 0, KEY_WIDTH/2,
KEY_HEIGHT/2, keyNum+6));
blackKeys.add (new Key ((x += KEY_WIDTH)-4, 0, KEY_WIDTH/2,
KEY_HEIGHT/2, keyNum+8));
blackKeys.add (new Key ((x += KEY_WIDTH)-4, 0, KEY_WIDTH/2,
KEY_HEIGHT/2, keyNum+10));
}
keys.addAll (blackKeys);
keys.addAll (whiteKeys);
addMouseListener (new MouseAdapter ()
{
public void mousePressed (MouseEvent e)
{
// Identify the key that was pressed. A null
// value indicates something other than a key
// was pressed.
theKey = getKey (e.getPoint ());
// If a key was pressed ...
if (theKey != null)
{
// Tell key to start playing note.
theKey.on ();
// Update key's visual appearance.
repaint ();
}
}
public void mouseReleased (MouseEvent e)
{
if (theKey != null)
{
// Tell key to stop playing note.
theKey.off ();
// Update key's visual appearance.
repaint ();
}
}
public void mouseExited (MouseEvent e)
{
// This method is called if the mouse is moved
// off the keyboard component. If a key was
// pressed, we release that key.
if (theKey != null)
{
// Tell key to stop playing note.
theKey.off ();
// Update key's visual appearance.
repaint ();
// The following assignment is needed so
// that we don't execute the code within
// mouseReleased()'s if statement should we
// release a key after exiting the keyboard
// component. There is no need to tell the
// key to stop playing a note after we have
// already told it to do so. Furthermore,
// we prevent an unnecessary repaint.
theKey = null;
}
}
public Key getKey (Point point)
{
// Identify the key that was clicked.
for (int i = 0; i < keys.size (); i++)
{
if (((Key) keys.get (i)).contains (point))
return (Key) keys.get (i);
}
return null;
}
});
}
public boolean chooseInstrument (int instrumentID)
{
if (channel == null)
return false;
// Select new instrument based on ID.
channel.programChange (instrumentID);
return true;
}
public String [] getInstruments ()
{
if (synthesizer == null)
return null;
Instrument [] instruments = synthesizer.getLoadedInstruments ();
String [] ins = new String [instruments.length];
for (int i = 0; i < instruments.length; i++)
ins [i] = instruments [i].toString ();
return ins;
}
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize ();
g2.setBackground (getBackground ());
g2.clearRe
JAVA Applet钢琴模拟程序java源码
需积分: 0 52 浏览量
更新于2022-08-27
收藏 5KB ZIP 举报
Java Applet钢琴模拟程序是一个基于Java编程语言的交互式应用程序,它允许用户通过网页浏览器体验类似于真实钢琴的演奏效果。这个程序的核心在于利用了Java Applet技术,这是一种早期的Java技术,用于在Web浏览器中嵌入动态和交互式内容。在Java Applet钢琴模拟程序中,开发者将音乐理论与编程技巧结合,创建了一个可以播放不同音符的虚拟键盘。
我们来深入了解一下Java Applet。Java Applet是Java平台的一部分,主要用于创建可以在Web浏览器中运行的小型应用程序。这些小应用程序通常用于提供动态内容,如游戏、图形演示或其他交互式功能。Java Applet通过Java插件在用户的浏览器中执行,允许用户无需下载和安装整个应用程序就能使用其功能。
Java Applet的生命周期包括几个关键阶段:初始化(init)、启动(start)、绘画(paint)、更新(update)、暂停(stop)和终止(destroy)。在钢琴模拟程序中,初始化阶段可能涉及设置钢琴的布局和音符映射,启动阶段则可能开启音频播放线程,而绘画阶段则负责在屏幕上绘制钢琴键。
在代码实现方面,钢琴模拟程序可能会包含以下几个主要部分:
1. **用户界面**:使用Java的AWT(Abstract Window Toolkit)或Swing库来创建图形用户界面,展示钢琴键盘。键盘上的每个键可能对应一个按钮或组件,用户点击时会触发事件处理。
2. **音频处理**:Java Sound API是Java中的一个重要组成部分,用于处理音频输入和输出。在这个程序中,开发者可能使用该API来播放不同音符的声音,根据用户点击的键来选择对应的音频文件或生成音频流。
3. **事件监听**:为每个钢琴键添加事件监听器,当用户点击时,触发事件并播放相应的音符。这通常涉及到Java的事件处理机制,如ActionListener接口。
4. **音高和节奏控制**:高级的钢琴模拟程序可能还包括音高调整、音量控制以及节奏设定等功能,这些都是通过处理用户输入和调整音频参数来实现的。
5. **MIDI支持**:除了直接播放音频文件,Java也提供了对MIDI(Musical Instrument Digital Interface)的支持。通过MIDI,程序可以生成音乐符号,与各种音乐软件和硬件设备进行交互。
6. **线程管理**:为了确保钢琴模拟程序的流畅运行,音频播放通常会在独立的线程中进行,避免阻塞用户界面的更新。
尽管Java Applet在现代Web开发中已经逐渐被HTML5的Web应用所取代,但学习和理解Java Applet钢琴模拟程序的实现原理仍然可以帮助我们了解早期Web技术,以及Java在处理交互式内容方面的强大能力。此外,对于那些对音乐编程或游戏开发感兴趣的人来说,这样的项目也是一个很好的实践机会。
「已注销」
- 粉丝: 119
- 资源: 213
最新资源
- 基于Java+Swing+Mysql商城购物系统源码+数据库脚本.zip
- 嵌入式系统开发中FreeRTOS实时操作系统的应用详解
- 基于OpenVINO+Cpp部署YOLOv10目标检测算法源码.zip
- 基于Java+Swing+Mysql商城购物系统源码+数据库+报告PPT (高分项目)
- Windows操作系统全解析:发展历程、主要版本及应用场景详解
- 嵌入式系统开发领域FreeRTOS实时操作系统的特性和应用场景
- 前端开发领域的JavaScript基础知识与核心应用
- 学生信息管理系统(python+tkinter+MySQL)源码+课设报告
- 学生信息管理系统(python+tkinter+MySQL)源码+课设报告
- python学生信息管理系统+MySql(源码+数据库).zip
- 2311直播课程.part06.rar
- 多编程语言实现字符串转化为回文串与回文检测算法
- 多编程语言实现平方数及其倍数计算
- Maven安装配置指南-涵盖环境变量、IDE集成与常用命令详解
- winbox是管理mikrokit routeros的图形界面
- Python的招聘网站招聘信息分析系统源码+数据库+文档说明.zip