做了一个小程序,大家帮忙测试一下,这是为什么??????? 本身就是已经实现了Runnable接口,里面怎么还有2个thread作什么用。可以去掉吧。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那也不应该这个SLEEP()不起作用啊! 不是Sleep不起作用是你每次都new两个线程,这两个线程又分别再new两个线程他们都是做画图操作所以你的操作速度会成倍的上涨你的程序逻辑结构不合理县城数目是1 2 4 8 16 32这样增加的,所以会越来越快 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); }} 回复人: zyg158(DD) ( ) 信誉:100 2004-12-04 19:28:00 得分: 0 谢了,原来是这样的..........请,接分...... 关于myeclipse 有没有人升级了XP sp3? 小菜请教窗口切换问题,高手指点~~如何让弹出对话框像"打开","保存"一样始终浮于顶端 我的网站开张了,散分了,进来提提建议 求火焰效果的Applet。 继承问题 无法解决的问题。(在线等) 如何解析 FTP 的目录列表?100分相送。 我用jsp,servlet,等写好的程序如何用installshelld 打包发布! 进行数据库操作时的异常处理? 菜鸟提问 关于javac 一个小问题,给80分!
是你每次都new两个线程,这两个线程又分别再new两个线程
他们都是做画图操作
所以你的操作速度会成倍的上涨
你的程序逻辑结构不合理
县城数目是1 2 4 8 16 32这样增加的,所以会越来越快
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);
}
}
谢了,原来是这样的..........请,接分......