chainreaction.java~
来自「书籍"Java_面向事件编程"的附带光盘代码」· JAVA~ 代码 · 共 47 行
JAVA~
47 行
import objectdraw.*;// On a mouse press, the user can place balls on the canvas;// On exiting the window, a simulation of a "chain reaction" begins:// the most recently drawn ball hits into the second most recently drawn,// which hits into the next ball, and so on.public class ChainReaction extends WindowController { private RandomIntGenerator randomColor = new RandomIntGenerator(0, 255); // x and y coordinates of the first ball drawn private static final int BALL_X = 300; private static final int BALL_Y = 100; // ball diameter private static final int SIZE = 10; // distance between balls private static final int DISP = 2 * SIZE; // x coordinate of the next ball to be drawn private int ballX = BALL_X; // the list of all balls private BallListInterface ballList; public void begin() { // list of balls is empty at first ballList = new EmptyBallList(); } // draws a ball public void onMousePress(Location point) { FilledOval ball = new FilledOval(ballX, BALL_Y, SIZE, SIZE, canvas); ballList = new NonEmptyBallList(ball, ballList); // update x coordinate for the next ball to be drawn ballX = ballX - DISP; } // cause the balls to begin hitting into each // other in a "chain reaction" public void onMouseExit(Location point) { new BallMover(ballList, SIZE); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?