发新话题
移动帖子 加入精华 加入置顶 加入收藏 关注此帖

电子鼓程序,"Java Examples in Nutshell”中的例子,按键盘发声





电子鼓程序,"Java Examples in Nutshell”中的例子,按键盘发声

http://topic.csdn.net/u/20080713/08/7871bd3a-486e-4fb0-851a-8eccffef57db.html

import javax.sound.midi.*;
import java.awt.event.*;
import javax.swing.*;

public class T extends JFrame {
  MidiChannel channel; // The channel we play on: 10 is for percussion

  int velocity = 64; // Default volume is 50%

  public static void main(String[] args) throws MidiUnavailableException {
    // We don't need a Sequencer in this example, since we send MIDI
    // events directly to the Synthesizer instead.
    Synthesizer synthesizer = MidiSystem.getSynthesizer();
    synthesizer.open();
    JFrame frame = new T(synthesizer);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(50, 128); // We use window width as volume control
    frame.setVisible(true);
  }

  public T(Synthesizer synth) {
    super("T");
    // Channel 10 is the GeneralMidi percussion channel. In Java code, we
    // number channels from 0 and use channel 9 instead.
    channel = synth.getChannels()[9];
    addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key >= 35 && key <= 81) {
          channel.noteOn(key, velocity);
        }
      }

      public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key >= 35 && key <= 81)
          channel.noteOff(key);
      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent e) {
        velocity = e.getX();
      }
    });
  }
}
快乐渡过每一天,减肥坚持每一天



编辑 回复 快速回复 TOP


Re:电子鼓程序,"Java Examples in Nutshell”中的例子,按键盘发声

学习一下!
编辑 回复 快速回复 TOP
发新话题