import java.awt.*;
import java.applet.*;//<applet code=AppletDemo width=400 height=300></applet>public class AppletDemo extends Applet implements Runnable
{
Thread t;

public void init()
{
t = new Thread(this);
t.start();
}

public void run()
{
while(true)
{
repaint();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}

public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.drawRect((int)Math.random()*400, (int)Math.random()*300, 
(int)Math.random()*400, (int)Math.random()*300);
}
}
请帮我指证一下,谢谢

解决方案 »

  1.   

    如果你只是用 (int)Math.random()*400,那生成的两个点坐标有可能不是一个
    合法的矩形啊应当这样
    x=   (int)Math.random()*400;
    y=   (int)Math.random()*400;
    dx=  (int)Math.random()*400;
    dy=  (int)Math.random()*400; g.drawRect(x,y,x+dx,y+dy);
      

  2.   

    楼上的随机数显然是有问题的
    (int)(Math.random()*400)不然肯定是0
      

  3.   

    我看书上好像</applet>要分行的吧!^_^
    我是初学者,不知道对不对!