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 and y-coord of the paddle private static final int PADDLE_WIDTH = 60; private static final int PADDLE_HEIGHT = 20; private static final int PADDLE_Y = COURT_MARGIN + COURT_LENGTH - PADDLE_HEIGHT - 1; // the paddle itself private FilledRect paddle; // the rectangle surrounding the court playing area private FramedRect courtBoundary; public void begin() { resize(WINDOW_WIDTH, WINDOW_HEIGHT); // make the playing area courtBoundary = new FramedRect(COURT_MARGIN, COURT_MARGIN, COURT_LENGTH, COURT_LENGTH, canvas); // make the paddle paddle = new FilledRect(COURT_MARGIN + (COURT_LENGTH - PADDLE_WIDTH) / 2, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas); } public void onMouseMove(Location point) { if (point.getX() - PADDLE_WIDTH/2 < COURT_MARGIN) { // mouse past left edge, place paddle at left edge of the court paddle.moveTo(COURT_MARGIN, PADDLE_Y); } else if (point.getX() + PADDLE_WIDTH/2 > COURT_MARGIN + COURT_LENGTH) { // mouse past right edge, place paddle at right edge of the court paddle.moveTo(COURT_MARGIN + COURT_LENGTH - PADDLE_WIDTH, PADDLE_Y); } else { // mouse in the court area, keep the paddle lined up with the mouse paddle.moveTo(point.getX() - PADDLE_WIDTH/2, PADDLE_Y); } } public void onMousePress(Location point) { if (courtBoundary.contains(point)) { new PongBall(point, courtBoundary, paddle, canvas); } } }