// UpcDigit class, October 2001
//
// Objects of this class are designed to be used with the UpcClock class. Each
// object of this class is capable of representing a digit from 0 through 9.

import aLibrary.*;
import java.awt.*;

public class UpcDigit extends AView {
// This class derives from an AView

  /*
   * CLASS VARIABLES and constants
   */

  private ARectangle[] unit; // the seven individual units

  private final static int[][] boxWidth =
    {{3, 2, 1},   // 0
     {2, 2, 2},   // 1
     {2, 1, 2},   // 2
     {1, 4, 1},   // 3
     {1, 1, 3},   // 4
     {1, 2, 3},   // 5
     {1, 1, 1},   // 6
     {1, 3, 1},   // 7
     {1, 2, 1},   // 8
     {3, 1, 1}};  // 9
  // A mapping of the width of the boxes comprising each digit. This is pretty
  // weird, so look at it this way. Every digit is seven units wide, and is
  // made up of four distinct regions, two spaces and two bars (either
  // space-bar-space-bar or bar-space-bar-space). So a 0 would be encoded as a
  // 3-unit-wide space, a 2-unit-wide bar, a 1-unit-wide space, and a 1-unit-
  // wide bar; or, a 3-unit-wide bar, a 2-unit-wide space, a 1-unit-wide bar,
  // and a 1-unit-wide space. The fourth width is unnecessary, as we know that
  // every digit is 7 units wide.

  /*
   * CONSTRUCTOR
   */

  public UpcDigit(int x, int y, int w, int h) {
  // Instantiates a new UpcDigit based on (x, y, w, h) coordinates.
  //
  // Preconditions:
  //   none.
  //
  // Postconditions:
  //   A new UpcDigit is created.
  //   This new UpcDigit displays as a blank.

    super(x, y, w, h);  // AView constructor

    double \u0394x = w / 7d;
    // A number to make our calculations easier and less painful to the eyes.
    // For any non-Unicode-speakers, \u0394 is the Greek letter delta.
    // For any non-scientific-types, the Greek letter delta means "change in."

    unit = new ARectangle[7];
    // create array of units

    for (int i = 0; i < 7; i++) {
      unit[i] = new ARectangle((int)((i * \u0394x) + 0.5), 0,
        (int)(((i + 1) * \u0394x) + 0.5) - (int)((i * \u0394x) + 0.5), h);
      unit[i].setToFill();
      unit[i].place(this);
    }
    // Draw all seven segments, fill them in, and place them on this.

  }

  /*
   * UPDATE METHODS
   */

  public void setNumber(int number, boolean rightSide) {
  // Sets the number that this UpcDigit displays.
  //
  // Precondition:
  //   0 <= number <= 9
  //
  // Postconditions:
  //   Various units are set to visible or invisible as necessary to display
  //     the number.
  //   The UpcDigit is not repainted.
  //
  // Note: The digit will go bar-space-bar-space if it's on the right side,
  //   i.e., rightSide == true. Otherwise, it will go space-bar-space-bar.

    int i = 0;
    // Keep track of which bar we're on.

    if (0 <= number && number <= 9) {  // make sure we have a valid number
      for (int j = 0; j < boxWidth[number][0]; j++, i++)
        unit[i].setVisible(rightSide);
      for (int j = 0; j < boxWidth[number][1]; j++, i++)
        unit[i].setVisible(!rightSide);
      for (int j = 0; j < boxWidth[number][2]; j++, i++)
        unit[i].setVisible(rightSide);
      for (; i < 7; i++)
        unit[i].setVisible(!rightSide);
    }

  }

  public void blank() {
  // Sets a UpcDigit to a blank.
  //
  // Preconditions:
  //   none.
  //
  // Postconditions:
  //   All units are set to invisible.
  //   The digit is not repainted.

    for(int i = 0; i < 7; i++)
      unit[i].setVisible(false);

  }

}
