📄 xr.java
字号:
/*
* @(#)Jt.java 1.0 03/06/19
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_2\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.util.*;
class watch extends javax.swing.JApplet{
private Color butterscotch = new Color(255,204,102);
private String lastTime=" ";
Graphics g;
watch(Graphics g){
this.g=g;
setBackground(Color.BLACK);
}
public void paint(Graphics screen){
Graphics2D screen2D=(Graphics2D)screen;
Font type = new Font("Monospaced",Font.BOLD,20);
screen2D.setFont(type);
GregorianCalendar day=new GregorianCalendar();
String time=day.getTime().toString();
screen2D.setColor(Color.BLACK);
screen2D.drawString(lastTime,75,350);
screen2D.setColor(butterscotch);
screen2D.drawString(time,75,350);
try{
Thread.sleep(1000);
} catch(InterruptedException e){
}
lastTime = time;
repaint();
}
}
class roads //路类:负责道路以及周边环境的描述;
{
int rd1x=120,rd2x=340,rdy=170; //两个十字路口的x、y坐标(120,170)、(340,170);
int halfwid=40;//路宽的一半
int mleft=10,mright=790,mup=50,mdown=300;
int s[][]=new int[600][400]; //整个区域的一个映射数组;
int guan1=0,guan2=0,guan3=0,guan4=0,guan5=0;
int guan6=0,guan7=0,guan8=0,guan9=0,guanA=0;
roads() //路类的构造函数:每个全区域映射数组代表一个点,为1表示有车在上面,为0表示没有;
{
for(int t=0;t<=599;t++)
for(int f=0;f<=399;f++)
s[t][f]=0;
}
void show(Graphics g) //具体的画这个系统;
{
g.setColor(Color.BLACK);
g.fillRoundRect(75,330,310,25,5,5);
g.setColor(Color.cyan); //十字路口中心的中心线;
g.fillRoundRect(35,165,45,10,5,5);
g.fillRoundRect(160,165,140,10,5,5);
g.fillRoundRect(380,165,75,10,5,5);
g.fillRoundRect(115,55,10,75,5,5);
g.fillRoundRect(115,210,10,75,5,5);
g.fillRoundRect(335,55,10,75,5,5);
g.fillRoundRect(335,210,10,75,5,5);
g.setColor(Color.magenta); //十字路口中心的交警指挥台;
g.fillOval(112,162,16,16);
g.fillOval(332,162,16,16);
g.setColor(Color.green); //周边的绿地;
g.fillRoundRect(10,55,55,55,10,10);
g.fillRoundRect(10,230,55,60,10,10);
g.fillRoundRect(180,55,100,60,10,10);
g.fillRoundRect(180,230,100,60,10,10);
g.fillRoundRect(400,55,60,60,10,10);
g.fillRoundRect(400,230,60,60,10,10);
g.setColor(Color.gray); //道路边界;
g.drawLine(mleft,rdy-halfwid,rd1x-halfwid,rdy-halfwid);
g.drawLine(rd1x-halfwid,rdy-halfwid,rd1x-halfwid,mup);
g.drawLine(rd1x+halfwid,mup,rd1x+halfwid,rdy-halfwid);
g.drawLine(rd1x+halfwid,rdy-halfwid,rd2x-halfwid,rdy-halfwid);
g.drawLine(rd2x-halfwid,rdy-halfwid,rd2x-halfwid,mup);
g.drawLine(rd2x+halfwid,mup,rd2x+halfwid,rdy-halfwid);
g.drawLine(rd2x+halfwid,rdy-halfwid,mright,rdy-halfwid);
g.drawLine(rd2x+halfwid,rdy+halfwid,mright,rdy+halfwid);
g.drawLine(rd2x+halfwid,rdy+halfwid,rd2x+halfwid,mdown);
g.drawLine(rd2x-halfwid,rdy+halfwid,rd2x-halfwid,mdown);
g.drawLine(rd1x+halfwid,rdy+halfwid,rd2x-halfwid,rdy+halfwid);
g.drawLine(rd1x+halfwid,rdy+halfwid,rd1x+halfwid,mdown);
g.drawLine(rd1x-halfwid,rdy+halfwid,rd1x-halfwid,mdown);
g.drawLine(mleft,rdy+halfwid,rd1x-halfwid,rdy+halfwid);
g.drawLine(rd2x+halfwid,rdy-halfwid,mright,rdy-halfwid);
}
}
class Car extends Thread //汽车类:实际上是一个线程;
{
roads jiao; //数据成员:路类的对象jiao 灯类的对象light_1、light_2;
int x,y,a;
Graphics g;
Light light_1;
Light light_2;
Random random=new Random(2003); //随机函数产生是否转弯的一个随机数;
Car(Graphics g,Light light1,Light light2,int x,int y,int a,roads jiao) //CAR类的constructor;
{
this.g=g;
this.light_1=light1;
this.light_2=light2; //传进灯的状态:红或是绿;
this.x=x;
this.y=y;
this.a=a;
this.jiao=jiao; //路类的对象jiao:用来解决小车碰撞问题;
}
public void run() //线程的run函数;
{
while(true)
{
switch(a) //四个方向运行的画车函数;
{
case 0: paint(g,0);break; //0为自西向东运行的车的实现;
case 1: paint(g,1);break; //1为自南向北运行的车的实现;
case 2: paint(g,2);break; //2为自东向西运行的车的实现;
case 3: paint(g,3);break; //3为自北向南运行的车的实现;
default:;
}
}
}
void paint(Graphics g,int b) //具体的画车函数;
{ switch(b)
{
case 0:{ //车自西向东;
if(x==60 || x==280)
{if(Math.random()>0.2)
a=0;
else a=1;} //是否右转弯;
while(x==60 && a==0 && light_1.i==true || x==280 && a==0 && light_1.i==true) //是否为红灯:Y则等 N则直走;
{
try
{this.sleep(300);
}
catch(InterruptedException e)
{}
}
if(jiao.s[x+25][180]==0) //前面是否有车;
{x=x+5;
g.setColor(Color.RED);
g.fillRect(x,180,20,15);
if(x>0)jiao.s[x][180]=1;
if(x>0)jiao.s[x-5][180]=0;
g.clearRect(x-5,180,10,15);
}
while(a==1 && x<=90 && x>=0 || x>=280 && x<=305 && a==1) //转弯时的完善;
{
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
for(int p=150;p<=210;p++)
if(jiao.s[90][p]==1){jiao.guan3=1;break;};
for(int z=150;z<=210;z++)
if(jiao.s[280][z]==1){jiao.guan4=1;break;};
if(jiao.s[x+25][180]==0)
{x=x+5;
g.setColor(Color.RED);
g.fillRect(x,180,20,15);
jiao.s[x][180]=1;
jiao.s[x-5][180]=0;
g.clearRect(x-5,180,10,15);
}
}
if(a==1){g.clearRect(x,180,20,15);jiao.s[x][180]=0; }
if(x==460) //自西向东时到东边后是否左转:a=2为是,a=0为否;
{if(Math.random()>0.5)
a=0;
else a=2;
if(a==2)g.clearRect(x,180,20,15);
while(a==2 && y>=140)
{
for(int p=430;p<=495;p++)
if(jiao.s[p][140]==1){jiao.guanA=1;};
if(jiao.s[x][y-25]==0 && jiao.guanA==0) //前面是否有车;
{ y=y-5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
jiao.s[x][y+5]=0;
g.clearRect(x,y+15,15,10);
}
jiao.guanA=0;
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
}
jiao.s[x][180]=0;
jiao.s[x-5][180]=0;
}
if(x==460 && a==2)g.clearRect(x,y,15,20);
if(x==500){jiao.s[x][180]=0;x=0;};
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
y=180;
};break;
case 1: //车自北向南;
{
if(y==110) //是否右转弯;
{if(Math.random()>0.5){a=1;}
else {a=2;}}
while(y==110 && light_1.i!=true && a==1) //是否为红灯:Y则等 N则直走;
{
try
{this.sleep(300);
}
catch(InterruptedException e)
{}
}
if(jiao.s[x][y+25]==0) //前面是否有车;
{ y=y+5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
if(y>0)jiao.s[x][y]=1;
if(y>0 )jiao.s[x][y-5]=0;
g.clearRect(x,y-5,15,10);
}
while(a==2 && y<=120 && y>=0) //转弯时的完善;
{
if(jiao.s[x][y+25]==0)
{ y=y+5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
if(y>0)jiao.s[x][y]=1;
if(y>0 )jiao.s[x][y-5]=0;
g.clearRect(x,y-5,15,10);
}
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
}
if(a==2){g.clearRect(x,y,15,20);jiao.s[x][y]=0;jiao.s[x][y-5]=0;}
if(y==300){jiao.s[x][y]=0;g.clearRect(x,y-20,15,40);y=0;};
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
};break;
case 2: //车自东向西;
{if(x==150 || x==380) //是否右转弯;
{if(Math.random()>0.5)
a=2;
else a=3;}
while(x==165 && a==2 && light_1.i==true || x==390 && a==2 && light_1.i==true) //是否为红灯:Y则等 N则直走;
{
try
{this.sleep(300);
}
catch(InterruptedException e)
{}
}
if(x<=25 || jiao.s[x-25][140]==0) //前面是否有车;
{x=x-5;
g.setColor(Color.RED);
g.fillRect(x,140,20,15);
jiao.s[x][140]=1;
jiao.s[x+5][140]=0;
g.clearRect(x+15,140,10,15);
}
while(a==3 && x<=200 && x>=140 ||a==3 && x>=360 && x<=400) //转弯时的完善;
{
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
if(jiao.s[x-25][140]==0)
{x=x-5;
g.setColor(Color.RED);
g.fillRect(x,140,20,15);
jiao.s[x][140]=1;
jiao.s[x+5][140]=0;
g.clearRect(x+15,140,10,15);
}
}
if(a==3){g.clearRect(x,140,10,15);jiao.s[x][140]=0; }
if(x==15) //自东向西时到西边后是否左转:a=0为是,a=2为否;
{if(Math.random()>0.5)
a=0;
else a=2;
while(a==0 && y<180)
{
for(int p=0;p<=40;p++)
if(jiao.s[p][180]==1){jiao.guan9=1;};
if(jiao.s[x][y+25]==0 && jiao.guan9==0)
{ y=y+5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
if(y>0)jiao.s[x][y]=1;
if(y>0)jiao.s[x][y-5]=0;
g.clearRect(x,y-5,15,10);
}
jiao.guan9=0;
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
}
jiao.s[x][140]=0;
jiao.s[x-5][140]=0; }
if(x==15 && a==0)g.clearRect(x,y,15,20);
if(x<=0){jiao.s[x][140]=0;g.clearRect(x,140,20,15);x=500;};
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
y=140;
};break;
case 3:{ //车自南向北;
if(y==215) //是否右转弯;
{if(Math.random()>0.5){a=3;}
else {a=0;}}
while(y==215 && light_1.i!=true && a==3) //是否为红灯:Y则等 N则直走;
{
try
{this.sleep(300);
}
catch(InterruptedException e)
{}
}
if(y<=25 || jiao.s[x][y-25]==0) //前面是否有车;
{ y=y-5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
jiao.s[x][y]=1;
jiao.s[x][y+5]=0;
g.clearRect(x,y+15,15,10);
}
while(a==0 && y<=225 && y>=180) //转弯时的完善;
{
for(int p=110;p<=180;p++)
if(jiao.s[p][180]==1){jiao.guan1=1;};
for(int z=290;z<=380;z++)
if(jiao.s[z][180]==1){jiao.guan2=1;};
if(jiao.s[x][y-25]==0 && jiao.guan1==0 || jiao.s[x][y-25]==0 && jiao.guan2==0)
{ y=y-5;
g.setColor(Color.RED);
g.fillRect(x,y,15,20);
jiao.s[x][y]=1;
jiao.s[x][y+5]=0;
g.clearRect(x,y+15,15,10);
}
jiao.guan1=0;
jiao.guan2=0;
if(y==180)break;
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
}
if(a==0){g.clearRect(x,y,15,20);jiao.s[x][y]=0;jiao.s[x][y+5]=0;}
if(y==0){g.clearRect(x,y,15,20);jiao.s[x][y]=0;y=300;};
try
{this.sleep(50);
}
catch(InterruptedException e)
{}
};break;
default:;
}
}
}
class Time extends Thread
{
Graphics g;
watch Awatch=new watch(g);
Time(Graphics g)
{
this.g=g;
}
public void run()
{
while(true){Awatch.paint(g);}
}
}
class Light extends Thread //灯类:实际上也是线程;
{
Graphics g; //数据成员;
int x,y,j;
public boolean c,i=true;
Light(Graphics g,int x,int y,int j,boolean c) //灯类的constructor;
{
this.g=g;
this.x=x;
this.y=y;
this.j=j;
this.c=c;
}
public void run()
{
while(true)
{
if(i)
{i=false;}
else
{i=true;}
paint(g,x,y,j); //十字路口交通灯每过4秒钟变一次;
try
{
this.sleep(4000);
}
catch(InterruptedException e)
{}
}
}
public void paint(Graphics g,int x,int y,int j) //四组灯,同一路口的两两相反,不同路口相同位置两两相同;
{
if(i==true && j==1 && c==true){
g.setColor(Color.yellow);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x,y+10,7,7);
g.setColor(Color.red);
g.fillOval(x,y+20,7,7);}
else if(i==false && j==1 && c==true){
g.setColor(Color.yellow);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x,y+10,7,7);
g.setColor(Color.green);
g.fillOval(x,y+20,7,7);}
if(i==false && j==1 && c==false){
g.setColor(Color.green);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x,y+10,7,7);
g.setColor(Color.yellow);
g.fillOval(x,y+20,7,7);}
else if(i==true && j==1 && c==false){
g.setColor(Color.red);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x,y+10,7,7);
g.setColor(Color.yellow);
g.fillOval(x,y+20,7,7);}
if(i==false && j==2 && c==true){
g.setColor(Color.yellow);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x+10,y,7,7);
g.setColor(Color.red);
g.fillOval(x+20,y,7,7);}
else if(i==true && j==2 && c==true){
g.setColor(Color.yellow);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x+10,y,7,7);
g.setColor(Color.green);
g.fillOval(x+20,y,7,7);}
if(i==false && j==2 && c==false){
g.setColor(Color.red);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x+10,y,7,7);
g.setColor(Color.yellow);
g.fillOval(x+20,y,7,7);}
else if(i==true && j==2 && c==false){
g.setColor(Color.green);
g.fillOval(x,y,7,7);
g.setColor(Color.lightGray);
g.fillOval(x+10,y,7,7);
g.setColor(Color.yellow);
g.fillOval(x+20,y,7,7);}
}
}
public class XR extends Applet { //XR主类;
Light light11;
Light light12;
Light light21;
Light light22;
Light light31;
Light light32;
Light light41;
Light light42; //四组八个灯;
Car car1;
Car car12;
Car car2;
Car car3;
Car car4;
Car car5;
Car car6; //七辆小车;
Time Atime;
roads r=new roads();
public int p=0;
public void init()
{
//for(int c=1;c<=10;c++)start();
}
public void start()
{
Graphics g=getGraphics();
Atime=new Time(g);
Atime.start();
if(light11==null && light12==null) //四组灯的产生;
{
light11=new Light(g,50,120,2,true);
light11.start();
light12=new Light(g,270,120,2,true);
light12.start();
}
if(light21==null && light22==null)
{
light21=new Light(g,165,100,1,true);
light21.start();
light22=new Light(g,385,100,1,true);
light22.start();
}
if(light31==null && light32==null)
{
light31=new Light(g,70,215,1,false);
light31.start();
light32=new Light(g,290,215,1,false);
light32.start();
}
if(light41==null && light42==null)
{
light41=new Light(g,165,215,2,false);
light41.start();
light42=new Light(g,385,215,2,false);
light42.start();
}
if(car1==null) //小车的产生;
{
car1=new Car(g,light21,light11,5,180,0,r);
car1.start();
}
try
{Thread.sleep(1000);
}
catch(InterruptedException e)
{}
if(car12==null)
{
car12=new Car(g,light21,light11,5,180,0,r);
car12.start();
}
if(car2==null)
{
car2=new Car(g,light41,light11,500,130,2,r);
car2.start();
}
if(car3==null)
{
car3=new Car(g,light41,light11,0,180,0,r);
car3.start();
}
if(car4==null)
{
car4=new Car(g,light12,light42,355,300,3,r);
car4.start();
}
if(car5==null)
{
car5=new Car(g,light12,light42,310,0,1,r);
car5.start();
}
try
{Thread.sleep(1000);
}
catch(InterruptedException e)
{}
if(car6==null)
{
car6=new Car(g,light11,light41,140,300,3,r);
car6.start();
}
}
public void paint(Graphics g) {
r.show(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -