本身就是已经实现了Runnable接口,里面怎么还有2个thread作什么用。可以去掉吧。

解决方案 »

  1.   

    那也不应该这个SLEEP()不起作用啊!
      

  2.   

    不是Sleep不起作用
    是你每次都new两个线程,这两个线程又分别再new两个线程
    他们都是做画图操作
    所以你的操作速度会成倍的上涨
    你的程序逻辑结构不合理
    县城数目是1 2 4 8 16 32这样增加的,所以会越来越快
      

  3.   

    import java.awt.*;
    import java.applet.*;
    public class  Lx7_2 extends Applet implements Runnable
    {
    int ox1,ox2,
    oy1,oy2,
    h,
    v,
    r,
    wid,
    hei,
    n;
    Thread thread1=null;
    Thread thread2=null;
    Color color1,color2;
    public void init()
    {
    wid=size().width;
    hei=size().height;
    n=size().width/2<size().height/2?size().width/2:size().height/2;
    h=wid-n;
    v=hei-n;
    ox1=(int)(Math.random()*h);
         oy1=(int)(Math.random()*v);
    color1=new Color(Color.HSBtoRGB((float)(Math.random()*10),1.0f,1.0f));
    ox2=(int)(Math.random()*h);
         oy2=(int)(Math.random()*v);
    color2=new Color(Color.HSBtoRGB((float)(Math.random()*10),1.0f,1.0f));
    }
    public void start()
    {
    thread1=new Thread(this);
    thread2=new Thread(this);
    thread1.start();
    thread2.start();
    }
    public void stop()
    {
    if (r==0)
    {
    thread1.stop();
    thread1=null;
    thread2.stop();
    thread2=null;
    }
    }
    public void run()
    {
    try
    {
    while (true)
    {
    r++;
    if (r++>n)
    {
    r=0;
    ox1=(int)(Math.random()*h);
    oy1=(int)(Math.random()*v);
    color1=new Color(Color.HSBtoRGB((float)(Math.random()*10),1.0f,1.0f));
    ox2=(int)(Math.random()*h);
    oy2=(int)(Math.random()*v);
    color2=new Color(Color.HSBtoRGB((float)(Math.random()*10),1.0f,1.0f));
    }
    repaint();
    Thread.sleep(100);
    }
    }
    catch (Exception e)
    {
    }
    }
    public void paint(Graphics g)
    {
    g.setColor(color1);
    g.fillOval(ox1,oy1,r,r);
    g.setColor(color2);
    g.fillOval(ox2,oy2,r,r);
    g.drawRect(10,10,wid-20,hei-20);
    }
    }
      

  4.   

    回复人: zyg158(DD) ( ) 信誉:100  2004-12-04 19:28:00  得分: 0  
    谢了,原来是这样的..........请,接分......