/* Tracker is the canvas class on which the rocket trajectory is drawn */ 

import java.awt.*; 

public class Tracker extends Canvas 

{  Launcher L; 
   int h,w,psz,cursor; // height & width of the tracker; plot size  
   final int m=25;     // margin for plot 
   int[] xp,yp;        // arrays used to hold plot curve coordinates 
   boolean plotExists; // tells whether a curve exists to plot 
 
   Tracker(Launcher parent, int height, int width) 

   {  int idx; 

      L = parent; 
      setBackground(Color.black); 
      setForeground(Color.yellow); 
      h = height; 
      w = width;  
      psz = w-2*m; 
      cursor = m + psz/2; 
      xp = new int[psz]; 
      yp = new int[psz]; 
      for (idx=0; idx<psz; idx++) xp[idx] = yp[idx] = 0; 
      plotExists = false; 
   }  

   public void paint(Graphics g) 
   
   {  int idx; 

      g.drawLine(m,m,m,h-m);  
      g.drawLine(m,h-m,w-m,h-m); 
      g.setColor(Color.blue);
      if (plotExists) for (idx=1; idx<psz; idx++) 
        g.drawLine(xp[idx-1],yp[idx-1],xp[idx],yp[idx]);  
      g.setColor(Color.lightGray); 
      if (plotExists) g.drawLine(cursor,m+1,cursor,h-(m+1)); 
      g.setColor(Color.white); 
      g.drawString("Rocket Tracker", w/2-35,m-5);
   }  

   void update(double[] y, double dt, double t) 

   {  int idx,index,asz; 
      double scale,yMax,tmax; 

      asz = y.length; 
      tmax = Math.min(2*t,(double)(asz-1)/100); 
      cursor = m+(int)Math.round(psz*t/tmax); 
      yMax = 0;  
      index = (int)Math.round(tmax/dt); 
      for (idx=0; idx<index; idx++) yMax = Math.max(y[idx],yMax); 
      scale = yMax>0 ? (h-2*m)/yMax : 1.0; 
      for (idx=0; idx<psz; idx++) 
      {  xp[idx] = m+idx; 
         index   = (int)Math.round((tmax/dt)*(double)idx/psz); 
         yp[idx] = (h-m)-(int)Math.round(scale*y[index]); 
      } 
      plotExists = true; 
      repaint(); 
   }  
}     