Esempio n. 1
0
  public static boolean tellVerticalCar() {
    boolean result = false;
    if (hCar999 instanceof HorizontalCar) {
      HorizontalCar car = (HorizontalCar) hCar999;

      // 判断路中间是否有车,并将结果告知南北方向的车
      if ((car.getX() + car.getWidth()) > TrafficDemo.horizontalLefEdge
          && car.getX() < TrafficDemo.horizontalRightEdge) {
        logger.debug("我正在通过东西路中间, 我的坐标:" + car.getX() + " - " + TrafficDemo.class.getName());
        result = true;
      } else result = false;
    } else if (hCar119 instanceof HorizontalCar) {
      HorizontalCar car = (HorizontalCar) hCar119;

      // 判断路中间是否有车,并将结果告知南北方向的车
      if ((car.getX() + car.getWidth()) > TrafficDemo.horizontalLefEdge
          && car.getX() < TrafficDemo.horizontalRightEdge) {
        logger.debug("我正在通过东西路中间, 我的坐标:" + car.getX() + " - " + TrafficDemo.class.getName());
        result = true;
      } else result = false;
    }

    return result;
  }
Esempio n. 2
0
  /**
   * 主函数
   *
   * @param args
   */
  public static void main(String[] args) {
    TrafficFrame tf = new TrafficFrame("Traffic Demo");
    TrafficDemo td = new TrafficDemo();
    tf.add(td);
    tf.createUI();
    tf.center();

    g2 = (Graphics2D) td.getGraphics();

    // 车110
    vCar110 = new VerticalCar(g2, td);

    // 设置车110的属性
    if (vCar110 instanceof VerticalCar) {
      VerticalCar car = (VerticalCar) vCar110;
      car.setX(508);
      car.setY(-car.getHeight() / 2);
      car.setSpeed(60);
      car.setNorthToSouth(true);
    }

    // 车120
    vCar120 = new VerticalCar(g2, td);

    // 设置车120的属性
    if (vCar120 instanceof VerticalCar) {
      VerticalCar car = (VerticalCar) vCar120;
      car.setX(386);
      car.setY(TrafficFrame.DEFAULT_HEIGHT + car.getHeight() / 2);
      car.setSpeed(30);
      car.setNorthToSouth(false);
    }

    // 车119
    hCar119 = new HorizontalCar(g2, td);

    // 设置车119的属性
    if (hCar119 instanceof HorizontalCar) {
      HorizontalCar car = (HorizontalCar) hCar119;
      car.setX(-car.getWidth() / 2);
      car.setY(258);
      car.setSpeed(60);
      car.setLeftToRight(true);
    }

    // 车999
    hCar999 = new HorizontalCar(g2, td);

    // 设置车999的属性
    if (hCar999 instanceof HorizontalCar) {
      HorizontalCar car = (HorizontalCar) hCar999;
      car.setX(TrafficFrame.DEFAULT_WIDTH + car.getWidth() / 2);
      car.setY(382);
      car.setSpeed(30);
      car.setLeftToRight(false);
    }

    // 交通灯
    light = new TrafficLight(g2);

    // 设置交通灯的位置
    light.setWidth(36);
    light.setHeight(36);
    light.setVerticalRedX(683);
    light.setVerticalRedY(156);
    light.setVerticalGreenX(683);
    light.setVerticalGreenY(206);

    light.setHorizontalRedX(250);
    light.setHorizontalRedY(510);
    light.setHorizontalGreenX(300);
    light.setHorizontalGreenY(510);

    light.addObserver((Observer) vCar110);
    light.addObserver((Observer) vCar120);
    light.addObserver((Observer) hCar119);
    light.addObserver((Observer) hCar999);

    Thread thrVCar110 = new Thread((Runnable) vCar110, "car110");
    thrVCar110.start();

    Thread thrVCar120 = new Thread((Runnable) vCar120, "car120");
    thrVCar120.start();

    Thread thrHCar119 = new Thread((Runnable) hCar119, "car119");
    thrHCar119.start();

    Thread thrHCar999 = new Thread((Runnable) hCar999, "car999");
    thrHCar999.start();

    Thread thrLight = new Thread((Runnable) light);
    thrLight.start();
  }