import javax.sound.midi.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; //import java.util.*; public class Midi2 extends JFrame{ File CDir; String oldFile; JComboBox midiChoice; JButton playButton, infoButton; JTextArea textarea; Sequencer sequencer; Sequence sequence; public Midi2(){ playButton = new JButton ("PLAY"); playButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("PLAY")){ startPlay(); playButton.setText("STOP"); }else{ stopPlay(); playButton.setText("PLAY"); } } }); infoButton = new JButton("INFO"); infoButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("INFO")){ // siia tuleb info tekstiväljale midifaili kohta midiInfo(); } } }); CDir = new File(System.getProperty("user.dir")); FilenameFilter filter = new FilenameFilter(){ public boolean accept(File directory, String filename){ String name = filename.toLowerCase(); return name.endsWith(".mid"); } }; String midiFiles[] = CDir.list(filter); if(midiFiles == null || midiFiles.length == 0){ System.out.println("Ei leia faile"); System.exit(1); }try{ sequencer = MidiSystem.getSequencer(); sequencer.open(); }catch(MidiUnavailableException e){ System.out.println("Midi systemi viga"); System.exit(1); } midiChoice = new JComboBox(midiFiles); midiChoice.setSelectedIndex(0); newSequence(midiFiles[0]); oldFile = midiFiles[0]; midiChoice.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ newSequence((String)midiChoice.getSelectedItem()); } }); JToolBar toolBar = new JToolBar(JToolBar.HORIZONTAL); GridLayout gl = new GridLayout(2,1); JPanel panel1 = new JPanel(gl); JPanel panel2 = new JPanel(gl); Label label = new Label("Midi List:"); panel1.add(label); panel1.add(midiChoice); panel2.add(playButton); panel2.add(infoButton); JPanel panel3=new JPanel(); panel3.setLayout(new BorderLayout()); textarea = new JTextArea(300, 500); //textarea.setScrollbar(new JScrollBar()); textarea.setFont(new Font("Arial", Font.PLAIN, 10)); textarea.setLineWrap(true); textarea.setWrapStyleWord(true); JScrollPane areaScrollPanel=new JScrollPane(textarea); areaScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPanel.setPreferredSize(new Dimension(600, 400)); panel3.add(areaScrollPanel); /*JEditorPane textarea = new JEditorPane(); JScrollPane editorScrollPane = new JScrollPane(textarea); editorScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); editorScrollPane.setPreferredSize(new Dimension(300, 500)); //editorScrollPane.setMinimumSize(new Dimension(10, 10));*/ Container content = getContentPane(); content.add(toolBar, BorderLayout.NORTH); content.add(panel3, BorderLayout.CENTER); toolBar.add(panel1); toolBar.addSeparator(); toolBar.add(panel2); setVisible(true); } void newSequence(String filename){ File midiFile = new File(CDir, filename); try{ sequence = MidiSystem.getSequence(midiFile); sequencer.setSequence(sequence); playButton.setText("PLAY"); oldFile = filename; } catch(InvalidMidiDataException e){ System.out.println("Vale data"); midiChoice.setSelectedItem(oldFile); } catch(IOException e){ System.out.println("Ei moodusta sequenci"); midiChoice.setSelectedItem(oldFile); } } void startPlay(){ sequencer.start(); } void stopPlay(){ sequencer.stop(); } void midiInfo(){ System.out.println("1"); textarea.append("Info ALGUS..................."); System.out.println("info"); textarea.append(sequence.getDivisionType()+" pikkus: "+sequence.getMicrosecondLength()/1000000.0+ " sekundit, "+sequence.getTickLength()+" tiksu"); Track[] rajad=sequence.getTracks(); textarea.append(rajad.length+" rada"); for(int nr=0; nr