package Bugs;
import java.awt.*;
    /**
    * A class for incrementally plotting data on canvas
    * update takes vectors of data points
    * stores date prescaled for speed of plotting
    // Dec 97 Frame version should use size.height() to scale Y.**
    * @version 1.1 17 Jan 1997 ; original inspired by StarLogo's plotting feature
    modified 27 March 97 to add x-step; placed in PADJava/ for shared use
    needs default color array @?
    * @author Peter Danielson
    * @extends Canvas
    */
public class Plot extends Canvas{
    Color backColor = new Color(150,160,160);
        boolean newMax = false;
        int[][] value;
        int NumPens;
        double Yscale;
        Color [] DispColor;
	    Canvas canvas;

        /*System colors available:
                Color.lightGray
                Color.red, Color.magenta,
                Color.blue, Color.yellow, Color.green, Color.cyan, Color.pink,
                Color.orange, Color.darkGray};*/
        int Ysize;
        int Xscale = 1; //to scale x
        int xstep = 0;  //the counter
        int maxY, MaxY,Xsize, NumbPens, MaxX;
            /**
            * constructor
            * need way to change default colors
            *@param  NumPens Number of plotting pens
            *@param  MaxY Maximum y value -- will be scaled to Ysize
            *@param  Xsize  X axis length
            *@param  Ysize  Y axis length
            *@param  Colors  An array of (at least NumPens) colors
            */
        //Plot(int NumPens, int MaxY, int Xsize, int Ysize, java.awt.Color [] Colors){
    	//super("Plotter (update)");


void Go(){   //THIS IS A BAD KLUDGE FOR A CONSTRUTOR!! /BH
        //this.NumPens = NumPens;
        //DispColor = Colors;
        this.Ysize = Ysize;
        Xscale = 400/Xsize; // try autoscaling x
        Yscale = MaxY / Ysize; // scale y to MaxY/Ysize
                              //scale is float to avoid truncating Maxy<Ysize
        value = new int [NumPens][Xsize];
        //this.resize(2*Xsize, 80+Ysize); // make canvas current size
        //this.move(5,600-80-Ysize);
}


    /**
    * Updates plot with set of y-values and increments xstep
    * reversal of Y-axis done here rather than in each paint
    * Paints on each update
    *@param  Yval  array of data y-values
    *@return xstep counter for debugging
    */

void update(int [] Yval){
  setBackground(backColor);
     for (int pen = 0; pen < NumPens; pen++){
            value[pen][xstep] = (int)(Ysize - Yval[pen]/Yscale);
                //reverse Y-axis
            if(value[pen][xstep]<=20) newMax = true;
        }
     System.out.println(xstep + "  " + value[0][xstep] + "  " +
              value[1][xstep] + "  " + value[2][xstep]);
    repaint();
  // need paint here but how get Graphics g?
  xstep++; // increment xcounter
  if (xstep >= Xsize) xstep = 0;
  if (newMax) newYscale();
}


    /**
    *@param  Yval  array of data y-values
    * paints plot
    * lacks scales
    * Problem: is values equal, only last plotted shows -
    * alternated order through pens 5 Dec 97 (slower?)
    */

public void paint(Graphics g){
    g.setColor(Color.red.darker());
    g.drawLine(0,Ysize,500,Ysize); //draws the zero line /bh
    for (int x = 0; x < xstep-1; x++) //stop 1 early
	  for (int pen = 0; pen < NumPens; pen++){
	       g.setColor(DispColor[pen]); // many more color changes!!
            g.drawLine(x * Xscale + 2, value[pen][x],
                (x+1)*Xscale + 2, value[pen][x+1]);
      }
}


void newYscale(){
    for (int x = 0; x < xstep +1 ;x++){
       for (int pen=0;pen<NumPens;pen++){
        value[pen][x] = (int)(Ysize/2 + value[pen][x]/2);
       }
    }
    newMax = false;
    Yscale = Yscale * 2;
    System.out.println(" Running newYscale()");
}


public void resetPlot(){
    Yscale = MaxY / Ysize;
    xstep = 0;
 }



} // end Plot Class


