/** * ParallelCollectionDemo.java * * Two jobs stuffed into a ParallelCollection, then launched. Each job has its * own timing and repeat count. * * @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 jmslexamples; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.view.TextAreaOut; public class ParallelCollectionDemo extends java.applet.Applet implements ActionListener { TextJob tj1; TextJob tj2; ParallelCollection 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")); add(p); add(textArea); buildHierarchy(); startButton.addActionListener(this); stopButton.addActionListener(this); } void buildHierarchy() { // constructor method has a place to draw, a message, and a repeat delay tj1 = new TextJob(canvas1, "This job changes color 3 times per second", 0.3333); tj1.addRepeatPlayable(new FlasherFunction()); tj1.setRepeats(12); tj2 = new TextJob(canvas2, "This job changes color every second", 1.0); tj2.addRepeatPlayable(new FlasherFunction()); tj2.setRepeats(4); JMSL.setSTDOut(new TextAreaOut(textArea)); col = new ParallelCollection(tj1, tj2); col.setRepeats(3); 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(); } } public void stop() { synchronized (JMSL.class) { col.finishAll(); try { col.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } void handleStart() { col.finishAll(); col.launch(JMSL.now()); } void handleStop() { col.finishAll(); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) handleStart(); if (source == stopButton) handleStop(); } /* Can be run as either an application or as an applet. */ public static void main(String args[]) { ParallelCollectionDemo applet = new ParallelCollectionDemo(); Frame f = new Frame("Parallel 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(800, 600); f.setVisible(true); } }