The
applet used for this tutorial
should
appear below.
The yellow area above is the applet frame. Click here to view or download the HTML source Let's have a closer look at the HTML file.
The important stuff in the HTML code above is between the <applet> tags. The location of the class file that is the compiled applet is passed as the 'code' argument. Note that in this case the class file is assumed to be in the same location as the html file. Importantly, you need to tell the applet where to access the jMusic classes. The easiest way is to copy the jmusic.jar file and put it in the same folder as the html and class file, then indicate the location as shown as the 'archive' argument. The size (width and height0 can be wahtever is necessary and will define the amount of screen realestate the applet occupies. The string argument for 'alt' is the text that will display inthe browser if java is not availible to or supported by that browser. Let's have a closer look at the Java file.
Import the classes required for the
applet and button clicks, as well as the jMusic pakcages required.
Applets need to extends the Applet
class. We implement the JMc to get all the jmusic constants and
implement the ActionListener to notify us of the button clicks.
Applets always run an init method
then a start method. So these must be implemented. They can bconsidered
as similar in function to the contructor methods in a regular Java
class. The Applet provides a graphics context and frame by default, so
we simply add the other components we want to this instance.
When we implement the ActionListener
interface we must also include the actionPerformed method, which is
called whener the button is clicked. If the playBtn button was clicked
then the music is composed and then played.
This method adds specified notes to
the score to play a simple melody. See other introductory
tutorials on jMusic is this does not make sense to you.
The play method is simple - perhaps
it should even be a method, of well - it calls jMusic's Play class to
send the score for playback via Java's JavaSound software synthesizer. Note: You must have Java 1.3 or
higher installed for this feature to work. Some browers run earlier
version of Java and so there will be no sound output from them.
|
|
|
|
© 2003 Andew R Brown |
|
|