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

📄 crossroad.java

📁 交通灯多线程
💻 JAVA
字号:
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;

public class Crossroad extends Frame implements Runnable{
  Road r1,r2;//南北方向和东西方向两条路
  Car c1,c2,c3;//这两条路上的车辆
  TraficLight tlight;//十字路口的灯

  public static void main(String [] args){
    Color gcol=new Color(100,100,0);//定义 ground color
    Crossroad win =              
      new Crossroad(100,100, // top left corner 
		       600, 600,  // width, height
		       Color.black, gcol,
		       "TraficLights" );//win是Crossroad的对象
    Thread cthread = new Thread(win); //定义线程,用实现了Runnable接口的Crossroad的对象win中所定义的run()方法,来覆盖新创建的线程对象cthread的run()方法
    win.show();//显示路况
  cthread.start();//启动线程
  }


//覆盖定义Crossroad的run方法
  public void run(){
    while(true){
      try{Thread.sleep(100);} catch(InterruptedException e){};//100ns
      repaint();
    };
  };

  
  public Crossroad(int x, int y, int wid, int hei, 
			      Color fg, Color bg, String title) {
        java.util.Random rand=new java.util.Random();//产生一个随机数
        addWindowListener(new Wclose());//保证窗口可以关闭
        setLocation(x,y);
        setSize(wid, hei);
        setTitle(title);
        setForeground(fg);
        setBackground(bg);

	r1 = new Road(20,hei/2,wid-20,hei/2);
	r2 = new Road(wid/2,40,wid/2,hei-20);
	c1 = new Car(r1,0.003+0.01*rand.nextDouble(),100,new Color(rand.nextInt()));//车的速度和颜色是随机的,从而表示速度是改变的,也就是车流量是随机的
	c2 = new Car(r2,0.003+0.01*rand.nextDouble(),100,new Color(rand.nextInt()));//车的速度和颜色是随机的,从而表示速度是改变的,也就是车流量是随机的
	tlight = new TraficLight(wid/2,hei/2,2500); //在中间建立红绿灯
	c1.registerTraficLight(tlight);//控制car1的交通灯
	c2.registerTraficLight(tlight);//控制car2的交通灯
	tlight.start();
	c1.start();
	c2.start();
  };
  
  public void paint(Graphics g) {         
    
    r1.paint(g);
    r2.paint(g);
    c1.paint(g);
    c2.paint(g);
    tlight.paint(g);
    
  };
  //车是一个线程
  public class Car extends Thread{
    double v,pos;//
    int x,y,cx1,cy1,dx,dy,delay,fx,fy;
    ArrayList arrTl;// 
    Color color;
    TraficLight t;
    public void registerTraficLight(TraficLight tl){
      arrTl.add(tl);
    };

    public Car(Road r,double velocity,int Delay,Color col){
      color=col;
      cx1=r.getcx1();
      cy1=r.getcy1();
      dx=r.getcx2()-cx1;//
      dy=r.getcy2()-cy1;//

      pos=0;//初始位置
      v=velocity;//速度
      delay=Delay;
      arrTl = new ArrayList();
    };
    //重写run();
    public void run(){
      while (true){
	pos+=v;
	if(pos>=1){
	  pos=1;
	  v = -v;//到尽头,然后反向
	}
	if(pos<=0){
	  pos=0;
	  v = -v;//到尽头,然后反向
	}
	x=(int) Math.round(dx*pos)+cx1;
	y=(int) Math.round(dy*pos)+cy1;
	rest(delay);//控制车的速度
	for(int c=0;c<arrTl.size();c++){
	  t=(TraficLight) arrTl.get(c);//得到交通灯实例
	  if( dx > dy ){ //
	    fy=y; 
	    fx=x+(int) Math.round(40*v/Math.abs(v));
	  } else { //
	    fx=x;
	    fy=y+(int) Math.round(40*v/Math.abs(v));
	  }
	  if(t.near(fx,fy)){
	    while(tlight.signStop( dx > dy )) 
	      rest(delay);//遇到红灯停车
	  }
	}
      };
    };
    public void paint(Graphics g){
      g.setColor(color);//
      g.fillRect(x-5,y-5,10,10);
    };

    private void rest(int ns){
      try{Thread.sleep(ns);} catch (InterruptedException e) {;};
    };
  };





  //state 0=black 1=red 2=yellow 3=green
  public class TraficLight extends Thread{
    int x,y;//abs position
    int xmod,ymod,delay;

    //判断车与灯相近的方法
    public boolean near(int fx,int fy){
      if( (Math.abs(fx-x)<12) && (Math.abs(fy-y)<12) ) return true;
      else return false;
    };

    public boolean signStop(boolean xled){
      if(xled){
	if(3==xmod) return false;
      } else {
	if(3==ymod) return false;
      }
      return true;
    };

    private void rest(int ns){
      try{Thread.sleep(ns);} catch (InterruptedException e) {;};
    };
    //交通灯改变的延迟时间设置
    public void run(){
      while (true){
	xmod=2;
	ymod=2;
	rest(delay/2);
	xmod=3;
	ymod=1;
	rest(delay);
	xmod=2;
	ymod=2;
	rest(delay/2);
	xmod=1;
	ymod=3;
	rest(delay);
      };
    };
    //构造函数
    public TraficLight(int xpos,int ypos,int delay_){
      x=xpos;
      y=ypos;
      xmod=1;
      ymod=3;
      delay=delay_;
    };
    

    //state 0=black 1=red 2=yellow 3=green
    private void paintLight(Graphics g,int state,int x,int y){
       g.setColor(Color.black);
       g.fillRect(x,y,10,22);
       
       switch (state) {
       case 1:
	 g.setColor(Color.red);
	 g.fillOval(x+1,y,8,8);
	 break;
       case 2:
	 g.setColor(Color.yellow); 
	 g.fillOval(x+1,y+7,8,8);
	 break;
       case 3:
	 g.setColor(Color.green);
	 g.fillOval(x+1,y+14,8,8);
	 break;
       }
    };
    //定义画灯的方法
    public void paint(Graphics g){
      paintLight(g,xmod,x-40,y-32);
      paintLight(g,xmod,x-40,y+10);
      paintLight(g,xmod,x+30,y-32);
      paintLight(g,xmod,x+30,y+10);

      paintLight(g,ymod,x-21,y-40);
      paintLight(g,ymod,x+11,y-40);
      paintLight(g,ymod,x-21,y+30);
      paintLight(g,ymod,x+11,y+30);      
    };

  };

 public class Road{
   int x1,x2,y1,y2;
   int cx,cy,c2x,c2y;//
   int fileWidth=10; //the width of one file of the road in pixell

   public int getcx1(){ return cx; };
   public int getcy1(){ return cy; };
   public int getcx2(){ return c2x; };
   public int getcy2(){ return c2y; };

   public Road(int xp,int yp,int xpp,int ypp){  //定义路和路的中心线的数值
     cx=Math.min(xp,xpp);
     c2x=Math.max(xp,xpp);
     cy=Math.min(yp,ypp);
     c2y=Math.max(yp,ypp);
     
     x2=c2x-cx+fileWidth*2;
     y2=c2y-cy+fileWidth*2;
     x1=cx-fileWidth;
     y1=cy-fileWidth;
   };
    
    public void paint(Graphics g){//画路和路的中心线
      g.setColor(Color.black);
      g.fillRect(x1,y1,x2,y2);
      g.setColor(Color.white);
      g.drawLine(cx,cy,c2x,c2y);
    };

  };
}
class Wclose extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
	System.exit(0);
    }
}

⌨️ 快捷键说明

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