/**
   * Create a big and self-updating clock.
   *
   * @param lcd The graphics context
   * @param alarm When true, the clock can invoke alarms
   */
  public Clock(int x, int y, int width, int height, Graphics lcd, boolean alarm) {
    this.alarm = alarm;
    this.x = x;
    this.y = y;
    radius = width;
    if (height < width) radius = height;
    radius >>= 1;

    clock = new AnalogClock(x, y, radius, true);
    clock.setGraphics(lcd);
    clock.setVisible(true);

    new Thread(this).start();
  }
  /**
   * This method can be run from a global menu context, using the global Keyboard and Display
   * objects.
   */
  public void run() {
    running = true;
    int hours, minutes, seconds;
    {
      Time time = new Time();
      hours = time.hour;
      minutes = time.minute;
      seconds = time.second;
    }

    Deadline dl = new Deadline(1000);
    synchronized (dl) {
      int count = 0;
      Buzzer b = null;
      do {
        clock.setValue(hours, minutes, seconds);
        try {
          if (alarm && count < 20) {
            if (b == null) b = new Buzzer();
            for (int i = 0; i < 4; i++) {
              b.on((short) 880);
              ThreadExt.sleep(120);
              b.off();
              ThreadExt.sleep(30);
            }
            count++;
          }
        } catch (InterruptedException e) {
          count = 20;
        }
        seconds++;
        if (seconds >= 60) {
          seconds -= 60;
          minutes++;
          if (minutes >= 60) {
            minutes -= 60;
            hours++;
            if (hours >= 24) hours -= 24;
          }
          if (RTC.isAlarm()) abort = true;
        }
        if (!abort)
          try {
            dl.append(1000);
          } catch (DeadlineMissException e) {
          }
      } while (!abort);
    }
    running = false;
  }
  /** The main menu with date labels and analog clock. */
  void mainMenu() {

    modify = false;
    mainContainer.removeAll(); // remove all components
    okButton = null; // delete all components for the garbage collector
    cancelButton = null;
    dayChange = null;
    monthChange = null;
    yearChange = null;
    hourChange = null;
    minuteChange = null;
    secondChange = null;
    if (graphics != null) graphics.clearRect(0, 0, 128, 64); // clear screen
    Border b1 = new Border("Datum", 0, 0, 53, 34); // create borders
    mainContainer.add(b1);
    Border b2 = new Border("Uhrzeit", 54, 0, 74, 64);
    mainContainer.add(b2);

    changeButton = new Button("stellen", 2, 51, 40, 11); // the set time button
    mainContainer.add(changeButton);
    changeButton.setActionListener(this);

    clock = new AnalogClock(63, 5, 28, true); // analog clock
    mainContainer.add(clock);

    { // initialize time values
      Time t = new Time();
      years = t.year;
      months = t.month;
      days = t.day;
      clock.setValue(t.hour, t.minute, t.second); // initially draw the clock
    }

    // create date labels
    day = new Label((days < 10 ? "0" : "").concat(String.valueOf(days)).concat("."), 2, 15);
    month = new Label((months < 10 ? "0" : "").concat(String.valueOf(months)).concat("."), 15, 15);
    year = new Label(String.valueOf(years), 28, 15);
    // add date labels
    mainContainer.add(day);
    mainContainer.add(month);
    mainContainer.add(year);

    changeButton.requestFocus();

    // start time requester thread
    timeRequester = new TimeRequester();
    timeRequester.start();
  }