/*----------------------------------------------------------------------+
|    Title:  TimeGraph.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:   May, 2002                                                  |
+----------------------------------------------------------------------*/

import java.awt.*;

public class TimeGraph extends Canvas {

  private Color background; // the background color
  private Color color[];    // the colors associated to the q quantities
  private int history[][];  // the history of the quantities being displayed
  private int point;        // the oldest data point
      
  public void init(Color backgroundIn, Color colorIn[], int widthIn) {
    background = backgroundIn;
    color = colorIn;
    point = 0;
    // see if we need a new history matrix, or if we can use the old one
    if (history == null || history[0].length != widthIn)
      history = new int[widthIn][color.length];
    // reset the histories to 0
    for (int i=0; i<history.length; ++i)
      for (int j=0; j<history[0].length; ++j)
        history[i][j] = 0;
  } // init
  
  public void newData (int data[]) {
     if (history == null) return; // not yet initialized
     if (data.length != history[0].length) return; // wrong data length
     for (int j=0; j<data.length; ++j)
       history[point][j] = data[j];
     point = (point + 1) % history.length;
  }

  private Image offscreen;
  private Dimension offscreensize;
  private 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) {
    if (history == null) return; // not yet initialized
     // first, find the maximum column sum in the history matrix
    int max = 1;
    int sum;
    for (int i=0; i<history.length; ++i) {
      sum = 0;
      for (int j=0; j<history[0].length; ++j)
        sum += history[i][j];
      if (sum > max)
        max = sum;
    }
    // now, draw the graph
    Dimension d = getSize();
    g.setColor(background);
    g.fillRect(0,0,d.width,d.height);
    int left = 0, right;
    for (int i=0; i<history.length; ++i) {
      int m = (point + i) % history.length;
      int oldsum = 0, newsum, low, high;
      right = (i+1)*d.width/history.length;
      for (int j=0; j<history[0].length; ++j)
        if (history[m][j] != 0) {
          g.setColor(color[j]);
          newsum = oldsum + history[m][j];
          low = oldsum*d.height/max;
          high = newsum*d.height/max;
          g.fillRect (left,low,right-left,high-low);
          oldsum = newsum;
        } // if
      left = right;
    } // for i
  }
  
  public void paint(Graphics g) {
    update(g);
  }

  // The purpose of printData is for debugging
  public void printData() {
    for (int i=0; i<history.length; ++i) {
      System.out.print(i);
      for (int j=0; j<history[0].length; ++j)
        System.out.print("\t"+history[i][j]);
      System.out.println();
    }
  }
}
