package shape;import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Map;
public class Shape extends Applet implements ActionListener,Runnable{
Button start;
Button stop;
SubThread magenta,green,pink,yellow;
int runTime=0;
Map map;
public void init()
{
this.resize(380,230);
start=new Button("start");
stop=new Button("stop");
add(start);
add(stop);
start.addActionListener(this);
stop.addActionListener(this);
}

public void startRun()
{
  if(magenta==null)
   {
  magenta=new SubThread(this,"magenta");
  magenta.setXpos(20);
  map.put("magenta", magenta);
   }
    magenta.start();
  if(green==null)
   {
  green=new SubThread(this,"green");
  green.setXpos(60);
  map.put("green", green);
   }
    green.start();
  if(pink==null)
   {
  pink=new SubThread(this,"pink");
  pink.setXpos(100);
  map.put("pink", pink);
   }
    pink.start();
  
  if(yellow==null)
   {
  yellow=new SubThread(this,"yellow");
  yellow.setXpos(140);
  map.put("yellow", yellow);
   }
    yellow.start();
}

public void stopRun()
{
if(magenta!=null)
{   magenta.stop(); }
if(green!=null)
{   green.stop();  }
if(pink!=null)
{   pink.stop();   }
    if(yellow!=null)
    {   yellow.stop(); } 
    map.clear();
}

public void run()
{  
String threadName=Thread.currentThread().getName();
SubThread currentThread=(SubThread)map.get(threadName);
while((currentThread.getYpos()-10)>=30)
{
currentThread.setYpos(currentThread.getYpos()-10);
repaint(); 
try
{
Thread.sleep((int)(Math.random()*3000));
}
catch(InterruptedException ie)
{ }
}

}

public void actionPerformed(ActionEvent ave)
{
if(ave.getSource()==start)
{
  startRun();
}

if(ave.getSource()==stop)
{
  stopRun();
}

}

public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(10,30,270,30);
g.setColor(Color.magenta);
try
{
if(map.get("magenta")==null)
g.fillRect(20,200,10,10);
else
g.fillRect(((SubThread)map.get("magenta")).getXpos(),((SubThread)map.get("magenta")).getYpos(),10,10);
g.setColor(Color.green);
if(map.get("green")==null)
g.fillRect(60,200,10,10);
else
    g.fillRect(((SubThread)map.get("green")).getXpos(),((SubThread)map.get("green")).getYpos(),10,10);
g.setColor(Color.pink);
if(map.get("pink")==null)
g.fillRect(100,200,10,10);
else
g.fillRect(((SubThread)map.get("pink")).getXpos(),((SubThread)map.get("pink")).getYpos(),10,10);
g.setColor(Color.yellow);
if(map.get("yellow")==null)
g.fillRect(140,200,10,10);
else
g.fillRect(((SubThread)map.get("yellow")).getXpos(),((SubThread)map.get("yellow")).getYpos(),10,10);
}
catch(Exception e)
{ System.out.println("error is: "+e.getMessage()); }
g.setColor(Color.black);
g.drawString("successful1!",180,80);
g.drawString("successful2!",180,120);
g.drawString("successful3!",180,160);
g.drawString("successful4!",180,200);
}

}
下一个类是:
package shape;public class SubThread extends Thread {
private long startTime,endTime;
private Runnable target;
private String name;
private int xpos,ypos=200;

public SubThread(Runnable target,String name)
{
this.target=target;
this.name=name;
}

public void setXpos(int xpos)
{
this.xpos=xpos;
}

public  void setYpos(int ypos)
{
this.ypos=ypos;
}

public int getYpos()
{
return this.ypos;
}

public int getXpos()
{
return this.xpos;
}

public   long getTime(long startTime,long endTime)
{
return endTime-startTime;
}
}
为什么点击开始按钮时会出现异常??