bouncingball.java~
来自「书籍"Java_面向事件编程"的附带光盘代码」· JAVA~ 代码 · 共 53 行
JAVA~
53 行
import objectdraw.*;// animate the motion of a simple, falling ballpublic class BouncingBall extends ActiveObject{ // size and speed of balls private static final double BALLSIZE = 30; private static final double DELAY_TIME = 33; private double ySpeed = 0.1; // y coordinate where ball disappears private double bottom; // the ball private FilledOval ball; // the paddle private FilledRect paddle; public BouncingBall(double x, double y, FilledRect thePaddle, DrawingCanvas aCanvas) { ball = new FilledOval(x - BALLSIZE/2 , y, BALLSIZE, BALLSIZE, aCanvas); bottom = aCanvas.getHeight(); paddle = thePaddle; } public void run() { double top = ball.getY(); // move the ball repeatedly until it fall off screen while (ball.getY() < bottom ) { // determine how much time has passed currentTime = System.currentTimeMillis(); elapsedTime = currentTime - lastTime; // restart timing lastTime = currentTime; ball.move(0, ySpeed * elapsedTime); if (ball.overlaps(paddle) || ball.getY() < top){ ySpeed = -ySpeed; } pause(DELAY_TIME); } ball.removeFromCanvas(); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?