import objectdraw.*; // a simple one-player version of the game Pong public class Pong extends FrameWindowController { // dimensions of the window private static final int WINDOW_WIDTH = 500; private static final int WINDOW_HEIGHT = 550; // margin between court and window boundaries private static final int COURT_MARGIN = 50; private static final int COURT_LENGTH = WINDOW_WIDTH - 2 * COURT_MARGIN; // dimensions of the paddle private static final int PADDLE_WIDTH = 60; private static final int PADDLE_HEIGHT = 20; // the paddle itself private FilledRect paddle; // the rectangle surrounding the court playing area private FramedRect court; public void begin() { resize(WINDOW_WIDTH, WINDOW_HEIGHT); // make the playing area court = new FramedRect(COURT_MARGIN, COURT_MARGIN, COURT_LENGTH, COURT_LENGTH, canvas); // make the paddle paddle = new FilledRect(COURT_MARGIN + (COURT_LENGTH - PADDLE_WIDTH) / 2, COURT_MARGIN + COURT_LENGTH - PADDLE_HEIGHT - 1, PADDLE_WIDTH, PADDLE_HEIGHT, canvas); } public void onMouseMove(Location point) { double paddleY = COURT_MARGIN + COURT_LENGTH - PADDLE_HEIGHT - 1; if (point.getX() < COURT_MARGIN) { // mouse past left edge, place paddle at left edge of the court paddle.moveTo(COURT_MARGIN, paddleY); } else if (point.getX() > COURT_MARGIN + COURT_LENGTH - PADDLE_WIDTH) { // mouse past right edge, place paddle at right edge of the court paddle.moveTo(COURT_MARGIN + COURT_LENGTH - PADDLE_WIDTH, paddleY); } else { // mouse in the court area, keep the paddle lined up with the mouse paddle.moveTo(point.getX() - PADDLE_WIDTH / 2, paddleY); } } public void onMouseClick(Location point) { // make a ball appear! new PongBall(point, paddle, court, canvas); } }