⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 growableballbouncer.java

📁 书籍"Java_面向事件编程"的附带光盘代码
💻 JAVA
字号:
import java.awt.*;import objectdraw.*;// A class to draw a bunch of basketballs on the screen so// that we can watch them bounce randomly.  Each click// adds a ball to the display.  Leaving the canvas makes// the balls temporarily vanish.  Moving the mouse back// into the canvas makes the balls reappear and bounce// for a while.public class GrowableBallBouncer extends WindowController {        // diameter of the balls    private static final double BALLSIZE = 16;        // spacing between balls    private static final double BALLSPACING = 13;        // spacing between balls and edges of canvas    private static final double MARGIN = 20;        // all the balls displayed.    private GrowableBallCollection allTheBalls;        // position for next ball added    private double ballLeft;        // common y coordinate for all balls    double ballTop;        // create the ball collection    public void begin() {        // make the collection size appropriate for balls that will        // fit on the canvas        allTheBalls =            new GrowableBallCollection(                                       (int) ( (canvas.getWidth() - 2*MARGIN - BALLSIZE) /                                               (BALLSIZE + BALLSPACING)                                               ) + 1);                // x coordinate of first ball        ballLeft = canvas.getWidth() - BALLSIZE - BALLSPACING;                // center the balls vertically        ballTop = canvas.getHeight()/2 - BALLSIZE/2;    }        // add a new ball (if it will fit) when the mouse is clicked.    public void onMousePress( Location point ) {        if ( ballLeft > MARGIN ) {            allTheBalls.add( new BBall( ballLeft , ballTop, BALLSIZE, canvas));            ballLeft = ballLeft - BALLSIZE - BALLSPACING;        }    }            // hide all the balls when the mouse exits    public void onMouseExit( Location point ) {        if ( !allTheBalls.isEmpty() ) {            allTheBalls.hide();        }    }            // show all the balls when the mouse arrives and bounce for a while    public void onMouseEnter( Location point ) {        if ( !allTheBalls.isEmpty() ) {            allTheBalls.show();            new Bouncer( allTheBalls );        }    }        }

⌨️ 快捷键说明

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