This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Movement is wacky, just increases.

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So like this?


Would a BufferedImage get rid of the previous circles?

Thanks so much,
cc11rocks
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic