java简单红绿灯变化(本菜鸟著)
今天看了书本线程和绘图的⼀部分,翻了翻书本,发现⼀道课后习题是要模拟红绿灯变化,好像有点意思,嘿嘿嘿,我就来写了写,不喜勿喷,⼤家可以多多指正~
运⾏起来是这样的
(第⼀次上传代码有点紧张 [擦擦⼿⼿的汗] )
import Canvas;
个体年报
import Color;
import Graphics;
import Graphics2D;
import JFrame;
public class light extends JFrame{
/**
* 模拟红绿灯,红灯2秒,绿灯3秒,黄灯4秒
*/
private static final long rialVersionUID =1L;李白形象
private Thread t;
古文化常识没有奶水怎么办public light(){
四川杀人案tVisible(true);//设置窗体可见
tTitle("红绿灯模拟");//设置标题
tSize(100,600);//窗体⼤⼩
tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭模式
Canv c =new Canv();
add(c);//窗体作为画布
t =new Thread(c);//创建匿名线程对象
t.start();//启动线程
}
class Canv extends Canvas implements Runnable{//创建画布(内部类)
class Canv extends Canvas implements Runnable{//创建画布(内部类)/**
*
*/
private static final long rialVersionUID =1L;
public void paint(Graphics g,Color c,int y){
super.paint(g);
Graphics2D g2 =(Graphics2D) g;
g2.tColor(Color.black);
g2.fillRect(40,40,40,120);//绘制灯的外廓
g2.tColor(Color.black);
g2.fillRect(55,160,10,270);//灯的杆⼦
g2.tColor(c);
g2.fillOval(40, y,40,40);//绘制灯
}
@Override
public void run(){
// TODO Auto-generated method stub
while(true){//⽆限循环
try{
Thread.sleep(4000);// 线程休眠4秒,黄灯
}catch(InterruptedException e){
e.printStackTrace();
}
Graphics g =getGraphics();
this.paint(g, d,40);// 红灯
我的好妈妈教案try{
Thread.sleep(2000);// 线程休眠2秒,红灯
}catch(InterruptedException e){
e.printStackTrace();
}
this.paint(g, ,80);// 绿灯
try{
Thread.sleep(3000);// 线程休眠3秒,绿灯
}catch(InterruptedException e){
入党外调函e.printStackTrace();
}
this.paint(g, llow,120);// 黄灯
}
}
}
public static void main(String[] args){
太极五章new light();
}
}