/** MJToot11.java A subclass of MusicJob that chooses its own Playable each time it repeats. Uses a PlayableProvider class. * @author Phil Burk and Nick Didkovsky */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved * JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom. */ package jmsltutorial; import java.awt.TextArea; import com.softsynth.jmsl.*; public class MJToot11 extends java.applet.Applet { SmarterJob mySmarterJob; TextArea myTextArea; /* Build our SmarterJob when applet initializes, send JMSL's STDOut to a TextArea */ public void init() { // initialize the TextArea with 20 rows, 50 columns myTextArea = new TextArea(20, 50); // Add the TextArea to the applet's layout add(myTextArea); // Hand this TextArea to a new TextAreaSTDOut, and use it for JMSL.out JMSL.setSTDOut(new TextAreaSTDOut(myTextArea)); // initialize a new SmarterJob mySmarterJob = new SmarterJob(); // set its duration mySmarterJob.setRepeatPause(2.0); // set its repeat count mySmarterJob.setRepeats(50); } /* When applet starts up, launch it */ public void start() { JMSL.clock = new DefaultMusicClock(); mySmarterJob.launch(JMSL.now()); } /* Shut down the Job when applet stops */ public void stop() { mySmarterJob.finish(); } } /* A class containing Playables, accessable to all classes */ class PlayableProvider { // An array of Playable private static Playable[] playables; // constructor, initializes array of Playable static { // allocate a new array playables = new Playable[4]; // fill it with four unique Playables // Note that they did NOT all have to be MessagePrintingPlayable - just for convenience // Could plug in any class that implements Playable playables[0] = new MessagePrintingPlayable("I am Playable ONE"); playables[1] = new MessagePrintingPlayable("I am Playable TWO"); playables[2] = new MessagePrintingPlayable("I am Playable THREE"); playables[3] = new MessagePrintingPlayable("I am Playable FOUR"); } public static Playable getRandomPlayable() { return playables[JMSLRandom.choose(playables.length)]; } } class SmarterJob extends MusicJob { /** Override repeat() of MusicJob to choose a new repeatFunction */ public double repeat(double playTime) { removeAllRepeatPlayables(); // get a play function from PlayableProvider and plug it in addRepeatPlayable(PlayableProvider.getRandomPlayable()); return playTime; } }