Hey guys, i've been trying to learn to create timers for 2 days now, but i just cant get it. there seems to be the need to insert an actionlistener....and an actionevent....wtf??
i took this from the java tutorials:
timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
which should create a timer...
anyway what I want my timer to do is manage a clock that will count the amount of time elapsed in the game im making. I dont get how to make my timer update this clock...or where should I insert those actionlisteners and actionevents...
please help!
Creating a swing timer
Page 1 of 17 Replies - 2106 Views - Last Post: 14 November 2011 - 01:48 PM
Replies To: Creating a swing timer
#2
Re: Creating a swing timer
Posted 13 November 2011 - 03:02 PM
Take a look at this tutorial first.
Essentially in your actionlistener for the timer you should increment some counter variable that defines how long the game has been running. If you timer fires every second or whatever, the counter increments, and just update a label etc with the new counter value.
Essentially in your actionlistener for the timer you should increment some counter variable that defines how long the game has been running. If you timer fires every second or whatever, the counter increments, and just update a label etc with the new counter value.
#3
Re: Creating a swing timer
Posted 13 November 2011 - 03:17 PM
i had...with no effect
i try to put the code in that page but eclipse just marks it as mistakes:
i had...with no effect
i try to put the code in that page but eclipse just marks it as mistakes:
i try to put the code in that page but eclipse just marks it as mistakes:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class taima {
public static void main(String[] args){
taima pancho = new taima();
pancho.loco();
}
public void loco(){
//these are the things I copied, but eclipse detects them as mistakes!
timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
}
}
i had...with no effect
i try to put the code in that page but eclipse just marks it as mistakes:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class taima {
public static void main(String[] args){
taima pancho = new taima();
pancho.loco();
}
public void loco(){
//these are the things I copied, but eclipse detects them as mistakes!
timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
}
}
#4
Re: Creating a swing timer
Posted 13 November 2011 - 03:25 PM
If you to this
timer = new Timer(speed, this);
the "this" means that your class taima has to implement ActionListener
and needs a method
public void actionPerformed(ActionEvent e)
timer = new Timer(speed, this);
the "this" means that your class taima has to implement ActionListener
and needs a method
public void actionPerformed(ActionEvent e)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class taima implements ActionListener {
public static void main(String[] args){
taima pancho = new taima();
pancho.loco();
}
public void loco(){
//these are the things I copied, but eclipse detects them as mistakes!
timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
}
public void actionPerformed(ActionEvent e) {
// code when Timer expires
}
}
#5
Re: Creating a swing timer
Posted 13 November 2011 - 03:27 PM
just made those changes, but eclipse keeps detecting mistakes at the same points:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class taima implements ActionListener{
public static void main(String[] args){
taima pancho = new taima();
pancho.loco();
}
public void actionPerformed(ActionEvent e) {
}
public void loco(){
//mistakes ahead
timers = new Timer(54, this);
timers.setInitialDelay(pause);
timers.start();
}
}
#6
Re: Creating a swing timer
Posted 13 November 2011 - 04:19 PM
You'll have to declare your variable timers in the instance variables
and pause too is declare nowhere
public class taima implements ActionListener{
Timer timers;
and pause too is declare nowhere
#7
Re: Creating a swing timer
Posted 13 November 2011 - 10:58 PM
Also, the whole point of a Swing Timer is to be used with your GUI. The Swing Timer runs on the EventDispatchingThread. So if there is no GUI, your program will simply exit.
#8
Re: Creating a swing timer
Posted 14 November 2011 - 01:48 PM
Good point 
Unless you ThreadSleep() in main()
Unless you ThreadSleep() in main()
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|