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

📄 bball4two.java

📁 书籍"Java_面向事件编程"的附带光盘代码
💻 JAVA
字号:
import objectdraw.*;// program for two players to play basketball.public class BBall4Two extends WindowController{        // Location of the display    private static final int DISPLAY_X = 150;    private static final int DISPLAY_Y = 200;    private static final int DISPLAYSIZE = 16; // in points        // Location and dimensions of the hoop    private static final int HOOPTOP = 50;    private static final int HOOPLEFT = 160;    private static final int HOOPWIDTH = 80;    private static final int HOOPHEIGHT = 35;        // Initial locations and dimensions of the balls    private static final int BALLX = 110;    private static final int BALLY = 300;    private static final int BALLSIZE = 40;    private static final int BALLSPACING = 160;        // Distance between balls and score displays    private static final int SCOREDOWNSET = BALLSIZE + 25;        // the Text objects which display the scores    private Text leftDisplay, rightDisplay;        // the oval that represent the hoop    private FramedOval hoop;        // the two balls    private BBall leftBall, rightBall;        // the ball currently being dragged    private BBall ballInPlay;        // whether any ball is currently in play    private boolean carryingBall;        // the scores for each player    private int leftScore = 0,   rightScore = 0;        // the last previous known location of the mouse    private Location lastMouse;        // initialize the counter and the text message    public void begin() {                hoop = new FramedOval( HOOPLEFT, HOOPTOP, HOOPWIDTH, HOOPHEIGHT, canvas);                leftBall = new BBall( BALLX, BALLY, BALLSIZE, canvas);        rightBall = new BBall( BALLX + BALLSPACING, BALLY, BALLSIZE, canvas);                leftDisplay = new Text("HOME 0", BALLX + BALLSIZE/2,                                BALLY + SCOREDOWNSET,  canvas);        rightDisplay = new Text("VISITORS 0", BALLX + + BALLSPACING + BALLSIZE/2,                                BALLY + SCOREDOWNSET,   canvas);                leftDisplay.setFontSize(DISPLAYSIZE);        leftDisplay.move( - leftDisplay.getWidth()/2,0);        rightDisplay.setFontSize(DISPLAYSIZE);        rightDisplay.move( - rightDisplay.getWidth()/2,0);    }        // Note where mouse is depressed    public void onMousePress(Location point) {        lastMouse = point;        if ( leftBall.contains( point ) )             {                ballInPlay = leftBall;                carryingBall = true;            }         else if ( rightBall.contains( point ) )             {                ballInPlay = rightBall;                carryingBall = true;            }    }        // Move the ball as the mouse is dragged    public void onMouseDrag(Location point) {        if ( carryingBall )             {                ballInPlay.move( point.getX() - lastMouse.getX(),                                 point.getY() - lastMouse.getY() );                lastMouse = point;            }    }        // check to see if either player scored    public void onMouseRelease(Location point) {        if ( hoop.contains(point) )             {                if ( ballInPlay == leftBall )                     {                        leftScore = leftScore + 2;                        leftDisplay.setText("HOME " + leftScore );                    }                 else if ( ballInPlay == rightBall )                     {                        rightScore = rightScore + 2;                        rightDisplay.setText("VISITORS " + rightScore);                    }            }                if ( carryingBall )             {                ballInPlay.reset();                carryingBall = false;            }    }}

⌨️ 快捷键说明

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