import javax.sound.midi.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Vector; public class Noodirakend6 extends JApplet implements ActionListener, MetaEventListener{ Sequence sequence; Sequencer sequencer; JButton nupp=new JButton("Mängi"); JCheckBox ruut=new JCheckBox("Korda"); public void init(){ try{ ShortMessage lahti = new ShortMessage(); ShortMessage kinni = new ShortMessage(); ShortMessage algus = new ShortMessage(); algus.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 16, 0); lahti.setMessage(ShortMessage.NOTE_ON, 0, 65, 93); kinni.setMessage(ShortMessage.NOTE_OFF, 0, 65, 93); sequence=new Sequence(Sequence.PPQ, 4); Track track=sequence.createTrack(); track.add(new MidiEvent(algus, 0)); track.add(new MidiEvent(lahti, 0)); track.add(new MidiEvent(kinni, 4)); track.add(new MidiEvent(lahti, 8)); track.add(new MidiEvent(kinni, 11)); track.add(new MidiEvent(lahti, 12)); track.add(new MidiEvent(kinni, 15)); track.add(new MidiEvent(lahti, 16)); track.add(new MidiEvent(kinni, 31)); getContentPane().add(nupp, BorderLayout.SOUTH); getContentPane().add(ruut, BorderLayout.NORTH); nupp.addActionListener(this); }catch(Exception e){e.printStackTrace();} } public void actionPerformed(ActionEvent e){ try{ sequencer=MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.addMetaEventListener(this); sequencer.start(); }catch(Exception ex){ ex.printStackTrace(); System.out.println(ex); } } public void meta(MetaMessage m){ if(m.getType()==47 && ruut.isSelected()){ sequencer.start(); } else { sequencer.close(); } } } /** * MetaEventListenerile saadetakse rajalt saabuvad meta-tüüpi teated. * Üheks selliseks on ka raja lõpp (teade koodiga 47). Et lugu taas otsast algaks * tuleb sekventserile öelda start. Muul juhul aga vabastab close() muusikaressursid * teiste programmide tarvis. */