Results 1 to 6 of 6
  1. #1
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Converting to JApplet

    Hey fellas,

    I was wondering if anybody could help me with converting my BeatBox app (from head first java) to a JApplet..
    I've done it with alot of applications but this one is abit too advanced for me I guess...

    Code of application:
    Java Code:
    package BeatBox;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.sound.midi.*;
    
    import java.util.*;
    import java.awt.event.*;
    
    public class BeatBox  {
    	
    	JPanel mainPanel;
    	ArrayList<JCheckBox> checkboxList;
    	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", "Maracas",
    			"Whistle", "Low Conga", "Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo", "Open Hi Conga"};
    	int[] intruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
    
    	public static void main(String[] args) {
    		BeatBox box = new BeatBox();
    		box.buildGui();
    	}
    	
    	public void buildGui(){
    		
    		theFrame = new JFrame("Jimme's BeatBox");
    		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		BorderLayout layout = new BorderLayout();
    		JPanel background = new JPanel(layout);
    		background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    		
    		checkboxList = new ArrayList<JCheckBox>();
    		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("Tempo Up");
    		upTempo.addActionListener(new MyUpTempoListener());
    		buttonBox.add(upTempo);
    		
    		JButton downTempo = new JButton("Tempo Down");
    		downTempo.addActionListener(new MyDownTempoListener());
    		buttonBox.add(downTempo);
    		
    		JButton clear = new JButton("Clear");
    		clear.addActionListener(new Clear());
    		buttonBox.add(clear);
    
    		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.setHgap(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();
    			sequence = new Sequence(Sequence.PPQ, 4);
    			track = sequence.createTrack();
    			sequencer.setTempoInBPM(120);
    		}catch(Exception e){e.printStackTrace();}
    	}//close method
    	
    	public void buildTrackAndStart(){
    		int[] trackList = null;
    		
    		sequence.deleteTrack(track);
    		track = sequence.createTrack();
    		
    		for (int i = 0; i < 16; i++){
    			trackList = new int[16];
    			
    			int key = intruments[i];
    			
    			for (int 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(176,1,127,0,16));
    		}
    		
    		track.add(makeEvent(192,9,1,0,15));
    		try{
    			sequencer.setSequence(sequence);
    			sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    			sequencer.start();
    			sequencer.setTempoInBPM(120);
    			
    		}catch(Exception e)
    		{e.printStackTrace();}
    				
    		}
    	
    	public class MyStartListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			buildTrackAndStart();
    			
    		}
    	}
    	
    	public class MyStopListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			sequencer.stop();
    			
    		}
    	}
    	
    	public class MyUpTempoListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			float tempoFactor = sequencer.getTempoFactor();
    			sequencer.setTempoFactor((float) (tempoFactor * 1.03));
    		}
    	}
    	
    	public class MyDownTempoListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			float tempoFactor = sequencer.getTempoFactor();
    			sequencer.setTempoFactor((float) (tempoFactor * .97));
    		}
    	}
    	
    	public class Clear implements ActionListener{
    	@Override
    	public void actionPerformed(ActionEvent a) {
    		for(int i = 0; i < 256; i++){
    			checkboxList.get(i).setSelected(false);		}
    	}
    	}
    	
    	public void makeTracks (int[] list){
    		
    		for(int i = 0; i < 16; i++){
    			int key = list[i];
    			
    			if(key !=0){
    				track.add(makeEvent(144,9,key,100,i));
    				track.add(makeEvent(128,9,key,100,i+1));
    			}
    		}
    	}
    	
    	public 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;
    	}		
    }

    Code of JApplet:
    Java Code:
    package BeatBox;
    
    
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.sound.midi.*;
    
    import java.util.*;
    import java.awt.event.*;
    
    public class BeatBoxApplet implements JApplet  {
    	
    	JPanel mainPanel;
    	ArrayList<JCheckBox> checkboxList;
    	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", "Maracas",
    			"Whistle", "Low Conga", "Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo", "Open Hi Conga"};
    	int[] intruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
    
    	
    	public void init(){
    		BeatBoxApplet box = new BeatBoxApplet();
    		box.start();
    	}
    	public BeatBoxApplet() {
    	}
    	
    	public void start(){
    		
    		theFrame = new JFrame("Jimme's BeatBox");
    		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		BorderLayout layout = new BorderLayout();
    		JPanel background = new JPanel(layout);
    		background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    		
    		checkboxList = new ArrayList<JCheckBox>();
    		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("Tempo Up");
    		upTempo.addActionListener(new MyUpTempoListener());
    		buttonBox.add(upTempo);
    		
    		JButton downTempo = new JButton("Tempo Down");
    		downTempo.addActionListener(new MyDownTempoListener());
    		buttonBox.add(downTempo);
    		
    		JButton clear = new JButton("Clear");
    		clear.addActionListener(new Clear());
    		buttonBox.add(clear);
    
    		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.setHgap(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();
    			sequence = new Sequence(Sequence.PPQ, 4);
    			track = sequence.createTrack();
    			sequencer.setTempoInBPM(120);
    		}catch(Exception e){e.printStackTrace();}
    	}//close method
    	
    	public void buildTrackAndStart(){
    		int[] trackList = null;
    		
    		sequence.deleteTrack(track);
    		track = sequence.createTrack();
    		
    		for (int i = 0; i < 16; i++){
    			trackList = new int[16];
    			
    			int key = intruments[i];
    			
    			for (int 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(176,1,127,0,16));
    		}
    		
    		track.add(makeEvent(192,9,1,0,15));
    		try{
    			sequencer.setSequence(sequence);
    			sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    			sequencer.start();
    			sequencer.setTempoInBPM(120);
    			
    		}catch(Exception e)
    		{e.printStackTrace();}
    				
    		}
    	
    	public class MyStartListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			buildTrackAndStart();
    			
    		}
    	}
    	
    	public class MyStopListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			sequencer.stop();
    			
    		}
    	}
    	
    	public class MyUpTempoListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			float tempoFactor = sequencer.getTempoFactor();
    			sequencer.setTempoFactor((float) (tempoFactor * 1.03));
    		}
    	}
    	
    	public class MyDownTempoListener implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent a) {
    			float tempoFactor = sequencer.getTempoFactor();
    			sequencer.setTempoFactor((float) (tempoFactor * .97));
    		}
    	}
    	
    	public class Clear implements ActionListener{
    	@Override
    	public void actionPerformed(ActionEvent a) {
    		for(int i = 0; i < 256; i++){
    			checkboxList.get(i).setSelected(false);		}
    	}
    	}
    	
    	public void makeTracks (int[] list){
    		
    		for(int i = 0; i < 16; i++){
    			int key = list[i];
    			
    			if(key !=0){
    				track.add(makeEvent(144,9,key,100,i));
    				track.add(makeEvent(128,9,key,100,i+1));
    			}
    		}
    	}
    	
    	public 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;
    	}		
    }
    The application works fine but the JApplet doesnt work in html browser :(

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Converting to JApplet

    Do you get an error?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting to JApplet

    The first code that I gave has no errors and works fine.. The second code is just a beginning I've made to make the application an applet.. But there are things wrong there..
    On line 12: public class BeatBoxApplet implements JApplet
    JApplet is still underscored with red, while I've overridden all JApplet methodes.. (I think)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting to JApplet

    For one, JApplet is a class, not an interface, and so you can't implement it but rather must extend it.

    Next of all, you're still trying to create a JFrame, and in fact your "new" code is not much different from the old other than you're trying to "implement" a JApplet and having given it an init method. This isn't how it works.

    I suggest that you read over the applet tutorial and that you scrap this code, start over, and sure, use some of the ideas and perhaps some of the code from your current attempt, but in your next attempt, gear your main program towards making a JPanel, one you can put in your JApplet's contentPane in its init method.
    Last edited by Fubarable; 02-21-2013 at 03:13 PM.

  5. #5
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting to JApplet

    Oh ye it extends ofc :S It was such a long time ago I read that.. So I'm reading the tutorial again for refreshment.. And thanks alot for the pointers :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Converting to JApplet

    Quote Originally Posted by JavaJimme View Post
    Oh ye it extends ofc
    This is a technical forum, not SMS chat. Please take the trouble to spell words in full.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2012, 05:32 AM
  2. Converting JFrame to JApplet is not showing on browser.
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 4
    Last Post: 06-12-2012, 06:05 PM
  3. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 02:40 PM
  4. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 09:45 AM

Posting Permissions

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