/** * ShapeDemo.java * * Build a shape, display in a shape editor, print it, play it. * * @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.MusicShapeEditor; import com.softsynth.jmsl.view.TextAreaOut; public class MusicShapeDemo extends java.applet.Applet implements ActionListener { MusicShape myShape; MusicShapeEditor se; TextArea textArea; Button startButton; Button stopButton; public void init() { JMSLRandom.randomize(); JMSL.setIsApplet(true); // a place for printed output setLayout(new BorderLayout()); textArea = new TextArea(10, 80); add("South", textArea); JMSL.setSTDOut(new TextAreaOut(textArea)); Panel p = new Panel(); p.add(startButton = new Button("Start")); p.add(stopButton = new Button("Stop")); stopButton.setEnabled(false); add("Center", p); // build a prefabricated 4 dimensional shape myShape = new MusicShape(4); myShape.prefab(); myShape.print(); myShape.setRepeats(10); myShape.addRepeatPlayable(new MessagePrinter("REPEATS")); // build a shape editor to view the shape se = new MusicShapeEditor(); se.addMusicShape(myShape); add("North", se.getComponent()); startButton.addActionListener(this); stopButton.addActionListener(this); } public void start() { synchronized (JMSL.class) { JMSL.clock = new DefaultMusicClock(); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); } } public void stop() { synchronized (JMSL.class) { myShape.finishAll(); try { myShape.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } void handleStart() { myShape.finish(); try { myShape.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } myShape.launch(JMSL.now()); stopButton.setEnabled(true); startButton.setEnabled(false); } void handleStop() { myShape.finishAll(); 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[]) { MusicShapeDemo applet = new MusicShapeDemo(); Frame f = new Frame("Shape 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); } }