📄 ballcollection.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -