📄 race.java
字号:
import java.applet.Applet;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class car
{int cx,cy;
int color,num;
car(int color,int num,int x,int y)
{this.color=color;
this.num=num;
this.cx=x;
this.cy=y;
}
void move()
{
this.cy-=5;
}
}
public class race extends Applet implements Runnable
{int i=0;
car cars[]=new car[4];
String s[]=new String[4];
Font f=new Font("TimesRoman",Font.BOLD,20);
Image offimg;
Graphics offg;
Thread threads;
Thread ThreadA;
Thread ThreadB;
Thread ThreadC;
Thread ThreadD;
public void init()
{
offimg=createImage(800,600);
offg=offimg.getGraphics();
/////////////
s[0]="第一名是:";
s[1]="第二名是:";
s[2]="第三名是:";
s[3]="最后一名是:";
}
public void start()
{ThreadA=new Thread(this,"redcar");
ThreadB=new Thread(this,"greencar");
ThreadC=new Thread(this,"bluecar");
ThreadD=new Thread(this,"orangercar");
cars[0]=new car(0,1,200,400);
cars[1]=new car(1,2,250,400);
cars[2]=new car(2,3,300,400);
cars[3]=new car(3,4,350,400);
ThreadA.start();
ThreadB.start();
ThreadC.start();
ThreadD.start();
}
public void stop()
{if(ThreadA!=null)
ThreadA.stop();
else if(ThreadB!=null)
ThreadB.stop();
else if(ThreadC!=null)
ThreadC.stop();
else if(ThreadD!=null)
ThreadD.stop();
ThreadA=null;
ThreadB=null;
ThreadC=null;
ThreadD=null;
}
public void update(Graphics g)
{offg.setColor(Color.white);
offg.fillRect(0,0,800,600);
offg.setColor(Color.black);
for(int j=0;j<4;j++)
offg.drawString(s[j],450,50+j*50);
offg.drawLine(160,40,400,40);
offg.setColor(Color.red);
offg.fill3DRect(cars[0].cx,cars[0].cy,25,40,true);
offg.setColor(Color.green);
offg.fill3DRect(cars[1].cx,cars[1].cy,25,40,true);
offg.setColor(Color.blue);
offg.fill3DRect(cars[2].cx,cars[2].cy,25,40,true);
offg.setColor(Color.orange);
offg.fill3DRect(cars[3].cx,cars[3].cy,25,40,true);
g.drawImage(offimg,0,0,this);
}
public void run()
{for(int cnt=0;cnt<72;cnt++)
{threads=Thread.currentThread();
if(threads==ThreadA)
cars[0].move();
else if(threads==ThreadB)
cars[1].move();
else if(threads==ThreadC)
cars[2].move();
else
cars[3].move();
if(cnt==70)
{s[i]=s[i]+Thread.currentThread().getName();i++;}
try{repaint();
Thread.currentThread().sleep((int)(Math.random()*100));
}catch(InterruptedException e){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -