• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To make moving ball

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain me that little bit more detailed, please?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maja neskovic wrote:


Huh?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code isn't doing much.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wouldn't be legal, so no.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic