7 Replies - 5280 Views - Last Post: 14 November 2011 - 01:48 PM Rate Topic: -----

#1 tomasmk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-October 11

Creating a swing timer

Posted 13 November 2011 - 02:57 PM

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a swing timer

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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.
Was This Post Helpful? 0
  • +
  • -

#3 tomasmk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-October 11

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:


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(); 
	  
}
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

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)


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
}
}



Was This Post Helpful? 0
  • +
  • -

#5 tomasmk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-October 11

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(); 
	  

	  
}
}



Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Creating a swing timer

Posted 13 November 2011 - 04:19 PM

You'll have to declare your variable timers in the instance variables

public class taima implements ActionListener{
	Timer timers;


and pause too is declare nowhere
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Creating a swing timer

Posted 14 November 2011 - 01:48 PM

Good point :)

Unless you ThreadSleep() in main()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1