bouncer.java
来自「书籍"Java_面向事件编程"的附带光盘代码」· Java 代码 · 共 50 行
JAVA
50 行
import objectdraw.*;import java.awt.Color;// moves each ball in the array up and downpublic class Bouncer extends ActiveObject { // how many balls to bounce private static final int BOUNCES = 20; // speed and acceleration of ball motion private static final double INITSPEED = -3; private static final double GRAVITY = .125; // length of pauses between moving a ball private static final int STEPDELAY = 30; // length of pause between picking balls to bounce private static final int BOUNCEDELAY = 250; // the set of balls to choose from BallCollectionInterface allTheBalls; public Bouncer( BallCollectionInterface theBalls ) { allTheBalls = theBalls; start(); } public void run() { // bounce BOUNCES randomly picked balls. for ( int count = 0; count < BOUNCES; count ++ ) { BBall ball = allTheBalls.pickRandomly(); // animate the bounce for ( double velocity = INITSPEED; velocity <= -INITSPEED; velocity = velocity + GRAVITY) { ball.move(0,velocity); pause( STEPDELAY); } pause( BOUNCEDELAY); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?