Results 1 to 5 of 5
Thread: stopping timer
- 03-06-2012, 09:49 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 15
- Rep Power
- 0
stopping timer
Hi,
In the following code i can move the car object when pressing the button, but the problem is that i am unable to stop the car before it exits the panel.
can any one help me??
thx.Java Code:final ActionListener animation = new ActionListener(){ int i = 1; public void actionPerformed(ActionEvent e){ {int j= i++; car.setBounds(2+j,260,50,50); System.out.println(j);} } }; ActionListener buttonAction = new ActionListener(){ public void actionPerformed(ActionEvent e){ final Timer timer = new Timer(10,animation); } };
- 03-06-2012, 11:37 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: stopping timer
I really hope your code isn't actually formed like that. Some better indentation will make it much easier to understand. If you declare the timer in a larger scope you can reference it from other methods and call timer.stop().
- 03-07-2012, 08:49 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 15
- Rep Power
- 0
Re: stopping timer
Dear Dunde887
Thanks for your reply and can you give me a more detailed explanation or if you can indicate a link to a similair problem.
- 03-08-2012, 11:05 PM #4
Re: stopping timer
What he is saying is that if you declare your time in an accessible place with an accessible scope, you can access it from anywhere (with the help of an accessor method if needed) in your program. Once you can 'see' it, you can call it's methods.
In this way, other actionHandlers or classes can ask the main class for access to the timer, and then call timer.stop() on said timer.
- 03-08-2012, 11:23 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: stopping timer
I can link you to some code of mine which uses timers, https://github.com/hegek87/Breakout/...r/src/breakout
Similar Threads
-
JOptionPane stopping timer
By a_programmer in forum AWT / SwingReplies: 8Last Post: 08-09-2011, 03:43 AM -
stopping the thread
By kailash in forum Threads and SynchronizationReplies: 2Last Post: 02-06-2011, 01:21 PM -
Stopping a thread
By Arne in forum Threads and SynchronizationReplies: 9Last Post: 10-21-2010, 10:26 AM -
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-04-2010, 11:15 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks