import jas.hist.*;
import javax.swing.*;
import java.util.Observable;

public class Example3 extends JFrame
{
	Example3()
	{
		super("Example 3");
		JASHist	plot = new JASHist();
							 
		plot.addData(new RandomChangingDataSource()).show(true);
							  
		getContentPane().add(plot);
		setSize(400,400);
		show();
	}
	public static void main(String[] argv)
	{
		new	Example3();
	}
}
// Note	that this class	extends	Observable so that it can notify
// the plot	(JASHist) when the data	changes.
class RandomChangingDataSource extends Observable 
	  implements Rebinnable1DHistogramData,	Runnable
{
	RandomChangingDataSource()
	{
		setData(); // Set initial data
		// Create a	thread to update the data, it will call	the	run
		// method in this class.
		Thread t = new Thread(this);
		t.start(); 
	}
	private	void setData()
	{
		int	bins = (int) (Math.random()*40)	+ 10;
		double scale = 100 * Math.random();
		double[] newData = new double[bins];
		for	(int i=0; i<newData.length;	i++) 
			newData[i] = scale*Math.random();
					 
		data = newData;
	}
	public void	run()
	{
		try
		{
			// Create the HistogramUpdate object that will be sent
			// to all our Observers	(specifically the JASHist).	This
			// HistogramUpdate will	specify	that both the bin contents
			// (data) and the axis min/max (range) have	changed. The 
			// boolean argument	to the HistogramUpdate constructor 
			// indicates that this is a	"final"	update,	which the 
			// JASHist should resond to	immediately.
			int	flags =	HistogramUpdate.DATA_UPDATE	| 
						HistogramUpdate.RANGE_UPDATE;
			HistogramUpdate	hu = new HistogramUpdate(flags,true);

			Thread.sleep(5000);	// wait	5 seconds before first update
			for	(;;)
			{
				setData(); // update the data
				setChanged(); // notifyObservers needs this
				notifyObservers(hu);
				Thread.sleep(1000);	// wait	1 second
			}
		}
		catch (InterruptedException	e) {}
	}
	public double[][] rebin(int	rBins, double rMin,	double rMax, 
							boolean	wantErrors,	boolean	hurry)
	{
		double[][] result =	{ data };
		return result;
	}
	public String[]	getAxisLabels()	{ return null; }
	public double getMin() { return	0; }
	public double getMax() { return	data.length; }
	public boolean isRebinnable() {	return false; }
	public int getBins() { return data.length; }
	public int getAxisType() { return DOUBLE; }
	public String getTitle() { return "Changing	Data Source"; }
				 
	private	double[] data;	
}
