我遇到了2个类的问题,每个类都有一个有线程的applet,但是我想在一个applet中运行这两个类,以便最后有一个种族的动画。我需要在一个类中启动两个线程。
TortuleThread
public class TortuleThread extends Applet implements Runnable {
public int y= 0;
Thread thread;
public void init(){
thread = new Thread(this);
thread.start();
resize(800,700);
}
public void paint(Graphics g){
g.setColor(Color.green);
g.fillOval(80, 30+y, 50, 50);
g.fillOval(88, 70+y, 30, 30);
g.fillOval(70, 50+y, 20, 20);
g.fillOval(120, 50+y, 20, 20);
g.fillOval(85, 25+y, 20, 20);
g.fillOval(105, 25+y, 20, 20);
g.setColor(Color.BLACK);
g.fillOval(92, 80+y, 6, 6);
g.fillOval(105, 80+y, 6, 6);
y=y+15;
}
public void run()
{
int i=0;
System.out.println("start tortule");
while(i<5)
{
repaint();
try{
Thread.sleep(5000);
System.out.println("Tortule");
}catch(InterruptedException ex)
{
}
i++;
}
System.out.println("end tortule");
}
}
HareThread
public class HareThread extends Applet implements Runnable {
public int y = 0;
Thread thread;
public void init() {
thread = new Thread(this);
thread.start();
resize(800, 700);
}
public void paint(Graphics g) {
g.setColor(Color.gray);
g.fillOval(58, 3 + y, 8, 30);
g.fillOval(68, 3 + y, 8, 30);
g.fillRect(55, 30 + y, 20, 20);
g.fillRect(40, 50 + y, 30, 30);
g.setColor(Color.white);
g.fillOval(60, 32 + y, 6, 6);
g.fillOval(68, 32 + y, 6, 6);
g.setColor(Color.red);
g.fillOval(64, 40 + y, 6, 6);
g.setColor(Color.gray);
g.fillOval(30, 60 + y, 15, 15);
y=y+15;
}
public void run() {
int i = 0;
System.out.println("start hare");
while (i < 5) {
repaint();
try {
Thread.sleep(2000);
System.out.println("hare");
} catch (InterruptedException ex) {
}
i++;
}
System.out.println("end the hare");
}
}
GraphicRace
public class GraphicRace extends Applet{
public static void main (String [] args)
{
TortuleThread tortule = new TortuleThread();
Thread hare = new Thread(new HareThread());
tortule.start();
hare.start();
}
}