Class MovingGraph

import java.awt.*;
import java.lang.*;

public class MovingGraph extends Canvas  {
	double data[];
	int nPoints, next;
	double yMax, yMin;
	
	public MovingGraph(int nPointsVal, double yMinVal, double yMaxVal)  {
			setBackground(Color.black);
			nPoints = nPointsVal;
			data = new double[nPoints];
			yMax = yMaxVal;
			yMin = yMinVal;
			clear();
	}
	
	public void clear () {
		next = 0;	
		for (int i=0; i<nPoints; ++i)
		    data[i] = 0.0/0.0;  // not a number
	}
	
	public void paint (Graphics g)  {
		int w = size().width;
		int h = size().height;
	    int wide = (w/nPoints);
		g.setColor(Color.yellow);
		for (int i=0; i<nPoints; ++i)  {
		    	int j = (next+i) % nPoints;
		    	double y = (data[j]-yMin)/(yMax-yMin);
				g.drawLine(i*wide,h-(int)(y*h),(i+1)*wide,h-(int)(y*h));
	    }
	}
	
	public void newData (double d)  {			
	    data[next] = d;
	    if (++next == nPoints) next = 0;
	}
}



Please send comments and suggestions to jdecarolis@clarku.edu
Last updated 8 December 1997