This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes To make moving ball Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To make moving ball" Watch "To make moving ball" New topic
Author

To make moving ball

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I'm trying to make moving ball animation in Swing, but I don't know how. Can anybody help with this? I was reading about threads, but I didn't understand a lots of it...
Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
If you just need to make animation for moving ball, then you don't need to use mutithreading. You can achieve this just using repaint method.
You just need to decide path for Ball, and keep repainting the ball.


Chintan Patel, SCJP1.5, http://datewithjava.blogspot.com
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I wrote this:



but I got just line of drawn balls. How to fix that? I would like to make more balls moving in line order one behind the other.
Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
You have to keep logic such that only X and Y coordinates will get change every time you re paint it.
g.fillOval(130, i, 20, 20);
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Can you explain me that little bit more detailed, please?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Don't ever call repaint() from paintComponent or paint - it will cause the method to call itself and your CPU usage goes up like a nutter.

Instead, check out javax.swing.Timer - you'll want to regularly cause a repaint with new coordinates, and the Timer will do the firing for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Hi, guys! I have been dealing with this problem and wrote this code:



This is code just for JPanel on which is my work drawn and it is then called from the JApplet. I would like to make 3 balls (from 5 of them) to get in horizontal line and to wait a moment simulating people waiting on 3 information desks. I hope I didn't explain too confusing

P.S. If you thing that I should make new topic for that problem, just say and I'll do it
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4575
    
    5

maja neskovic wrote:


Huh?
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
What?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That code isn't doing much.
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Yes, I saw when I removed it And what about my question - about making 3 balls to stop in line and to wait a little bit? Any idea...?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39090
    
  23
What books do you have access to? There is an example in Horstmann and Cornell (I have the 6th edition myself and it is in the 1st chapter in vol II), which sounds quite like what you are doing.
I would have thought the other books would have similar examples, so try your library.
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I have only Java in 21 days, but there is nothing about my problems... Would you be kind to send me that book on email?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That wouldn't be legal, so no.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39090
    
  23
Don't like the Java in 21 days book or similar, myself.


And I won't send my book by snail-mail, because I wish to keep it
 
 
subject: To make moving ball