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

  private int n;            // the number of bars
  private Color color[];    // their colors
  private Stats stats[];    // their values--means and standard deviations
  private Color background; // the background color

  public void init(int nIn, Color backgroundIn, Color barcolor) {
    n = nIn;
    background = backgroundIn;
    color = new Color[n];
    stats = new Stats[n];
    for (int i=0; i<n; ++i) {
      color[i] = barcolor;
    }
  } // init
  

  public void setValues (Stats statsIn[]) {
    stats = statsIn;
  }

  public void setValue (int i, Stats statsIn) {
    stats[i] = statsIn;
  }
  
  public void setBarColor(int i, Color barcolor) {
    if (i<0 || i >= n) return;
    color[i] = barcolor;
  }

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

  public void update(Graphics g) {
    if (color == null) return; // not initialized
    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 (stats == null|| stats[0] == null) return; // not yet initialized
    Dimension d = getSize();
    g.setColor(background);
    g.fillRect(0,0,d.width,d.height);
    int meanBarWidth = (int)(0.7*d.width/n);
    int meanBarHalfWidth = meanBarWidth/2;
    for (int i=0; i<n; ++i) {
      g.setColor(color[i]);
      int left = (int)((i+0.5)*d.width/n) - meanBarHalfWidth;
      int meanHeight = (int)((1.0-stats[i].mean)*d.height);
      int stdevHeight = (int)(stats[i].standardDeviation*d.height);
      g.fillRect (left,meanHeight-stdevHeight,
                  meanBarWidth+2,2*stdevHeight);
      g.setColor(Color.black);
      g.drawLine (left-1,meanHeight,left+meanBarWidth+2,meanHeight);
    } // for i
  }
  
  public void paint(Graphics g) {
    update(g);
  }

}

