一般地,在Component中绘制动画,是重载paint(g)方法,然后在自己的线程中更新动画内容后再调用repaint(),但下面的情况就有些特殊:(1)由于设计的需要,没有将Component对象甚至graphics暴露给客户代码,因此也就无法调用repaint()
(2)由于性能的原因,动画更新是非常少量的、短暂的,如果总是在一个线程中定时调度是不够恰当的因此,我尝试了下面的代码,但是不成功:class MyComponent extends JComponent
{
   MyObject myObject;
   public void paint(Graphics g)
   {
      myObject.draw(g);
   }
}
class MyObject implements Runnable
{
   Graphics g;
   boolean first=true;
   public void draw(Graphics g)
   {
     this.g=g;
     if(first)
     {
        new Thread(this).start();
        first=false;// Only one thread generated for one object
     }
   }
  
   public void run()
   {
     for(i=0;i<100;i++)
     {
       g.draw***();
       sleep a while;
     }
   }
}下面的代码是可以完成动画的: 
main()
{
  // init myComponent,myObject
  myObject.draw(myComponent.getGraphics());
}
class MyComponent extends JComponent
{
   MyObject myObject;
   public void paint(Graphics g)
   {
      //myObject.draw(g);
   }
}
class MyObject implements Runnable
{
   Graphics g;
   boolean first=true;
   public void draw(Graphics g)
   {
     this.g=g;
     if(first)
     {
        new Thread(this).start();
        first=false;// Only one thread generated for one object
     }
   }
  
   public void run()
   {
     for(i=0;i<100;i++)
     {
       g.draw***();
       sleep a while;
     }
   }
}