//
//	Copyright 1996 by NetFactory, Inc, Laurel, MD USA
//
// 	Permission to use, copy, modify, and to distribute this software
// 	and its documentation for any purpose is hereby granted without
// 	fee, provided that the above copyright notice appear in all
// 	copies and that both that copyright notice and this permission
//  	notice appear in supporting documentation.
//
// 	NetFactory Inc. disclaims all warranties with regard to this 
// 	software, including all implied warranties of merchantability 
// 	and fitness. In no event shall NetFactory Inc. be liable for any
// 	special, indirect or consequential damages or any damages 
// 	whatsoever resulting from loss of use, data or profits, whether 
// 	in an action of contract, negligence or other tortuous action, 
// 	arising out of or in connection with the use or performance of 
// 	this software
//

import netcharts.graphics.*;
import netcharts.util.*;
import java.awt.*;
import java.applet.*;

//
//	This sample Strip Chart applet contains an NFStripChartApp and a
//	button.  When the button is pressed, a new value is computed and
//	passed to the chart.
//
//	The initial strip chart parameters are input from the HTML
//	document containing this applet.
//

public class NFStripChartDemo1 extends Applet implements Runnable
{
	private NFStripChartApp	strip;
	private Button		button = new Button("Single Step");
	private Checkbox	checkbox = new Checkbox("Auto-Step");
	private double		x = 0;
	private Thread		autostep = null;

	public void init()
	{
		setLayout(new BorderLayout());

		strip = new NFStripChartApp(this);
		strip.init();

		add ("Center", strip);

		Panel flowPanel = new Panel();
		flowPanel.setLayout(new FlowLayout());
		flowPanel.add(button);
		flowPanel.add(checkbox);

		add ("South", flowPanel);
	}

	public void start()
	{
		strip.start();
	}

	public void stop()
	{
		strip.stop();
	}

// This method is called whenever the user presses the button
// or the checkbox.

	public boolean action(Event evt, Object arg)
	{
		if (evt.target == button) {
			genNextStep();
		}

		if (evt.target == checkbox) {

// Note: Don't kill the thread while it is in the middle of generating
// a new value.
			if (autostep != null) {
				synchronized (this) {
					autostep.stop();
					autostep = null;
				}
			}

			if (checkbox.getState() == true) {
				autostep = new Thread(this);
				autostep.start();
			}
		}

		return true;
	}

// This method is called whenever the Auto-Step checkbox is
// set to true.  It periodically generates new values.

	public void run ()
	{
		while (true) {
			try { Thread.sleep(100); }
			catch (Exception e) {};

			genNextStep();

			Thread.yield();
		}
	}

// Generate another set of Y values for the current X value
// and update the strip chart display

	private synchronized void genNextStep ()
	{
		double y;

		y = round(Math.sin(x));
		strip.loadParams("AppendDataSet1 = "+y+";");

		y = round(Math.cos(x));
		strip.loadParams("AppendDataSet2 = "+y+";");

		y = round(2*Math.cos(x/2.0));
		strip.loadParams("AppendDataSet3 = "+y+";");

		y = round(2*Math.sin(x/2.0));
		strip.loadParams("AppendDataSet4 = "+y+";");

		strip.loadParams("Update;");

		x += Math.PI/4;
	}

// Round off value to 3 decimal places

	private double round(double x)
	{
		return Math.rint(x*1000)/1000;
	}
}
