java交通信号灯
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.applet.Applet;import java.awt.*;import java.awt.Color;
public class LX8_10 extends Applet{
dxx td;nbx td2;jt a=new jt();String xhd="东西灯";
Graphics g;int x1=0,y1=220;
public void init(){setForeground(Color.white);}
public void start(){
if(td==null)td=new dxx(a);td.start();
if(td2==null)td2=new nbx(a);td2.start();}
public void stop(){td=null;td2=null;}
public void update(Graphics g){paint(g);}
public void paint(Graphics g){
g.fillRect(0,100,300,100);
g.fillRect(100,0,100,300);g.setColor(Color.red);
g.drawString("南北行",110,300-y1);
g.drawString("南北行",160,y1);
g.drawString("东西行",280-x1,120);
g.drawString("东西行",x1,180);
if(xhd=="东西灯")a.qh(100,100,"红","绿",g);
if(xhd=="南北灯")a.qh(100,100,"绿","红",g);
if(xhd=="黄灯")a.qh(100,100,"黄","黄",g);}
class jt{
int x,y;String 颜色1,颜色2,d;int 显示时间;
public void qh(int x1,int y1,String c1,String c2,Graphics g){
x=x1;y=y1;颜色1=c1;颜色2=c2;
for(int i=0;i<3;i++){
if(颜色1=="黄")g.setColor(Color.yellow);
if(颜色1=="红")g.setColor(Color.red);
if(颜色1=="绿")g.setColor(Color.green);
g.fillOval(x+i*15,y,15,15);
g.fillOval(x+55+i*15,y+85,15,15);
if(颜色2=="黄")g.setColor(Color.yellow);
if(颜色2=="红")g.setColor(Color.red);
if(颜色2=="绿")g.setColor(Color.green);
g.fillOval(x+85,y+i*15,15,15);
g.fillOval(x,y+55+i*15,15,15);}}}
class dxx extends Thread{
private jt a1;
public dxx(jt a){a1=a;}
public void run(){
for(int j=0;j<100+j;j++)
synchronized(a1){for(int i=0;i<100;i++){x1+=5;repaint();if(x1==285)
x1=0;
try{Thread.sleep(100);}catch(InterruptedException e){} }
xhd="黄灯";repaint();try{Thread.sleep(400);}
catch(InterruptedException e){}
xhd="南北灯";x1=70;repaint();try{Thread.sleep(400);}catch(InterruptedException e){}
}
}
}
class nbx extends Thread{
private jt a1;
public nbx (jt a){a1=a;}
public void run(){
for(int j=0;j<100+j;j++){
synchronized(a1){for(int i=0;i<100;i++){y1+=5;repaint();if(y1==285) y1=0;
try{Thread.sleep(50);}catch(InterruptedException e){}}
xhd="黄灯";repaint();try{Thread.sleep(400);}catch(InterruptedException e){}
xhd=”东西灯”;y1=220;repaint();try{Thread.sleep(400);}catch(InterruptedException e){}}
}}}}