/*----------------------------------------------------------------------+
|    Title:  Herd.java                                                  |
|                                                                       |
|    Author: David E. Joyce                                             |
|            Department of Mathematics and Computer Science             |
|            Clark University                                           |
|            Worcester, MA 01610-1477                                   |
|            U.S.A.                                                     |							
		|                                                                       |
|            http://aleph0.clarku.edu/~djoyce/home.html                 |
|            djoyce@clarku.edu                                          |
|                                                                       |
|    Date:   February, 2001.                                            |
+----------------------------------------------------------------------*/

import java.awt.*;

public class TimeGraph extends Canvas {

  int n;            // the number of data points, also the width of the Canvas
  int point;        // the oldest data point
  Color background; // the color for the background of the graph
  int a[],b[],c[];  // the three quantities being displayed
  Color aColor[],bColor[],cColor[];  // the colors associated to them
  
  TimeGraph (int init_n, Color bg_init) {
    n = init_n;
    background = bg_init;
    a = new int[n];
    b = new int[n];
    c = new int[n];
    aColor = new Color[n];
    bColor = new Color[n];
    cColor = new Color[n];
    point = 0;
  }
  
  void clear() {
    for (int i=0; i<n; ++i)
      a[i]=b[i]=c[i]=0;
  }
  
  void newData (int av, int bv, int cv, Color aC, Color bC, Color cC) {
    a[point] = av;
    b[point] = bv;
    c[point] = cv;
    aColor[point] = aC;
    bColor[point] = bC;
    cColor[point] = cC;
    point = (point+1)%n;
    repaint();
  }

  Image offscreen;
  Dimension offscreensize;
  Graphics offgraphics;

  public void update(Graphics g) {
    g.setColor(background);
    Dimension d = getSize();
    if ((offscreen == null) || (d.width != offscreensize.width) 
		|| (d.height != offscreensize.height)) {
      offscreen = createImage(d.width, d.height);
      offscreensize = d;
      offgraphics = offscreen.getGraphics();
      offgraphics.setFont(g.getFont());
    }
    drawGraph(offgraphics);
    g.drawImage(offscreen, 0, 0, null);
  }

  void drawGraph(Graphics g) {
    Dimension d = getSize();
    // first, find the maximum value
    int max = 1;
    int sum;
    for (int i=0; i<n; ++i) {
      sum = a[i] + b[i] + c[i];
      if (sum > max) max = sum;
    }
    // next, paint lines
    for (int i=0; i<d.width; ++i) {
      int m = (point + i) % n;
      g.setColor(aColor[m]);
      g.drawLine (i, 0, i, d.height*a[m]/max);
      g.setColor(bColor[m]);
      g.drawLine (i, d.height*a[m]/max, i, d.height*(a[m]+b[m])/max);
      g.setColor(cColor[m]);
      g.drawLine (i, d.height*(a[m]+b[m])/max, i, d.height*(a[m]+b[m]+c[m])/max);
    }
  }
  
  public void paint(Graphics g) {
    update(g);
  }

}
