Results 1 to 12 of 12
  1. #1
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default Moving GRect: Bad Code or Bug?

    Hi Forum Members,

    I'm trying to move a shape around the screen, wherever my mouse pointer is. However, moving around the pointer seems to "smear" the shape, for lack of better words. Is this coding issue, or a bug? I'm using Eclipse on my mac.

    Here's the code:
    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Breakout extends GraphicsProgram {
    
    /** 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;
    
    	public void run() {
    		addMouseListeners();		
    	}
    	public void mouseMoved(MouseEvent e) {
    		GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT, (getWidth() - PADDLE_WIDTH)/2, getHeight() - PADDLE_Y_OFFSET);
    		paddle.setFilled(false);
    		add(paddle);
    		paddle.move(e.getX(), egetY());
    	}
    }
    Here's what it looks like when I move the paddle around:


    Am I doing something wrong with my code?

    Thanks in advance,
    Adam

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The thing is, you start of with a blank screen, then you draw a GRect on it. When you want to move it, you should first blank out the screen again, then draw the GRect again. Something to this effect:
    Java Code:
    void myDrawMethod() {
      Graphics g = this.getGraphics(); //get the graphics context of the current container
      g.setColor(Color.White);
      g.fillRect(0,0,this.getHeight(),this.getWidth());
      drawTheRect(g);
    }
    However, this style of drawing on screen will produce flickering. Do a bit of research on doubble buffering for a better way of drawing.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not familiar with the acm library as it's not part of standard Java, but you appear to be creating a new GRect and adding this to your graphics program with mouse movement, but I don't ever see you removing the old GRect. Is it possible to create one GRect object but have the variable be a class field and in the mouse listener, rather than creating a new GRect, simply moving the one you've got. I looked at the API, and perhaps the move or setLocation methods will do this for you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by m00nchile View Post
    However, this style of drawing on screen will produce flickering. Do a bit of research on doubble buffering for a better way of drawing.
    But as this isn't Swing or AWT but rather uses the ACM graphics libraries, do you know if you can do double buffering with the ACM graphics library? I'm not familiar with this, but if you are, please elucidate for me. Thanks.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Whoops, I kinda jumped the gun there a bit. As soon as I saw the image posted I threw down the response without even skimming through the code the OP posted. Unfortunately, my encounters with Java based graphics are limited to Swing.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Hi guys - I appreciate the help. But is anyone able to run this and see if they get the same result?

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by adamg106 View Post
    Hi guys - I appreciate the help. But is anyone able to run this and see if they get the same result?
    First tell us: have you implemented the recommendation made by Fubarable in his first post?

  8. #8
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    I feel like an idiot - I did not see that. That worked. Can someone explain to me why that worked?

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Also, since I'm pretty sure you're working on the Breakout game from the Stanford CS106A course, your paddle should not be moving in the Y direction at all. It should stay near the bottom of the screen and just move side-to-side.

    -Gary-

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by adamg106 View Post
    I feel like an idiot - I did not see that. That worked. Can someone explain to me why that worked?
    Simple. The way you were doing it, you were putting a new GRect on the canvas every time the mouse moved.

    -Gary-

  11. #11
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Gary - Thanks, I know about the Y constraints. I can do that, but haven't you always wanted to play breakout where you can move the paddle wherever you want? Isn't the whole point of programming to make the game more fun? :-)

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by adamg106 View Post
    Gary - Thanks, I know about the Y constraints. I can do that, but haven't you always wanted to play breakout where you can move the paddle wherever you want? Isn't the whole point of programming to make the game more fun? :-)
    Good point. When I play ping pong, I like to stand on the middle of the table and swing my paddle at my opponent's head. :D (I'm not invited to play very often anymore.)

    -Gary-

Similar Threads

  1. moving square
    By blindfolded in forum New To Java
    Replies: 5
    Last Post: 01-22-2010, 05:58 PM
  2. moving a file
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 08:44 AM
  3. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM
  4. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 04:07 PM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:52 PM

Posting Permissions

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