?? 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 + -