Results 1 to 3 of 3
- 07-13-2012, 06:51 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
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 */
}
- 07-13-2012, 10:05 PM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Mouse Listeners causing Error (on Breakout)
This is a part of your code:
When you have "addMouseListeners();" you should change it to something like this:Java 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 */ }
Then you should write your own class into existing Breakout class:Java Code:addMouseMotionListener(new MouseListenerThatIShouldWrite());
And, finally delete your "public voide mouseMover(MouseEvent e) {}" method.Java 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 }
- 07-13-2012, 10:59 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
SetVisible causing problem
By eric_bloodaxe in forum NetBeansReplies: 2Last Post: 03-11-2012, 07:06 PM -
3 tier architecture causing problems
By zezinz in forum New To JavaReplies: 7Last Post: 11-24-2011, 04:32 PM -
Recursive for loop causing problems
By Catfish1 in forum New To JavaReplies: 2Last Post: 02-22-2011, 03:12 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
What could be causing the browser to close?
By Marcus in forum Java AppletsReplies: 2Last Post: 07-04-2007, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks