package jmslexamples; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.view.TextAreaOut; /** * BehavioralColDemo.java * * Two jobs stuffed into a SequentialCollection, then launched. Each job has its * own timing and repeat count. The collection chooses one of these two jobs to * fire every time it repeats by acting "behaviorally". * * @author Nick Didkovsky and Phil Burk, (c) 2003 all rights reserved */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved JMSL is based upon * HMSL (C) Phil Burk, Larry Polansky and David Rosenboom */ public class BehavioralColDemo extends java.applet.Applet implements ActionListener { TextJob tj1; TextJob tj2; SequentialCollection col; // a collection to hold both jobs DrawingCanvas canvas1; // one for Job 1 DrawingCanvas canvas2; // one for Job 2 TextArea textArea; Button startButton; Button stopButton; public void init() { JMSLRandom.randomize(); canvas1 = new DrawingCanvas(); canvas2 = new DrawingCanvas(); textArea = new TextArea(20, 80); setLayout(new GridLayout(0, 1)); add(canvas1); add(canvas2); Panel p = new Panel(); p.add(startButton = new Button("Start")); p.add(stopButton = new Button("Stop")); stopButton.setEnabled(false); add(p); add(textArea); startButton.addActionListener(this); stopButton.addActionListener(this); buildHierarchy(); } void buildHierarchy() { // constructor method has a place to draw, a message, and a repeat delay tj1 = new TextJob(canvas1, "This job, when chosen, changes color 3 times per second", 0.3333); tj1.addRepeatPlayable(new FlasherFunction()); tj1.setRepeats(6); tj2 = new TextJob(canvas2, "This job, when chosen, changes color every second", 1.0); tj2.addRepeatPlayable(new FlasherFunction()); tj2.setRepeats(2); JMSL.setSTDOut(new TextAreaOut(textArea)); col = new SequentialCollection(tj1, tj2); col.setBehavior(new UniformRandomBehavior()); // uniformly random // selection of one child // every repeat col.setRepeats(30); col.addStartPlayable(new MessagePrinter("STARTS")); col.addRepeatPlayable(new MessagePrinter("REPEATS")); col.addStopPlayable(new MessagePrinter("STOPS")); col.print(); } public void start() { synchronized (JMSL.class) { JMSL.clock = new DefaultMusicClock(); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); } } void handleStart() { handleStopCol(); col.launch(JMSL.now()); startButton.setEnabled(false); stopButton.setEnabled(true); } public void stop() { synchronized (JMSL.class) { handleStopCol(); JMSL.scheduler.stop(); } } private void handleStopCol() { col.finish(); try { col.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } startButton.setEnabled(true); stopButton.setEnabled(false); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) handleStart(); if (source == stopButton) handleStopCol(); } public static void main(String args[]) { BehavioralColDemo applet = new BehavioralColDemo(); Frame f = new Frame("Behavioral Collection Demo"); f.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); f.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); f.setSize(600, 600); f.setVisible(true); } }