This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes need help to move a ball from scratch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "need help to move a ball from scratch" Watch "need help to move a ball from scratch" New topic

need help to move a ball from scratch

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1012
Can anyone please help me to move a ball step by step, i found lots of codes on internet for moving ball, but i want to understand it step by step...
initially i created two classes that displaying a ball on window...

what the next things i should do to move this ball??
please let me know step by step..
this can be a silly question but i need to clear my concept, so help me guys....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if using a JPanel, in paintComponent()
you set the color and fillOval(x,y,w,h),
where w,h sets the size of the ball, and
x,y sets its location.

you give x,y initial values of (say) 10,100
then you start a timer to change x and/or y
and at the same time call panel.repaint()
which will 'repaint' the ball at its new location.

the time delay/interval of the timer will
determine how fast your animation appears.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1012
okay , i set the initial value of x,y and w,z and
now how and where i use the timer class to change the x and y coordinates??

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43885
It’s in the Java Tutorials.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347

there is a similar discussion here

Visit my download page
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

Not to be too picky, but why would the GUI stuff (JFrame) be under the CBall class? It should be under the main class(or at least it seems more logical that way)
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: need help to move a ball from scratch
It's not a secret anymore!