aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Movement is wacky, just increases. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Movement is wacky, just increases." Watch "Movement is wacky, just increases." New topic
Author

Movement is wacky, just increases.

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


Movement is "wacky". q and w get wider for some reason. The goal of this is to have the ball move to the other side and stay at the same size. What am I doing wrong?
Thanks,
cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Why do you still create a timer within your paintComponent method? Each repaint there will be another timer, and therefore your movements become faster and faster.
And where is the call to super.paintComponent? And why are your variables Doubles instead of doubles? Again, move the timer code to the constructor. It doesn't belong in the paintComponent method.

Your timer increases not only "w" (the X location) but also "q" (the width). That's why your ellipse is increasing in size each time.

So:
- move the timer code to the newly created constructor.
- add super.paintComponent(g).
- remove the code that increases "q".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

So like this?


Would a BufferedImage get rid of the previous circles?

Thanks so much,
cc11rocks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

No, nothing like this. I mentioned a constructor, but you don't have any. Instead you include a new variable with only one purpose - let only one timer be created from the paintComponent method.

This is what I meant:
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

YOU ROCK MAN!!! Thank you so much. If you have any other tips, please share. There is one thing you missed. I'm just telling you for future reference:

You have to add this line to public Ball() or paintComponent() for the background to show up blue.
Thank you so so much,
cc11rocks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You don't need to; line 48 from my code does the same. But yes, setting it from the constructor should also work. Don't set the background from the painting code, because I believe it will trigger another repaint - which will set the color which will trigger another repaint, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Movement is wacky, just increases.
 
Similar Threads
Nothing seems to be moving?
Suggestions to make code (Pong) better (from the code I have, not adding additional code)
MouseListener isn't working right
Having touble adding graphics to JPanel
MouseDragged doesn't seem to be working