// Digit class, September/October 2001
//
// Designed for Brian's Superbly Incredible and Well-Commented Non-Functional
// Clock.
//
// Objects of this class are designed to be used with the DigitalClockDisplay
// class. Each object of this class is capable of displaying one digit from
// 0 through 9.

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

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

  /*
   * CLASS VARIABLES and constants
   */

  private ARectangle[] lcd; // the seven LCD segments in this digit

  private final static boolean[][] boxLit =
    {{true , true , true , false, true , true , true },   // 0
     {false, false, true , false, false, true , false},   // 1
     {true , false, true , true , true , false, true },   // 2
     {true , false, true , true , false, true , true },   // 3
     {false, true , true , true , false, true , false},   // 4
     {true , true , false, true , false, true , true },   // 5
     {true , true , false, true , true , true , true },   // 6
     {true , false, true , false, false, true , false},   // 7
     {true , true , true , true , true , true , true },   // 8
     {true , true , true , true , false, true , true }};  // 9
  // A mapping of which segments are lit for each digit

  /*
   * CONSTRUCTOR
   */

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

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

    double xUnit = w / 6d;
    double yUnit = h / 13d;
    // numbers to make our calculations easier and less painful to the eyes

    lcd = new ARectangle[7];
    // create array of LCD segments

    lcd[0] = new ARectangle((int)((0.5*xUnit) + 0.5), 0,
                            (int)((5*xUnit) + 0.5), (int)(yUnit + 0.5));
    lcd[1] = new ARectangle(0, (int)((1.5*yUnit) + 0.5),
                            (int)(xUnit + 0.5), (int)((4.75*yUnit) + 0.5));
    lcd[2] = new ARectangle((int)((5*xUnit) + 0.5), (int)((1.5*yUnit) + 0.5),
                            (int)(xUnit + 0.5), (int)((4.75*yUnit) + 0.5));
    lcd[3] = new ARectangle((int)((1.5*xUnit) + 0.5), (int)((6*yUnit) + 0.5),
                            (int)((3*xUnit) + 0.5), (int)(yUnit + 0.5));
    lcd[4] = new ARectangle(0, (int)((6.75*yUnit) + 0.5),
                            (int)(xUnit + 0.5), (int)((4.75*yUnit) + 0.5));
    lcd[5] = new ARectangle((int)((5*xUnit) + 0.5), (int)((6.75*yUnit) + 0.5),
                            (int)(xUnit + 0.5), (int)((4.75*yUnit) + 0.5));
    lcd[6] = new ARectangle((int)((0.5*xUnit) + 0.5), (int)((12*yUnit) + 0.5),
                            (int)((5*xUnit) + 0.5), (int)(yUnit + 0.5));
    // Draw all seven segments.

    for(int i = 0; i < 7; i++) {
      lcd[i].setToFill();
      lcd[i].setColor(Color.red);
    }
    // Set each segment to be filled in, color red.

  }

  /*
   * UPDATE METHODS
   */

  public void setNumber(int number) {
  // Sets the number that this digit displays.
  //
  // Precondition:
  //   0 <= number <= 9
  //
  // Postconditions:
  //   LCD segments are placed or removed as necessary to display the number.
  //   The digit is not repainted.

    if (0 <= number && number <= 9) // make sure we have a valid number
      for(int i = 0; i < 7; i++)
        if (boxLit[number][i]) // this LCD should be lit
         lcd[i].place(this);
        else if (lcd[i].parentContainer() != null) // can't remove if not placed
          lcd[i].remove();

  }

  public void blank() {
  // Sets a digit to a blank (no LCDs lit).
  //
  // Preconditions:
  //   none.
  //
  // Postconditions:
  //   All LCD segments are removed from this digit.
  //   The digit is not repainted.

    for(int i = 0; i < 7; i++)
      if (lcd[i].parentContainer() != null) // can't remove if not placed
        lcd[i].remove();

  }

}
