package jmslexamples; /** * JobDemo1.java * * Two jobs launched at the same time, each with their own schedule, painting * independent messages in an Applet * * @author 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. */ import java.awt.*; import java.awt.event.*; import com.softsynth.jmsl.*; import com.softsynth.jmsl.view.*; public class JobDemo1 extends java.applet.Applet implements ActionListener { TextJob tj1; TextJob tj2; 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); add(textArea); Panel p = new Panel(); p.add(startButton = new Button("Start")); p.add(stopButton = new Button("Stop")); stopButton.setEnabled(false); add(p); startButton.addActionListener(this); stopButton.addActionListener(this); buildJobs(); } void buildJobs() { // constructor method has a place to draw, a message, and a repeat delay tj1 = new TextJob(canvas1, "This job changes color every second", 1.0); tj1.addRepeatPlayable(new FlasherFunction()); tj2 = new TextJob(canvas2, "This job changes color every 2 seconds", 2.0); tj2.addRepeatPlayable(new FlasherFunction()); JMSL.setSTDOut(new TextAreaOut(textArea)); tj1.print(); tj2.print(); } public void start() { synchronized (JMSL.class) { JMSL.clock = new DefaultMusicClock(); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); } } public void stop() { synchronized (JMSL.class) { tj1.finishAll(); tj2.finishAll(); try { tj1.waitForDone(); tj2.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); } } void handleStart() { double when = JMSL.now(); tj1.launch(when); tj2.launch(when); stopButton.setEnabled(true); startButton.setEnabled(false); } void handleStop() { tj1.finish(); tj2.finish(); stopButton.setEnabled(false); startButton.setEnabled(true); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) handleStart(); if (source == stopButton) handleStop(); } public static void main(String args[]) { JobDemo1 applet = new JobDemo1(); Frame f = new Frame("Job Demo 1"); 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); } } // end applet code