@Override
 public void actionPerformed(ActionEvent e) {
   System.out.println(e.toString());
   if (e.getSource() == buttonUse) {
     state.doUse(this);
   } else if (e.getSource() == buttonAlarm) {
     state.doAlarm(this);
   } else if (e.getSource() == buttonPhone) {
     state.doPhone(this);
   } else if (e.getSource() == buttonExit) {
     System.exit(0);
   } else {
     System.out.println("?");
   }
 }
Пример #2
0
 public void paint(Graphics g) {
   g.setColor(Color.gray);
   g.fillRect(0, 0, w, h);
   g.setColor(Color.black);
   g.drawRect(0, 0, w, h);
   g.setColor(Color.blue);
   g.fillRect(0, h, w, h);
   g.setColor(Color.black);
   g.drawRect(0, h, w, h);
   g.drawString("resting", 10, 20);
   g.drawString("waits to swim", 10, h - 20);
   g.drawString("waits to exit", 10, (3 * h) / 2 - 20);
   g.drawString("swimming", 10, 2 * h - 20);
   if (e == null) return;
   for (int i = 0; i < e.e.length; i++) {
     switch (e.get(i)) {
       case 0:
         rect(g, (i < e.K ? Color.red : Color.green), i, 0);
         break;
       case 1:
         rect(g, (i < e.K ? Color.red : Color.green), i, 1);
         break;
       case 2:
         rect(g, (i < e.K ? Color.red : Color.green), i, 3);
         break;
       case 3:
         rect(g, (i < e.K ? Color.red : Color.green), i, 2);
         break;
       default:
     }
   }
 }
  @Override
  public int onLoop() throws InterruptedException {
    State state = null;
    for (State cur : this.states) {
      if (cur.shouldExecute()) {
        state = cur;
        break;
      }
    }

    if (state == null) {
      return 100;
    }

    this.textualState = state.getTextualState();
    state.onLoop();
    return Core.random(200, 400);
  }
 @Override
 public void setClock(int hour) {
   String clockstring = "現在時間は";
   if (hour < 10) {
     clockstring += "0" + hour + ":00";
   } else {
     clockstring += hour + ":00";
   }
   System.out.println(clockstring);
   textClock.setText(clockstring);
   state.doClock(this, hour);
 }
Пример #5
0
  /**
   * 次の状態へ遷移する。 台車、棒の位置はシミュレーション計算で求める。
   *
   * @param action 力を加える方向。
   */
  public void nextState(int action) {
    // 台車の加速度
    double xAcc;
    // 棒の角加速度
    double thetaAcc;
    // 台車に加える力
    double force;
    // cosθ
    double cosTheta;
    // sinθ
    double sinTheta;
    double temp;

    // actionに応じて力を加える方向を変える
    force = 0;
    switch (action) {
      case 0:
        force = -FORCE_MAG;
        break;
      case 1:
        force = FORCE_MAG;
        break;
    }

    // 台車と棒の運動方程式にしたがって次の状態を決める
    // 台車の詳しい運動方程式はWebを参照してください
    cosTheta = Math.cos(state.theta);
    sinTheta = Math.sin(state.theta);
    temp = (force + POLE_MASS_LENGTH * state.thetaDot * state.thetaDot * sinTheta) / TOTAL_MASS;
    thetaAcc =
        (GRAVITY * sinTheta - cosTheta * temp)
            / (LENGTH * (FOUR_THIRDS - MASS_POLE * cosTheta * cosTheta / TOTAL_MASS));
    xAcc = temp - POLE_MASS_LENGTH * thetaAcc * cosTheta / TOTAL_MASS;

    // 状態を更新する
    state.x += TAU * state.xDot;
    state.xDot += TAU * xAcc;
    state.theta += TAU * state.thetaDot;
    state.thetaDot += TAU * thetaAcc;
  }
Пример #6
0
 /** 環境を初期化する。 */
 public void init() {
   state.init();
 }