/** MJToot10.java A subclass of MusicJob that chooses its own Playable each time it repeats. * @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 MJToot10 extends java.applet.Applet { SmartJob mySmartJob; TextArea myTextArea; /* Build our SmartJob 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 SmartJob mySmartJob = new SmartJob(); // set its duration mySmartJob.setRepeatPause(2.0); // set its repeat count mySmartJob.setRepeats(50); } /* When applet starts up, launch it */ public void start() { JMSL.clock = new DefaultMusicClock(); mySmartJob.launch(JMSL.now()); } /* Shut down the Job when applet stops */ public void stop() { mySmartJob.finish(); } } class SmartJob extends MusicJob { // An array of Playable private Playable[] playFunctions; // constructor, initializes array of Playable SmartJob() { // allocate a new array playFunctions = 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 playFunctions[0] = new MessagePrintingPlayable("I am Playable ONE"); playFunctions[1] = new MessagePrintingPlayable("I am Playable TWO"); playFunctions[2] = new MessagePrintingPlayable("I am Playable THREE"); playFunctions[3] = new MessagePrintingPlayable("I am Playable FOUR"); } /** Override repeat() of MusicJob to choose a new repeatFunction */ public double repeat(double playTime) { // pick one of four Playables at random Playable pf = playFunctions[JMSLRandom.choose(playFunctions.length)]; // clear the list of Playables before adding a new one removeAllRepeatPlayables(); // plug it in for this repeat addRepeatPlayable(pf); return playTime; } }