首页 > 代码库 > java学习笔记_MIDI_GUI
java学习笔记_MIDI_GUI
1 import javax.sound.midi.*; 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 6 class MyPanel_2 extends JPanel implements ControllerEventListener{ 7 boolean msg = false; 8 9 public void paintComponent( Graphics g ) {10 if ( msg ) {11 g.fillRect(0, 0, this.getWidth(), this.getHeight());12 int red = (int) (Math.random() * 255);13 int green = (int) (Math.random() * 255);14 int blue = (int) (Math.random() * 255);15 Color randomColor = new Color(red, green, blue);16 g.setColor(randomColor);17 g.fillOval(70, 40, 100, 100);18 msg = false;19 }20 }21 22 public void controlChange(ShortMessage event) {23 msg = true;24 repaint();25 }26 }27 28 public class Midi{29 30 JFrame frame = new JFrame("My first Music Video");31 MyPanel_2 panel; 32 33 public void show_my_panel() {34 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);35 panel = new MyPanel_2();36 frame.setContentPane(panel);37 frame.setBounds(30, 30, 300, 300);38 frame.setVisible(true);39 }40 41 public static MidiEvent makeEvent(int cmd, int chan, int one, int two, int tick) {42 MidiEvent event = null;43 try {44 ShortMessage a = new ShortMessage();45 a.setMessage(cmd, chan, one, two);46 event = new MidiEvent(a, tick);47 } catch (Exception e) {}48 return event;49 }50 51 public void play() {52 try {53 Sequencer player = MidiSystem.getSequencer();54 player.open();55 56 int[] eventsIWant = {127};57 player.addControllerEventListener(panel, eventsIWant);58 59 Sequence seq = new Sequence(Sequence.PPQ, 4);//divisionType, resolution60 61 Track track = seq.createTrack();62 63 for (int i = 5; i < 61; i+= 4) {64 track.add(makeEvent(144, 1, i, 100, i));65 track.add(makeEvent(176, 1, 127, 0, i));66 track.add(makeEvent(128, 1, i, 100, i + 2));67 }68 69 player.setSequence(seq);70 player.setTempoInBPM(220);71 player.start();72 while( player.isRunning() ) { 73 Thread.sleep(1000);74 }75 player.close();76 77 } catch( Exception ex) {78 ex.printStackTrace();79 }80 } 81 82 public static void main(String[] args) {83 Midi midi = new Midi();84 85 midi.show_my_panel();86 midi.play();87 }88 }
java学习笔记_MIDI_GUI
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。