Results 1 to 3 of 3
  1. #1
    matvey is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default [help] my beatbox prgramming

    I run it on Eclipse but when i press button "play" , it shows error. Can you try to run it to help me? Thanks for your care!

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.sound.midi.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class BeatBox implements MetaEventListener {
    	
    	JPanel mainPanel;
    	ArrayList checkboxList;
    	int bpm;
    	Sequencer sequencer;
    	Sequence sequence;
    	Track track;
    	JFrame theFrame;
    	
    	String[] instrumentNames= { "Bass Drum","Closed Hi-hat","Open Hi-hat","Acoustic Snare","Crash Cymbal"
    			,"Hand Clap","High Tom","Hi Bongo","Macaras","Whistle","Low Conga","Cowbell","Vibraslap"
    			,"Low-mid Tom","High Agogo","Open Hi Conga"};
    	
    	int[] instruments= {35,42,46,38,49,50,60,70,72,64,56,58,47,67,63};
    	
    	public static void main(String[] args) {
    		
    		new BeatBox().buildGUI();
    
    	}
    	
    	public void buildGUI()
    	{
    		theFrame=new JFrame("Cyber BeatBox");
    		BorderLayout layout=new BorderLayout();
    		JPanel background=new JPanel(layout);
    		
    		background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    		checkboxList=new ArrayList();
    		Box buttonBox=new Box(BoxLayout.Y_AXIS);
    		
    		JButton start=new JButton("start");
    		start.addActionListener(new MyStartListener());
    		buttonBox.add(start);
    		
    		JButton stop=new JButton("stop");
    		stop.addActionListener(new MyStopListener());
    		buttonBox.add(stop);
    		
    		JButton upTempo=new JButton("upTempo");
    		upTempo.addActionListener(new MyUpTempoListener());
    		buttonBox.add(upTempo);
    		
    		JButton downTempo=new JButton("downTempo");
    		downTempo.addActionListener(new MyDownTempoListener());
    		buttonBox.add(downTempo);
    		
    		Box nameBox=new Box(BoxLayout.Y_AXIS);
    		
    		for(int i=0;i<16;i++)
    		{
    			nameBox.add(new Label(instrumentNames[i]));
    		}
    		
    		background.add(BorderLayout.EAST,buttonBox);
    		background.add(BorderLayout.WEST,nameBox);
    		
    		theFrame.getContentPane().add(background);
    		
    		GridLayout grid=new GridLayout(16,16);
    		
    		grid.setVgap(1);
    		grid.setVgap(2);
    		
    		mainPanel=new JPanel(grid);
    		
    		background.add(BorderLayout.CENTER,mainPanel);
    		
    		for(int i=0;i<256;i++)
    		{
    			JCheckBox c=new JCheckBox();
    			c.setSelected(false);
    			checkboxList.add(c);
    			
    			mainPanel.add(c);
    			
    		}
    		
    		setUpMidi();
    		
    		theFrame.setBounds(50,50,300,300);
    		
    		theFrame.pack();
    		theFrame.setVisible(true);
    				
    			
    	}
    	
    	public void setUpMidi()
    	{
    		try{
    			sequencer =MidiSystem.getSequencer();
    			sequencer.open();
    			
    			sequencer.addMetaEventListener(this);
    			sequence=new Sequence(Sequence.PPQ,4);
    			track=sequence.createTrack();
    			sequencer.setTempoInBPM(bpm);
    			
    			
    		}
    		
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	
    	public void buildTrackAndStart()
    	{
    		int[] trackList=null;
    		int j,key;
    		
    		sequence.deleteTrack(track);
    		track =sequence.createTrack();
    		
    		for(int i=0;i<16;i++)
    		{
    			trackList=new int[16];
    			key=instruments[i];
    			
    			for(j=0;j<16;j++)
    			{
    				JCheckBox jc=(JCheckBox)checkboxList.get(j+(16*i));
    				if(jc.isSelected())
    				{
    					trackList[j]=key;
    					
    				}
    				else
    				{
    					trackList[j]=0;
    				}
    			}
    			
    			makeTracks(trackList);
    						
    		}
    		
    		track.add(makeEvent(192,9,1,0,15));
    		try
    		{
    			sequencer.setSequence(sequence);
    			sequencer.start();
    			sequencer.setTempoInBPM(bpm);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    		
    	}
    	
    	public class MyStartListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			buildTrackAndStart();
    		}
    	}
    	
    	public class MyStopListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			sequencer.stop();
    		}
    	}
    	
    	public class MyUpTempoListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			bpm+=3;
    		}
    	}
    	
    	public class MyDownTempoListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			bpm-=3;
    		}
    	}
    	
    	public void makeTracks(int[] list)
    	{
    		int key;
    		for(int i=0;i<16;i++)
    		{
    			 key=list[i];
    			
    			if(key!=0)
    			{
    				track.add(makeEvent(144,9,key,100,i));
    				track.add(makeEvent(128,9,key,100,i+1));
    				
    			}
    		}
    	}
    	
    	 public static MidiEvent makeEvent(int comd,int chan,int one,int two,int tick)
    	 {
    		 MidiEvent event=null;
    		 
    		 try{
    			 ShortMessage a=new ShortMessage();
    			 a.setMessage(comd, chan, one, two);
    			 event=new MidiEvent(a,tick);
    		
    		 }
    		 catch(Exception e){
    			 e.printStackTrace();
    			 
    		 }
    		 return event;
    	 }
    	 
    	 public void meta(MetaMessage message)
    	 {
    		 if(message.getType()==47)
    		 {
    			 sequencer.start();
    			 sequencer.setTempoInBPM(bpm);
    		 }
    	 }				
    		
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Why don't you post the error that you got?

  3. #3
    matvey is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    this is it.i'm sorry .

    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •