PatheticPong.java
import objectdraw.*;
import java.awt.*;
public class PatheticPong extends WindowController {
// position and dimensions of the court
private static final int COURT_LEFT = 50,
COURT_TOP = 50,
COURT_HEIGHT = 300,
COURT_WIDTH = 250;
// dimensions of the paddle
private static final int PADDLE_WIDTH = 50,
PADDLE_HEIGHT = 20;
FilledRect paddle;
FramedRect boundary; // the boundary of the playing area.
public void begin()
{
// make the playing area
boundary = new FramedRect(COURT_LEFT, COURT_TOP,
COURT_WIDTH, COURT_HEIGHT,
canvas);
// make the paddle
paddle = new FilledRect(COURT_LEFT + (COURT_WIDTH-PADDLE_WIDTH)/2,
COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT -1,
PADDLE_WIDTH, PADDLE_HEIGHT,
canvas);
}
public void onMouseClick(Location point)
{
// make a new ball when the player clicks
new FallingBall(canvas);
}
public void onMouseMove(Location point)
{
if ( point.getX() < COURT_LEFT ) {
// place paddle at left edge of the court
paddle.moveTo( COURT_LEFT,
COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT -1);
}
else if ( point.getX() > COURT_LEFT + COURT_WIDTH - PADDLE_WIDTH) {
// place paddle at right edge of the court
paddle.moveTo( COURT_LEFT + COURT_WIDTH - PADDLE_WIDTH,
COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT -1);
}
else {
// keep the edge of the paddle lined up with the mouse
paddle.moveTo( point.getX(),
COURT_TOP + COURT_HEIGHT - PADDLE_HEIGHT -1);
}
}
}
FallingBall.java
import objectdraw.*;
import java.awt.*;
public class FallingBall extends ActiveObject
{
private static final int BALLSIZE = 30;
private static final int TOP = 50;
private static final int BOTTOM = 600;
private static final int SCREENWIDTH = 400;
private static final double Y_SPEED = 4;
private static final int DELAY_TIME = 33;
private FilledOval ball;
public FallingBall(DrawingCanvas canvas)
{
ball = new FilledOval(SCREENWIDTH/2 ,TOP,
BALLSIZE, BALLSIZE, canvas);
start();
}
public void run()
{
while (ball.getY() < BOTTOM ) {
ball.move(0, Y_SPEED);
pause(DELAY_TIME);
}
ball.removeFromCanvas();
}
}