/**
 * Add algorithmically generated notes to a JMSL score. Notice that when there
 * are more notes than there are measures to accomodate them, that measures are
 * added as needed.
 * 
 * @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.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.softsynth.jmsl.JMSL;
import com.softsynth.jmsl.score.NoteFactory;
import com.softsynth.jmsl.score.Score;
import com.softsynth.jmsl.score.ScoreFrame;
import com.softsynth.jmsl.score.SelectionBuffer;

public class JScoreToot03 extends JFrame {

    Score score;
    ScoreFrame scoreFrame;

    
    public void init() {
        setBackground(Color.yellow);
        add(new JLabel("Your JMSL score will open in a new window"));
    }

    public void start() {
        JMSL.clock.setAdvance(0.1);

        // 2 staves, width, height
        score = new Score(2, 800, 400);
        score.addMeasure();
        scoreFrame = new ScoreFrame();
        scoreFrame.addScore(score);

        // 4 quarter notes
        double dur = 1.0; // qtr note
        for (int i = 0; i < 4; i++) {
            score.addNote(dur, NoteFactory.MIDDLE_C + i * 3, 0.5, dur * 0.8);
        }
        scoreFrame.setVisible(true);
        scoreFrame.setSize(900, 600);
        score.render();
    }

    
    public void stop() {

        if (scoreFrame != null) {
            scoreFrame.setVisible(false);
            scoreFrame.dispose();
            Score.deleteCanvas();
            SelectionBuffer.disposeEditFrame();
            scoreFrame = null;
            score = null;
        }
        JMSL.scheduler.stop();
        JMSL.closeMusicDevices();

    }

    public static void main(String args[]) {

        JScoreToot03 test = new JScoreToot03();
        test.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                test.stop();
                System.exit(0);
            }
        });

        test.init();
        test.start();
        test.pack();
        test.setVisible(true);
    }

}