问题 问答题

小应用程序的paint(Graphics g)方法能在屏幕窗口上显示信息和绘图,如果paint()方法能调用repaint()方法,这就能使显示实现动态效果,repaint()方法的功能是先清除paint()方法以前所画的内容,然后再调用paint()方法。
以下要你编写的paint()方法取随机的坐标位置画一个边长为20个像素的红色正方形。
其中随机的坐标位置可以用以下表达式表示:
(int)(Math.random()*100)+10
另要求paint()方法在绘制正方形后暂停100毫秒。
import java. applet.*;import java.awt.*;
public class Class1 extends Applet
public void paint(Graphics g)
//请在以下位置编写代码

答案

参考答案:g. setColor(Color.red);
g.fillRect((int)(Math. random()*100)+10,(int)(Math. random()*100)+10,20,20);
try{Thread.sleep(100);}
catch(InterruptedException e){}
repaint();

填空题
单项选择题