一个Applet的练习
1. 画随机的园 功能要求:实现简单的动画.要求:自动出现由小到大变换的园,位置与颜色随机,变到150直径时擦除,重新再出现园;变换速度由html文件传入的参数控制(提示:控制sleep时间). 界面要求:用java applet实现.
?
?Java类(AppletTest.java)
import java.applet.Applet;import java.awt.Color;import java.awt.Graphics;public class AppletTest extends Applet {int r = 0;// 半径,从0开始到150,然后再归0int time = 500;// 时间间隔public AppletTest() {setSize(400, 300);}public void setTime(int time) {this.time = time;}public void paint(Graphics g) {super.paint(g);try {while (true) {if (r == 150) {g.setColor(Color.white);g.fillRect(0, 0, 400, 300);g.setColor(Color.black);r = 0;}g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));g.drawOval((int) (Math.random() * 400), (int) (Math.random() * 300), r, r++);Thread.sleep(time);}} catch (Exception e) {}}}
?
HTML
<script>function setTime(){var time=document.getElementById("time").value;if(time<0){alert("非法");return;}document.getElementById("myapplet").setTime(time);}</script><input type="text" id="time"> <input type="button" onclick="setTime();" value="设置时间间隔"><br><applet code="AppletTest.class" width="400" height="300" id="myApplet"></applet>?