/*----------------------------------------------------------------------+
|    Title:  ColorGraph.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 ColorGraph extends Canvas {

  private int historyPoints;
  private int height;
  private int used[];
  private Color background;
  private Color history[][];  // the history of the colors being displayed
  private int point;          // the oldest data point

  public void init(Color background, int height, int historyPoints) {
    this.background = background;
    if (history == null || this.historyPoints != historyPoints || this.height != height) {
      this.historyPoints = historyPoints;
      this.height = height;
      history = new Color[historyPoints][height];
      used = new int[historyPoints];
    }
    point = 0;
     // reset the histories to 0
    for (int i=0; i<historyPoints; ++i)
      used[i] = 0;
  } // init

  public void newData (Color data[], int n) {
     if (history == null) return; // not yet initialized
     if (n > height)
       n = height; // as much as can be displayed
     for (int j=0; j<n; ++j)
       history[point][j] = data[j];
     used[point] = n;
     point = (point + 1) % historyPoints;
  }

  private Image offscreen;
  private Dimension offscreensize;
  private Graphics offgraphics;

  public void update(Graphics g) {
    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
    // 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<historyPoints; ++i) {
      int m = (point + i) % historyPoints;
      int low = 0, high;
      right = (i+1)*d.width/historyPoints;
      for (int j=0; j<used[m]; ++j) {
        g.setColor(history[m][j]);
        high = (j+1)*d.height/height;
        g.fillRect (left,d.height-high,right-left,high-low);
        low = high;
      } // for
      left = right;
    } // for i
  }

  public void paint(Graphics g) {
    update(g);
  }

}

