import java.awt.*;

/**
 * A special canvas to hold one vertical bar
 * @author Janne Kalliola <Janne.Kalliola@hut.fi>
 */

public class ExampleCanvas extends Canvas {
  private int value = 0; // the current height of the bar
  private int steps; // the incrementation value

  /**
   * Creates a new instance of ExampleCanvas
   * @param steps the increment value of the bar
   */

  public ExampleCanvas(int steps) {
    super();
    this.steps = steps;
  }

  /**
   * Increments the height of the bar and redraws the canvas
   */

  public void incrementValue() {
    value += steps;
    if (value > getSize().height) {
      value = 0;
    }

    /* If we want to redraw a component, we need to call repaint() as
     * we don't have the object Graphics which is needed by the method
     * paint(). This Graphics is generated by the runtime system as
     * needed.
     */

    repaint();
  }

  /**
   * Clears the height of the bar and the canvas, too.
   */

  public void clearValue() {
    value = 0;
    repaint();
  }

  /**
   * Draws the canvas and the bar
   * @param gfx the graphics context given by the runtime system
   */

  public void paint(Graphics gfx) {
    int h = getSize().height;

    /* Graphics has a lot of different drawing methods. We'll be using
     * fillRect to draw a solid box with the default color. As the fillRect
     * wants to have one pair of coordinates and width and height, we need
     * to do some calculations to make the bar look proper.
     */

    gfx.fillRect(0, h-value, getSize().width, value);
  }
}
