Java实现图形弹跳球程序

时间:2012-05-16 16:12:31
【文件属性】:

文件名称:Java实现图形弹跳球程序

文件大小:1KB

文件格式:JAVA

更新时间:2012-05-16 16:12:31

Applet & Graphics

package qingzh; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class Test extends Applet implements Runnable { int x = 150, y = 50, r = 30; //设置圆的属性 int dx = 11, dy = 7; // 圆的轨线设置 Thread animator; // 线程 volatile boolean pleaseStop; // 设置线程停止判断 public void paint(Graphics g) { g.setColor(Color.green); g.drawLine(0, 0, x, y); g.fillOval(x - r, y - r, r * 2, r * 2); } public void animate() { Rectangle bounds = getBounds(); //获得此 Rectangle 的边界 Rectangle if ((x - r + dx < 0) || (x + r + dx > bounds.width)) dx = -dx; if ((y - r + dy < 0) || (y + r + dy > bounds.height)) dy = -dy; // Move the circle. x += dx; y += dy; repaint(); //重画 } public void run() { while (!pleaseStop) { animate(); try { Thread.sleep(100); } // Wait 100 milliseconds catch (InterruptedException e) { } } } public void start() { animator = new Thread(this); pleaseStop = false; animator.start(); } public void stop() { pleaseStop = true; } }


网友评论

  • 一般,有参考价值,但不能直接运行。