/*----------------------------------------------------------------------+
|    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 Color history[][];  // the history of the colors being displayed
  private int point;          // the oldest data point

  public void init(Color background, int widthIn, int heightIn) {
    point = 0;
    // see if we need a new history matrix, or if we can use the old one
    if (history == null ||  history.length != heightIn || history[0].length != widthIn)
      history = new Color[heightIn][widthIn];
    // 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] = background;
  } // init

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

  public void paint(Graphics g) {
    update(g);
  }

}

