JAVA交通灯

来源:互联网 发布:数据库中的模式 编辑:程序博客网 时间:2024/04/30 22:19

import java.awt.*;
import java.awt.event.*;

public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
int j=0;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{j++;
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString("红灯亮了"+j+"秒", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}

if(yellowStatus==true){
j++;
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
g.drawString("黄灯注意啦"+j+"秒", 100, 220);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);

}
if(greenStatus==true){
j++;
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString("绿灯行"+j+"秒", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}

public void Lauch() {
setTitle("交通灯指示");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();

}

public class PaintThread implements Runnable
{

public void run() {
for(int i=0;i<80;i++){
switch (i) {
case 0:
j=0;
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 40:
j=0;
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;
case 50:
j=0;
redStatus=false;
greenStatus=true;
yellowStatus=false;
break;
default:
break;
}
repaint();
if(i==79)
i=-1;
try
{
Thread.sleep(1000);

}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

}

}

}
0 0