Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Applet, Moving a ball

 
Ammy Singh
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all i am trying to write a java program to move ball in Applet. i have written the code as following, its moving the ball but the behaviors is not as i want . There should only be one ball appearing. Thanks in advance.


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should erase the background before painting the ball in the paint() method, otherwise the old image of the ball will remain visible.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling super.paint(g); as the first line in the paint method should take care of that.
But you're making an often made mistake by sleeping on the Event Dispatcher Thread. Read Concurrency in Swing for more information. In this case, a javax.swing.Timer should be a better option.
 
Ammy Singh
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Jesper de Jong

Rob Spoor, thanks for your help . I inserted super.paint(g) as first statement in for loop, it is making the old image disapear but only afetr for loop executes four times , i want to see only one image at one time , further help would be appreciated and thanks for suggesting javax.swing.Timer, i would try it.
 
Ammy Singh
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i made few changes in the logic to erase the previous image plus some additional functionality to revert the ball back to its original position and it worked thanks for helping guys.
Dear Rob Spoor will you please help me how can i do this by using javax.swing.Timer instead of sleep() methd. thanks

Hear is the code:

 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: get rid of Thread.sleep and the current call to repain(). Causing a repaint from a painting method will cause an infinite painting loop that will cause your CPU usage to go up.
Step 2: get rid of the Thread variable completely.
Step 3: create a javax.swing.Timer object. Create a new ActionListener (an anonymous inner class will be good); this ActionListener will call the repaint method in its actionPerformed method.
Step 4: tweak the timer's settings as necessary.
Step 5: start the timer.
Step 6: enjoy your program.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got the right idea how you want your program to execute but you are delegating the responsibility incorrectly. The paint method should only paint, one frame at a time, it is not responsible for the flow of the program. Another function or inner class should be responsible for the timing operations and call the paint() method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And never use an empty catch{} block. You are simply hiding the Exception from yourself, so you don't know if anything has gone wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic