ballcollection.java

来自「书籍"Java_面向事件编程"的附带光盘代码」· Java 代码 · 共 77 行

JAVA
77
字号
import objectdraw.*;import java.awt.*;// A class that makes and keeps track of a line of small// basketballs that stretches from one end of the canvas// to the other.  A method is provided to access a random// ball from the collection.public class BallCollection implements BallCollectionInterface {        // diameter of the balls    private static final double BALLSIZE = 20;        // spacing between balls    private static final double BALLSPACING = 12;        // spacing between balls and edges of canvas    private static final double MARGIN = 20;        // the entire collection of balls.    private BBall[] ball;        // used to pick random balls from the collection    private RandomIntGenerator picker;            // create a new collection of basketballs that stretches across    // the program's window    public BallCollection ( DrawingCanvas canvas ) {                // how many balls will fit        int ballCount = (int) ( (canvas.getWidth() - 2*MARGIN ) /                                (BALLSIZE + BALLSPACING)                                ) ;                // center the balls vertically        double ballTop = canvas.getHeight()/2 - BALLSIZE/2;                // create the array of slots for basketballs        ball = new BBall[ballCount];                double ballLeft = MARGIN;                // Draw all the balls and put them in the array        for ( int ballNumber = 0;              ballNumber < ballCount;              ballNumber ++){            ball[ballNumber] = new BBall( ballLeft, ballTop, BALLSIZE, canvas);            ballLeft = ballLeft + BALLSPACING + BALLSIZE;        }                picker = new RandomIntGenerator( 0, ballCount-1);            }        // Return a random ball from the collection    public BBall pickRandomly() {        return ball[ picker.nextValue() ];    }        // hide all the balls    public void hide() {                for ( int counter = 0; counter < ball.length; counter++) {            ball[counter].hide();        }    }        // show all the balls    public void show() {                for ( int counter = 0; counter < ball.length; counter++) {            ball[counter].show();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?