Results 1 to 12 of 12
Thread: Moving GRect: Bad Code or Bug?
- 04-29-2010, 03:24 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 9
- Rep Power
- 0
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:
Here's what it looks like when I move the paddle around: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()); } }

Am I doing something wrong with my code?
Thanks in advance,
Adam
- 04-29-2010, 03:30 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
However, this style of drawing on screen will produce flickering. Do a bit of research on doubble buffering for a better way of drawing.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); }Ever seen a dog chase its tail? Now that's an infinite loop.
-
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.
-
- 04-29-2010, 03:43 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-01-2010, 11:51 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 9
- Rep Power
- 0
Hi guys - I appreciate the help. But is anyone able to run this and see if they get the same result?
- 05-02-2010, 12:14 AM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-02-2010, 12:23 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 9
- Rep Power
- 0
I feel like an idiot - I did not see that. That worked. Can someone explain to me why that worked?
- 05-02-2010, 12:25 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 05-02-2010, 12:27 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 05-02-2010, 12:32 AM #11
Member
- Join Date
- Apr 2010
- Posts
- 9
- Rep Power
- 0
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? :-)
- 05-02-2010, 12:35 AM #12
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
moving square
By blindfolded in forum New To JavaReplies: 5Last Post: 01-22-2010, 05:58 PM -
moving a file
By swati.jyoti in forum New To JavaReplies: 8Last Post: 11-23-2009, 08:44 AM -
Moving Box
By anilanar in forum New To JavaReplies: 2Last Post: 08-30-2009, 12:29 PM -
Moving textboxes
By GabWit in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:07 PM -
moving a file
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks