Mouse Listeners causing Error (on Breakout)
Hi, I am trying to learn to code, and am following the Stanford CS106A course.
I am getting a bug when I try create a paddle for the "Breakout" assignment. I simply cannot get the mouseMoved() method for addListeners() to work.
I have tried taking a complete solution to the problem, (which works in Eclipse) and then reducing it to the first steps, (setting up wall and paddle), and when I do this, the paddle won't work either.
i.e. the paddle won't work either in my code, or borrowed code that otherwise works.
The error on the left hand side is
"overrides acm.program.Program.mouseMoved"
I have tried adding
"@override"
before the mouseMoved method, but it makes no odds.....
/*
* File: Breakout.java
* -------------------
* Name:
* Section Leader:
*
* This file will eventually implement the game of Breakout.
*/
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
/** Width and height of application window in pixels */
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
/** Dimensions of game board (usually the same) */
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
/** Dimensions of the paddle */
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
/** Offset of the paddle up from the bottom */
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: run() */
/** Runs the Breakout program. */
public void run() {
createGameEnvironment(); /*sets up game*/
}
private void createGameEnvironment()
{
addWall();
addPaddle();
}
private void addWall()
{
for (int h = 0; h < NBRICK_ROWS; h++)
{
for (int i = 0; i < NBRICKS_PER_ROW; i++)
{
for (int j = 0; j < NBRICKS_PER_ROW; j++)
{
int x = BRICK_SEP/2 + j * (BRICK_SEP + BRICK_WIDTH);
GRect brick = new GRect (x, (h * (BRICK_HEIGHT + BRICK_SEP)) + BRICK_Y_OFFSET, BRICK_WIDTH,BRICK_HEIGHT);
if (h < 2) {brick.setFillColor(Color.red);}
else if (h < 4) {brick.setFillColor(Color.orange);}
else if (h < 6 ) {brick.setFillColor(Color.yellow);}
else if (h < 8 ) {brick.setFillColor(Color.green);}
else {brick.setFillColor(Color.cyan);}
brick.setFilled(true);
add(brick);
}
}
}
}
private void addPaddle()
{
double x = (WIDTH - PADDLE_WIDTH)/2;
double y = HEIGHT - PADDLE_Y_OFFSET;
GRect paddle = new GRect (x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFillColor(Color.black);
paddle.setFilled(true);
add(paddle);
addMouseListeners();
}
/* Sets paddle to move with mouse cursor, stopping before it exits the canvas. */
@Override
public void mouseMoved(MouseEvent e) {
double paddleX = e.getX();
double paddleY = HEIGHT - PADDLE_Y_OFFSET;
paddle.setLocation(paddleX, paddleY);
if ((paddleX + PADDLE_WIDTH) > WIDTH) {
paddle.setLocation((WIDTH - PADDLE_WIDTH), paddleY);
}
}
/* Instance variables */
private GRect paddle; /* The object being dragged */
}
Re: Mouse Listeners causing Error (on Breakout)
This is a part of your code:
Code:
addMouseListeners();
}
/* Sets paddle to move with mouse cursor, stopping before it exits the canvas. */
@Override
public void mouseMoved(MouseEvent e) {
double paddleX = e.getX();
double paddleY = HEIGHT - PADDLE_Y_OFFSET;
paddle.setLocation(paddleX, paddleY);
if ((paddleX + PADDLE_WIDTH) > WIDTH) {
paddle.setLocation((WIDTH - PADDLE_WIDTH), paddleY);
}
}
/* Instance variables */
private GRect paddle; /* The object being dragged */
}
When you have "addMouseListeners();" you should change it to something like this:
Code:
addMouseMotionListener(new MouseListenerThatIShouldWrite());
Then you should write your own class into existing Breakout class:
Code:
private class MouseListenerThatIShouldWrite implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
paddleX = e.getX();
paddleY = e.getY();
// write rest of your code here
}
And, finally delete your "public voide mouseMover(MouseEvent e) {}" method.
Re: Mouse Listeners causing Error (on Breakout)
Hi Cselic,
Thank you for taking the time and making the effort to reply.
I will try this immediately.