import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
public class Breakout1 extends GraphicsProgram {
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_Y_OFFSET = 30;
/**Number of bricks per row */
private static final int NBRICKS_PER_ROW = 10;
/**
* Number of rows of bricks
*/
private static final int NBRICK_ROWS = 10;
/**
* Separation between bricks
*/
private static final int BRICK_SEP = 4;
/**
* Width of a brick
*/
private static final int BRICK_WIDTH =
(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
/**
* Height of a brick
*/
private static final int BRICK_HEIGHT = 8;
/**
* Radius of the ball in pixels
*/
private static final int BALL_RADIUS = 10;
/**
* Offset of the top brick row from the top
*/
private static final int BRICK_Y_OFFSET = 70;
/**
* Number of turns
*/
private static final int NTURNS = 3;
/* Method: init() */
/**
* Sets up the Breakout program.
*/
public void init() {
add (new GImage("C:\\Users\\Home\\Desktop\\beach.jpg"));
drawCanvas();
drawBrickWall();
drawPaddle();
drawBall();
addMouseListeners();
}
/* Method: run() */
/**
* Runs the Breakout program.
*/
public void run() {
}
private GRect brick;
private GOval ball;
private GRect paddle;
private void drawBrickWall() {
for (int i = 0; i < NBRICK_ROWS; i++)
for (int j = 0; j < NBRICKS_PER_ROW; j++)
drawBrick(i, j);
}
private void drawBrick(int row, int col) {
double x, y; // brick location
GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
x = computeXOffset() + col * (BRICK_WIDTH + BRICK_SEP);
y = BRICK_Y_OFFSET + row * (BRICK_HEIGHT + BRICK_SEP);
brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
brick.setFilled(true);
add(brick, x, y);
if (row < 2) {
brick.setColor(Color.RED);
} else if (row == 2 || row == 3) {
brick.setColor(Color.ORANGE);
} else if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
} else if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
} else if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}
}
private double computeXOffset() {
return 0.5 * (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP - BRICK_WIDTH * NBRICKS_PER_ROW);
}
private void drawPaddle() {
GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle, 0.5 * (WIDTH - PADDLE_WIDTH), HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
addMouseListeners();
}
private boolean isMouseXInsideWindow(int x) {
return ((x > PADDLE_WIDTH / 2) && (x < (WIDTH - PADDLE_WIDTH / 2)));
}
public void mouseMoved(MouseEvent e) {
if (isMouseXInsideWindow(e.getX())) {
paddle.setLocation(paddle.getX() - PADDLE_WIDTH / 2, paddle.getY());
}
}
private void drawBall(){
GOval ball = new GOval(2*BALL_RADIUS, 2*BALL_RADIUS);
ball.setFilled(true);
add(ball, 0.5*WIDTH - BALL_RADIUS , 0.5*HEIGHT - BALL_RADIUS );
}
private void drawCanvas(){
GLabel myLabel= new GLabel ("Welcome to my Breakout Game!");
myLabel.setFont("Serif-bold-24");
myLabel.setColor(Color.CYAN);
add(myLabel,getXCenter(myLabel), getYCenter(myLabel));
}
private double getXCenter (GObject g){
return (getWidth()-g.getWidth())/2;
}
private double getYCenter (GObject g){
return (getHeight()-g.getHeight())/2;
}
}
it keeps giving me error NULLPOINTEXCEPTION as I run the program, Can someone try to check what is wrong with the mouseMoved(MouseEvent e), the paddle won't move and keeps giving me error. but the rest of the code are working.