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

📄 main.java

📁 用Java实现的电梯模拟程序。某公司要建造一栋n层的办公楼并用“最先进“的电梯进行装备
💻 JAVA
字号:
//package wuxf;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Main extends Applet implements Runnable{
  private Thread liftThread=null;
  TextArea textArea1 = new TextArea(50,50);
  Button button1 = new Button();
  //Label label1 = new Label();
  Image liftImg;
  Image personImg;
  Image offScreenImg;
  Graphics offScreenG;
  int personNumber=0;
  int x=14;
  int y=517;
  int xi=0;
  int xj=0;
  //int flag=0;
  //int iconNum[]=new int[10];
  AllFloors af=new AllFloors();
  Elevator elvt=new Elevator();
  BL al=new BL();


  public void init() {

 try {jbInit();}
    catch(Exception e) {}
  }
  private void jbInit() throws Exception {
    liftImg=this.getImage(getCodeBase()," floor.GIF");
    personImg=this.getImage(getCodeBase()," person.GIF");
    this.setLayout(null);
    add(textArea1);
    textArea1.setText("以下是进出情况:"+"\n");
    button1.setForeground(Color.black);
    button1.setLabel("产生一个人");
    button1.setBounds(new Rectangle(360, 530, 250, 60));
     textArea1.setBackground(Color.pink);

    textArea1.setBounds(new Rectangle(360, 5, 250, 510));
    this.add(textArea1);
     this.add(button1, null);
    button1.addActionListener(al);
  }

  class BL implements ActionListener{
      public void  actionPerformed(ActionEvent e) {
         xi=0;xj=0;
         while(xi==xj||xi==0||xj==0){
            xi=(int)(Math.random()*9);
            xj=(int)(Math.random()*9);}
          personNumber++;
          //System.out.println("person "+personNumber+" from:"+xi+"to:"+xj);
          textArea1.append("person "+personNumber+" from: "+xi+" to: "+xj+"\n");
         af.allFloors[xi-1].addANewPerson(xj,personNumber);//往相应楼层加一个人
         repaint();
      }}

  public void paint(Graphics g){
    g.drawImage(liftImg,0,0,this);
    g.setColor(Color.blue);
    g.fillRect(x,y,80,70);
    for(int i=0;i<8;i++){
      for(int j=0;j<af.allFloors[i].numberOfWaiting();j++){
          g.drawImage(personImg,100+j*15,550-i*73,this);
      }}
  }
  public void update(Graphics g){
     if(offScreenG!=null){
         paint(offScreenG);
         g.drawImage(offScreenImg,0,0,this);}
     else paint(g);
  }

public void start(){
    offScreenImg=createImage(800,600);
    offScreenG=offScreenImg.getGraphics();
    repaint();
    if(liftThread==null){
         liftThread=new Thread(this,"Elevator");
         liftThread.start();   }
  }

public void run(){
  Thread myThread = Thread.currentThread();
  while(liftThread==myThread){
           repaint();
          while(elvt.status==0){
               if(af.allFloors[elvt.currentFloor-1].upkey||af.allFloors[elvt.currentFloor-1].downkey){
                   if (elvt.addPersons(af)==true){
                      try { Thread.sleep(1000);}catch (InterruptedException e) {}}}
                    af.allFloors[elvt.currentFloor-1].kickPersons(elvt,af);
                elvt.updateStatus(af);}


         for(int i=0;i<73;i++){
            repaint();
            try { Thread.sleep(10);}catch (InterruptedException e) {}
            if (elvt.status == 1)y = y - 1;
            else y=y+1;}

         elvt.changeCurrentFloor();

        if(elvt.kickPersons(this)==true){
           try {  Thread.sleep(1000);}catch (InterruptedException e) {}};
        if(elvt.addPersons(af)==true){
              try { Thread.sleep(1000);}catch (InterruptedException e) {}}
          af.allFloors[elvt.currentFloor-1].kickPersons(elvt,af);
          elvt.updateStatus(af);

    }
  }
  public void stop(){
   liftThread=null;
 }
}

⌨️ 快捷键说明

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