做了一个小程序,大家帮忙测试一下,这是为什么???????
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();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
}
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));
thread1=new Thread(this);
thread2=new Thread(this);
thread1.start();
thread2.start();
}
repaint();
Thread.sleep(100);
}
}
catch (InterruptedException 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);
}
}
以上是做了一个JAVA小程序,在APPLET上有两个随机的圆,由小到大,到了APPLET的界面的一半时就擦掉重画,这都实现了,可是唯一不足的是,在每一次擦掉重画后,速度就会加快,我想每一次的速度都一样,请大侠赐教!!!!!!!!!!!
问题点数:100、回复次数:7Top
1 楼z3h(zhaohonghui)回复于 2004-12-04 18:30:54 得分 1
本身就是已经实现了Runnable接口,里面怎么还有2个thread作什么用。可以去掉吧。Top
2 楼mm_mm_mm(mmmmmm)回复于 2004-12-04 18:35:48 得分 0
不是吧!去了会提示错误的.Top
3 楼treeroot(旗鲁特)回复于 2004-12-04 18:37:32 得分 1
这里是重写了start方法呀Top
4 楼mm_mm_mm(mmmmmm)回复于 2004-12-04 18:39:09 得分 0
那也不应该这个SLEEP()不起作用啊!Top
5 楼zyg158((DD)OTP)回复于 2004-12-04 19:09:04 得分 50
不是Sleep不起作用
是你每次都new两个线程,这两个线程又分别再new两个线程
他们都是做画图操作
所以你的操作速度会成倍的上涨
你的程序逻辑结构不合理
县城数目是1 2 4 8 16 32这样增加的,所以会越来越快Top
6 楼zyg158((DD)OTP)回复于 2004-12-04 19:28:21 得分 48
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);
}
}Top
7 楼mm_mm_mm(mmmmmm)回复于 2004-12-04 19:43:23 得分 0
回复人: zyg158(DD) ( ) 信誉:100 2004-12-04 19:28:00 得分: 0
谢了,原来是这样的..........
请,接分......Top




