/*----------------------------------------------------------------------+
|    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.*;
import java.awt.event.*;

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 background, Color barcolor) {
    n = nIn;
    this.background = background;
    color = new Color[n];
    stats = new Stats[n];
    for (int i=0; i<n; ++i) {
      color[i] = barcolor;
    }
  } // init

  public void setValues (Stats stats[]) {
    this.stats = stats;
  }

  public void setValue (int i, Stats stats) {
    this.stats[i] = stats;
  }
  
  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 barWidth = (int)(0.7*d.width/n);
    int barHalfWidth = barWidth/2;
    for (int i=0; i<n; ++i) {
      int left = (int)((i+0.5)*d.width/n) - barHalfWidth;
      // draw a vertical line
      g.setColor(color[i]);
      g.drawLine(left+barHalfWidth,0,left+barHalfWidth,d.height);
      // draw the rectangle
      int meanHeight = (int)((1.0-stats[i].mean)*d.height);
      int stdevHeight = (int)(stats[i].standardDeviation*d.height);
      g.fillRect (left,meanHeight-stdevHeight,
                  barWidth+2,2*stdevHeight);
      // draw the black line representing the mean
      g.setColor(Color.black);
      g.drawLine(left-1,meanHeight,left+barWidth+1,meanHeight);
    } // for i
  }
  
  public void paint(Graphics g) {
    update(g);
  }

  // determine which bar a click occurred on
  public int getBar(int x, int y) {
    Dimension d = getSize();
    for (int i=0; i<n; ++i)
      if (x < d.width*(i+1)/n)
        return i;
    return -1;
  }

}

