betterbasketball.java

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

JAVA
60
字号
import objectdraw.*;// This program awards points whenever the mouse is clicked// inside of the basket.public class BetterBasketBall extends WindowController{        // dimensions and locations for the hoop        private static final int HOOP_TOP = 50;        private static final int HOOP_HEIGHT = 60;        private static final int HOOP_WIDTH = 100;                // dimensions and locations for the score display        private static final int DISPLAY_FONT_SIZE = 16;        private static final int DISPLAY_TOP = 350;                // the Text object which displays the count        private Text display;        // the oval that represent the hoop        private FramedOval hoop;        // the number of points        private int score = 0;        // initialize the counter and the text message        public void begin()        {                                display = new Text("Take a shot.",                                                        0, DISPLAY_TOP, canvas);                display.setFontSize(DISPLAY_FONT_SIZE);                display.move( (canvas.getWidth()-display.getWidth())/2, 0 );                                hoop = new FramedOval( (canvas.getWidth()-HOOP_WIDTH)/2, HOOP_TOP,                                                                HOOP_WIDTH, HOOP_HEIGHT,                                                                 canvas);                        }        // increment the counter if player scores and update the text        // appropriately        public void onMouseRelease(Location point)        {                if (hoop.contains(point)) {                                                score = score + 2;                        display.setText("You have scored " + score + " points.");                                        } else {                                                display.setText("WHOOPS!  You Missed.");                }                                display.moveTo( (canvas.getWidth() - display.getWidth()) / 2,                                                DISPLAY_TOP);        }}

⌨️ 快捷键说明

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