📄 zj.java
字号:
import java.applet.*;
import java.awt.*;
public class zj extends Applet implements Runnable
{
int x,y,m=100,n=100;
Thread th1=null,th2=null,th3=null;//th1为控制车辆的线程,th2为控制交通灯的线程,th3为显示名字得线程
Font f=new Font("宋体",Font.BOLD,24);
String str="4991252 31 张进";
Image img[]=new Image[2]; //用数组控制两辆车的加载
Image offimg;
Graphics offg;//定义Graphics对象,用一实现双缓冲
int t=0;
int tag=0;
boolean isRed=false,
isYellow=false,
isGreen=true;
public void init()
{
//加载图片的初始化
for(int i=0;i<2;i++)
img[i]=getImage(getDocumentBase(),i+".gif");
x=0;
y=this.getSize().height/2;
offimg=createImage(800,600);
offg=offimg.getGraphics();
}
public void start()
{
if(th1==null)
{
th1=new Thread(this);
th1.start();
}
if(th2==null)
{
th2=new Thread(this);
th2.start();
}
if(th3==null)
{
th3=new Thread(this);
th3.start();
}
}
public void run()
{
while(th1==Thread.currentThread())
{
x+=5;
try{th1.sleep(30);}
catch(Exception e) {}
repaint();
if(x>=200&&x<=400)
{
if(isRed)
{
try{th1.sleep(5000);
if(isGreen)
th1.resume();}
catch(Exception e) {}
}
if(isYellow)
{
if(tag==0)
{
try{th1.sleep(50);
if(isGreen)
th1.resume();
}
catch(Exception e) {}
}
if(tag==1)
{
try{th1.sleep(50);
if(isRed)
th1.suspend();
}
catch(Exception e) {}
}
}
}
if(x==this.getSize().width)
{
x=0;
t++;//交替车辆
if(t==2) t=0;
repaint();
}
}
while(th2==Thread.currentThread())
{
if(isRed)
{
try{th2.sleep(5000);
isRed=false;
isYellow=true;
isGreen=false;
repaint();
}
catch(Exception e) {}
}
if(isYellow)
{
if(tag==0)
{
try{th2.sleep(2000);
isYellow=false;
isGreen=true;
isRed=false;
repaint();
}
catch(Exception e) {}
}
}
if(isGreen)
{
try{th2.sleep(5000);
isYellow=true;
isRed=false;
isGreen=false;
tag=1;
repaint();
}
catch(Exception e) {}
}
if(isYellow)
{
if(tag==1)
{
tag=0;
try{th2.sleep(2000);
isYellow=false;
isGreen=false;
isRed=true;
repaint();
}
catch(Exception e) {}
}
}
}
while(th3==Thread.currentThread())
{
m++;
n++;
if(m==300)
{
m=100;
n=100;
}
repaint();
try{th3.sleep(100);}
catch(Exception e) {}
}
}
public void update(Graphics g)
{ //设置背景色
offg.setColor(Color.blue);
offg.fillRect(0,0,800,600);
offg.setColor(Color.magenta);
offg.setFont(f);
offg.drawString(str,m,n);
//画斑马线
offg.setColor(Color.black);
offg.fillRect(550,385,230,2);
offg.fillRect(525,400,230,2);
offg.fillRect(500,415,230,2);
offg.fillRect(475,430,230,2);
offg.fillRect(450,445,230,2);
offg.fillRect(425,460,230,2);
offg.drawImage(img[t],x,380,this);//加载图片
offg.setColor(new Color(80,100,140));
offg.fillRect(600,60,4,200);
offg.drawRect(525,30,150,30);
if(isRed)//当前灯为红灯,用红色填充第一个灯,其他两个灯用灰色填充
{ offg.setColor(Color.red);
offg.fillOval(535,30,30,30);
offg.setColor(Color.gray);
offg.fillOval(585,30,30,30);
offg.fillOval(635,30,30,30);
}
if(isYellow)//当前灯为黄灯,用黄色填充第二个灯,其他两个灯用灰色填充
{ offg.setColor(Color.yellow);
offg.fillOval(585,30,30,30);
offg.setColor(Color.gray);
offg.fillOval(535,30,30,30);
offg.fillOval(635,30,30,30);
}
if(isGreen)//当前灯为绿灯,用绿色填充第三个灯,其他两个灯用灰色填充
{ offg.setColor(Color.green);
offg.fillOval(635,30,30,30);
offg.setColor(Color.gray);
offg.fillOval(535,30,30,30);
offg.fillOval(585,30,30,30);
}
g.drawImage(offimg,0,0,this);//用offg、offimg实现双缓冲,消除屏幕的闪烁现象
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -