Results 1 to 6 of 6
Thread: Converting to JApplet
- 02-21-2013, 01:13 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
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:
The application works fine but the JApplet doesnt work in html browser :(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; } }
- 02-21-2013, 01:48 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Converting to JApplet
Do you get an error?
Please do not ask for code as refusal often offends.
- 02-21-2013, 02:08 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
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)
-
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 02:13 PM.
- 02-21-2013, 03:31 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
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 :)
- 02-21-2013, 04:41 PM #6
Similar Threads
-
Converting a buffered image into an int[] array, then converting it back
By nhmllr in forum New To JavaReplies: 2Last Post: 12-08-2012, 04:32 AM -
Converting JFrame to JApplet is not showing on browser.
By ganesh.gothi@gmail.com in forum Java AppletsReplies: 4Last Post: 06-12-2012, 05:05 PM -
JApplet on MAC
By ld_pvl in forum Java AppletsReplies: 0Last Post: 08-23-2009, 01:40 PM -
JFrame to JApplet or JApplet to JApplet
By ramesh.8189 in forum AWT / SwingReplies: 13Last Post: 02-08-2009, 06:14 AM -
help with converting to JApplet
By Simmy in forum AWT / SwingReplies: 2Last Post: 08-09-2007, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks