0
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?

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by jackbauer24
0

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, [U]72[/U], 100);
			MidiEvent non = new MidiEvent(a, [U]1[/U]);
			mft.add(non);

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

			mfpl.setSequence(mfs);

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

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.