wood burning stoves 2.0*
The moose likes Applets and the fly likes Java Applet, Moving a ball Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Java Applet, Moving a ball" Watch "Java Applet, Moving a ball" New topic
Author

Java Applet, Moving a ball

Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
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.




SCJP1.4
i luv Ranch it always helps...!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

You should erase the background before painting the ball in the paint() method, otherwise the old image of the ball will remain visible.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
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

Joined: Oct 17, 2008
Posts: 36
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

Joined: Oct 27, 2005
Posts: 19680
    
  19

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

Joined: Apr 30, 2005
Posts: 164
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

Joined: Oct 13, 2005
Posts: 38483
    
  23
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
 
subject: Java Applet, Moving a ball