954,052 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How's my son's (extremely beginner) app?

import javax.sound.midi.*;
import javax.swing.*;
import java.awt.event.*;

public class BPlay implements ActionListener {
	public static void main(String[] args) {
		BPlay stereo = new BPlay();
		stereo.play();
	}
	public void play() {
		JFrame myfirstFrame = new JFrame();
		JButton pl = new JButton("Click me to listen to something");
		

            myfirstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
   		pl.addActionListener(this);

		myfirstFrame.getContentPane().add(pl);
		myfirstFrame.setSize(300, 300);
		myfirstFrame.setVisible(true);

	}
	public void actionPerformed(ActionEvent event) {
		try {
			Sequencer mfpl = MidiSystem.getSequencer();
			mfpl.open();
			Sequence mfs = new Sequence(Sequence.PPQ, 4);

			Track mft = mfs.createTrack();
				
			ShortMessage a = new ShortMessage();
			a.setMessage(144, 1, 72, 100);
			MidiEvent non = new MidiEvent(a, 1);
			mft.add(non);

			ShortMessage b = new ShortMessage();
			b.setMessage(128, 1, 72, 100);
			MidiEvent noff = new MidiEvent(b, 1);
			mft.add(noff);

			mfpl.setSequence(mfs);

			mfpl.start();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}


This is my son's first app, how did you like it?

jackbauer24
Posting Whiz in Training
201 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

You are free to change the underlined numbers -

import javax.sound.midi.*;
import javax.swing.*;
import java.awt.event.*;

public class BPlay implements ActionListener {
	public static void main(String[] args) {
		BPlay stereo = new BPlay();
		stereo.play();
	}
	public void play() {
		JFrame myfirstFrame = new JFrame();
		JButton pl = new JButton("Click me to listen to something");
		

            myfirstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
   		pl.addActionListener(this);

		myfirstFrame.getContentPane().add(pl);
		myfirstFrame.setSize(300, 300);
		myfirstFrame.setVisible(true);

	}
	public void actionPerformed(ActionEvent event) {
		try {
			Sequencer mfpl = MidiSystem.getSequencer();
			mfpl.open();
			Sequence mfs = new Sequence(Sequence.PPQ, 4);

			Track mft = mfs.createTrack();
				
			ShortMessage a = new ShortMessage();
			a.setMessage(144, 1, 72, 100);
			MidiEvent non = new MidiEvent(a, 1);
			mft.add(non);

			ShortMessage b = new ShortMessage();
			b.setMessage(128, 1, 72, 100);
			MidiEvent noff = new MidiEvent(b, 1);
			mft.add(noff);

			mfpl.setSequence(mfs);

			mfpl.start();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
jackbauer24
Posting Whiz in Training
201 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

Not sure what you are expecting from us to hear. Program is functional. However there are things that could b changed, like using more define names for parameters and method, splitting application in more classes to make it more clear what component is doing what.
So please, when you create post make sure you clearly state what you expect from us to advice on.

peter_budo
Code tags enforcer
Moderator
15,432 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 900
 

1. How old is your son?
2. What are you expecting him to learn?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
1. How old is your son? 2. What are you expecting him to learn?

1. ten

2. he knows (basic) - python, java, c#, html, css

jackbauer24
Posting Whiz in Training
201 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You