⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 car.java

📁 停车场的程序
💻 JAVA
字号:
package parkproject;

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

class car
    extends JPanel
    implements Runnable {
  int imageid = 3;
  int width = 50, height = 25;
  Image[] carImage = new Image[4];
  String pictureUrl[] = {
      "images/car_upward.jpg", "images/car_downward.jpg",
      "images/car_backward.jpg", "images/car_forward.jpg"};
  private int x, y; //车的左上坐标
  private int stopTime = 0;
  volatile boolean stopFlag = false;
  BufferedImage[] bimage = new BufferedImage[4];
  int x0, y0; //the park place x,y
  int num_storePlace; //record the car's stop place
  String workNum; //worker's Num
  int carNum; // worker's Num
  park mypark;
  int inmoveX = 0;
  int outmoveX = 0;
  int k = 0; //record car's current roadnum
  final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
  int n; //record park's total  placement
  boolean valid_car = false; //indicated the car is valid or not

  //the direction of the car,instructs what picture to load.
  public car(int numid, park mypark) {
    workNum = Integer.toString(numid);
    carNum = numid;
    this.y = 160;
    this.mypark = mypark;
    x0 = mypark.getX0();
    y0 = mypark.getY0();
    this.x = x0 - 75 - 50;
    n = mypark.getN();
    for (int i = 0; i < 4; i++) {
      carImage[i] = this.getToolkit().getImage(ClassLoader.getSystemResource(
          pictureUrl[i]));
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(carImage[i], 1);

      try {
        mt.waitForAll();

      }
      catch (Exception err) {
        err.printStackTrace();
      }
      if (carImage[i].getWidth(this) == -1) {
        System.out.println("Could not get the image");
        System.exit( -1);
      }
      bimage[i] = new BufferedImage(carImage[i].getWidth(this),
                                    carImage[i].getHeight(this),
                                    BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2D = bimage[i].createGraphics();
      g2D.drawImage(carImage[i], 0, 0, this);
    }
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    g2D.drawImage(bimage[imageid], 0, 0, this);
    g.drawString(workNum, 10, 10);
  }

  public void moveForward() {
    x = x + 1;
    inmoveX++;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);

  }

  public void moveBackward() {
    x = x - 1;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);

  }

  public void turnNorth() {

    mypark.setRoadHolder(k, -1);
    mypark.setRoadHolder(k - 1, -1);
    mypark.setRoadHolder(k + 1, carNum);
    y = y0 + 50;
    x = x + 50;
    imageid = 1;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);
    inmoveX = inmoveX + 25;
  }

  public void backTurnNorth() {
    mypark.setRoadHolder(k, -1);
    //mypark.setRoadHolder(k - 1, -1);
    // mypark.setRoadHolder(k - 2, carNum);
    y = y0 + 50;
    imageid = 1;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);

  }

  public void turnSouth() {
    mypark.setRoadHolder(k, -1);
    mypark.setRoadHolder(k - 1, -1);
    mypark.setRoadHolder(k + 1, carNum);
    y = y0 + 50;
    x = x + 50;
    imageid = 0;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);
    inmoveX = inmoveX + 25;
  }

  public void backTurnSouth() {
    mypark.setRoadHolder(k, -1);
    // mypark.setRoadHolder(k - 1, carNum);
    y = y0 + 50;
    imageid = 0;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);
    inmoveX = inmoveX + 25;

  }

  public void turnForward() {
    y = 160;
    imageid = RIGHT;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);
    k = k + 1;

  }

  public void turnBackward() {
    y = 160;
    x = x - 25;
    imageid = RIGHT;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x, y, width, height);
    k = k - 1;

  }

  public void moveSouth(int id) {
    imageid = id;
    y = y + 1;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x + 1, y, width - 1, height);

  }

  public void moveNorth(int id) {
    y = y - 1;
    imageid = id;
    width = carImage[imageid].getWidth(this);
    height = carImage[imageid].getHeight(this);
    this.setBounds(x + 1, y, width - 1, height);

  }

  public void moveBacktoPlace() {
    int emptyPlace;
    int k1, k2;
    boolean backFlag = false;
    emptyPlace = mypark.getEmptyPlace();

    if (emptyPlace == -1)
      return;
    else {

      k1 = (emptyPlace + 1) / 2 + 1;
      k2 = k - 1;

      if (k2 > k1)
        backFlag = canGoback(k1, k2);
      else
        backFlag = true;
    }
    int place[] = new int[10];
    if (backFlag) {
      setbackroadHolder(k1, k2);
      backtoPlace(k2 - k1);
      if (emptyPlace % 2 == 0) {
        backTurnSouth();
        moveSouthHeight(UP);
      }
      else {
        backTurnNorth();
        moveNorthHeight(DOWN);
      }
      mypark.setplaceHolder(emptyPlace, carNum);
      mypark.setRoadHolder(k1, -1);
    }
    else {
      x = x - 25;
      mypark.setRoadHolder(k, 1);
      turnSouth();
      mypark.setRoadHolder(k, -1);
      moveSouthHeight(UP);
      while (!backFlag) {
        emptyPlace = mypark.getEmptyPlace();
        k1 = (emptyPlace + 1) / 2 + 1;
        k2 = k;

        backFlag = canGoback(k1, k2);
        sleepM(100);

      }
      setbackroadHolder(k1, k2);
      moveOutPlace();
      turnBackward();
      k2 = k - 1;
      backtoPlace(k2 - k1);

      if (emptyPlace % 2 == 0) {
        backTurnSouth();
        moveSouthHeight(UP);
      }
      else {
        backTurnNorth();
        moveNorthHeight(DOWN);
      }
      mypark.setRoadHolder(k1, -1);
      mypark.setplaceHolder(emptyPlace, carNum);
    }
  }

  public boolean canGoback(int k1, int k2) {
    return mypark.checkback(k1, k2);
  }

  public void setbackroadHolder(int k1, int k2) {
    for (int i = k1; i < k2; i++) {
      mypark.setRoadHolder(i, carNum);
    }
  }

  public void backtoPlace(int p) { //p indicate how many placewidth
    for (int i = 0; i < p; i++) {
      backMoveCarwidth();
    }

  }

  public void moveToDoor() {
    int i;
    for (i = 0; i < 50; i++) {
      moveForward();
      repaint();
      try {
        Thread.sleep(40);
      }
      catch (InterruptedException e) {}
    }
    if (k > 1) {
      mypark.setRoadHolder(k - 2, -1);
    }
  }

  public void run() {
    boolean del_Flag = false;
    moveToDoor();
    //move to the door
    //read the car number and check it
    valid_car = mypark.checkCar(carNum);
    sleepM(1000);

    while (valid_car) {
      mypark.addCar(this);
      if (!mypark.getdoor1Flag()) {
        mypark.setEntranceDoorFlag();
      }
      this.setBounds(x, y, width, height);
      //wait until door open
      while (!mypark.getdoor1Flag()) {
        sleepM(100);
      }

      while (mypark.getRoadHolder(k + 1) != -1) {
        sleepM(100);
      }
      movecarWidth();

      //move to the first car place and check
      // stopFlag  just function until the car into his placement
      while (!stopFlag) {
        this.setBounds(x, y, width, height);
        while (mypark.getplaceHolder(2 * k) != -1 &&
               mypark.getplaceHolder(2 * k - 1) != -1) {
          while (mypark.getRoadHolder(k + 1) != -1) {
            sleepM(100);
          }
          movecarWidth();
          checkforClose();
        }
        if (mypark.getplaceHolder(2 * k) == -1) {
          // mypark.SetEntranceDoorCloseFlag();
          while (mypark.getRoadHolder(k + 1) != -1) {
            sleepM(100);

          }
          mypark.setplaceHolder(2 * k, carNum);
          num_storePlace = 2 * k;
          turnSouth(); //to stop south
          checkforClose(); //to set door close;
          // this.setBounds(x,y,25,50);
          //
          k++;
          moveSouthHeight(UP);
          mypark.setRoadHolder(k, -1);
        }
        else if (mypark.getplaceHolder(2 * k - 1) == -1) {
          // mypark.SetEntranceDoorCloseFlag();
          while (mypark.getRoadHolder(k + 1) != -1) {
            sleepM(100);

          }

          mypark.setplaceHolder(2 * k - 1, carNum);
          num_storePlace = 2 * k - 1;
          turnNorth();
          checkforClose();
          moveNorthHeight(DOWN);
          k++;
          mypark.setRoadHolder(k, -1);
        }
        // the car stopped in the place
        // to stop a random time it begins to leave the park
        if (y == y0 || y == y0 + 100) {
          stopFlag = true;
          int stopS = (int) (Math.random() * 100);
          //stopS = 2;
          for (int i = 0; i < stopS; i++) {
            try {
              mypark.addTime();
              Thread.sleep(1000);

            }
            catch (Exception e) {}
          }
          boolean temp = false;
          for (int i = 0; i < 10; i++) {
            this.setVisible(temp);
            sleepM(100);
            temp = !temp;
          }
        }
      }
      // to check if it can  get out of the place
      while (true) {
        boolean temp = false;

        if (mypark.getRoadHolder(k) == -1 && mypark.getRoadHolder(k + 1) == -1) {
          synchronized (mypark) {
            mypark.setRoadHolder(k, carNum); //
            mypark.setRoadHolder(k + 1, carNum); //
            break;
          }
        }
        else {
          if (mypark.getRoadHolder(k) == carNum)
            temp = true;
          sleepM(100);
        }
        if (temp)
          break;
      } // until the road before it has no car
      moveOutPlace();
      //leave out of the place
      turnForward();
      // System.out.println("k"+k);
      mypark.setplaceHolder(num_storePlace, -1);
      for (int i = k; i < n + 2; ) {
        if (mypark.getRoadHolder(k + 1) == -1) {
          movecarWidth();
          i++;
        }
        else {
          sleepM(100);
        }
      }
      //move to the exit door
      sleepM(1000);
      //read and check
      boolean out_permit = false;
      out_permit = mypark.checkout(carNum);
      if (out_permit) {
        mypark.setExitDoorFlag();
        while (!mypark.getdoor2Flag()) {
          sleepM(100);
        }
        movecarWidth();
        movecarWidth();
        mypark.SetExitDoorCloseFlag();
      }
      else {
        moveBacktoPlace();
        break;
      }
      mypark.deleteOneCar(carNum);
      movecarWidth();
      this.setVisible(false);
      del_Flag = true;
      sleepM(40);
      if (del_Flag) {
        break;
      }

    }
    if (!valid_car) {
      mypark.setSign();
      this.setVisible(false);
    }
  }

  public void movecarWidth() {

    k++;
    mypark.setRoadHolder(k - 1, carNum);
    mypark.setRoadHolder(k, carNum);

    int i;
    for (i = 0; i < 25; i++) {
      moveForward();
      repaint();
      sleepM(40); //temp
    }
    if (k > 1) {
      mypark.setRoadHolder(k - 2, -1);
    }

  }

  public void backMoveCarwidth() {

    mypark.setRoadHolder(k, -1);
    k--;
    mypark.setRoadHolder(k, carNum);
    mypark.setRoadHolder(k - 1, carNum);

    int i;
    for (i = 0; i < 25; i++) {
      moveBackward();
      repaint();
      sleepM(40);
    }

  }

  public void moveNorthHeight(int imageid) {
    int i;

    for (i = 0; i < 50; i++) {
      moveNorth(imageid);
      repaint();
      sleepM(40);
    }

  }

  public void moveSouthHeight(int imageid) {
    int i;

    for (i = 0; i < 50; i++) {
      moveSouth(imageid);
      repaint();
      sleepM(40);
    }

  }

  public void moveOutPlace() {
    if (num_storePlace % 2 == 0)
      moveNorthHeight(UP);
    else
      moveSouthHeight(DOWN);
  }

  public void sleepM(int c) {
    try {
      Thread.sleep(c);
    }
    catch (InterruptedException e) {}

  }

  public int getNumid() {
    return carNum;
  }

  public void checkforClose() {
    if (inmoveX >= 50) {
      mypark.SetEntranceDoorCloseFlag();
      // System.out.println("called me");
    }
  }

  public void setFlag(boolean b) {
    stopFlag = b;
  }

  public void sprint() {
    for (int i = 0; i < 7; i++) {
      System.out.println(i + "  " + mypark.getRoadHolder(i));
    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -