Results 1 to 3 of 3
  1. #1
    kode is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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 */

    }

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Mouse Listeners causing Error (on Breakout)

    This is a part of your code:

    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 */
    
    }
    When you have "addMouseListeners();" you should change it to something like this:

    Java Code:
    addMouseMotionListener(new MouseListenerThatIShouldWrite());
    Then you should write your own class into existing Breakout class:

    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
    		}
    And, finally delete your "public voide mouseMover(MouseEvent e) {}" method.

  3. #3
    kode is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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.

Similar Threads

  1. SetVisible causing problem
    By eric_bloodaxe in forum NetBeans
    Replies: 2
    Last Post: 03-11-2012, 08:06 PM
  2. 3 tier architecture causing problems
    By zezinz in forum New To Java
    Replies: 7
    Last Post: 11-24-2011, 05:32 PM
  3. Recursive for loop causing problems
    By Catfish1 in forum New To Java
    Replies: 2
    Last Post: 02-22-2011, 04:12 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM
  5. What could be causing the browser to close?
    By Marcus in forum Java Applets
    Replies: 2
    Last Post: 07-04-2007, 08:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •