1 Importjavax.sound.midi.*;2 Importjavax.swing.*;3 Importjava.awt.event.*;4 Importjava.awt.*;5 6 classMypanel_2extendsJPanelImplementscontrollereventlistener{7 Booleanmsg =false;8 9 Public voidpaintcomponent (Graphics g) {Ten if(msg) { OneG.fillrect (0, 0, This. GetWidth (), This. GetHeight ()); A intRed = (int) (Math.random () * 255); - intGreen = (int) (Math.random () * 255); - intBlue = (int) (Math.random () * 255); theColor Randomcolor =NewColor (red, green, blue); - G.setcolor (randomcolor); -G.filloval (70, 40, 100, 100); -msg =false; + } - } + A Public voidControlchange (shortmessage event) { atmsg =true; - repaint (); - } - } - - Public classmidi{ in -JFrame frame =NewJFrame ("My first Music Video"); to mypanel_2 Panel; + - Public voidShow_my_panel () { the frame.setdefaultcloseoperation (jframe.exit_on_close); *Panel =Newmypanel_2 (); $ Frame.setcontentpane (panel);Panax NotoginsengFrame.setbounds (30, 30, 300, 300); -Frame.setvisible (true); the } + A Public StaticMidievent Makeevent (intCmdintChanintOneintBoth,inttick) { theMidievent event =NULL; + Try { -Shortmessage A =Newshortmessage (); $ a.setmessage (Cmd, Chan, One, and both); $event =NewMidievent (A, tick); -}Catch(Exception e) {} - returnevent; the } - Wuyi Public voidPlay () { the Try { -Sequencer player =midisystem.getsequencer (); Wu Player.open (); - About int[] Eventsiwant = {127}; $ Player.addcontrollereventlistener (panel, eventsiwant); - -Sequence seq =NewSequence (SEQUENCE.PPQ, 4);//Divisiontype, resolution - ATrack track =Seq.createtrack (); + the for(inti = 5; I < 61; i+= 4) { -Track.add (Makeevent (144, 1, I, 100, i)); $Track.add (Makeevent (176, 1, 127, 0, i)); theTrack.add (makeevent, 1, I, I, 2)); the } the the player.setsequence (seq); -PLAYER.SETTEMPOINBPM (220); in Player.start (); the while(Player.isrunning ()) { theThread.Sleep (1000); About } the player.close (); the the}Catch(Exception ex) { + ex.printstacktrace (); - } the } Bayi the Public Static voidMain (string[] args) { theMIDI MIDI =NewMidi (); - - Midi.show_my_panel (); the Midi.play (); the } the}
Java Learning Note _midi_gui