Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer needs a tweak plz! 1

Status
Not open for further replies.

jodders

Programmer
Nov 12, 2003
13
GB
hi all,ive created a timer and im almost managed to get it working. The problem i have is the timer display.The time countsdown,but the display of the time is incorrect.

Below shows a working timer,but if you compile the program,you can see whats wrong if it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class Countdown extends JFrame implements ActionListener{
JLabel count = new JLabel(" 00 : 00 : 00 "); //secs display
JTextField[] tf = new JTextField[3];
JButton btn = new JButton("Go!"); //button to start the clock
int ctr; //int counter
String output = "", temp;

public Countdown() {
super("Game Timer");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel msg = new JLabel("Enter the length of time, you want to play:");
JLabel lblSec = new JLabel("sec:");
btn.addActionListener(this);
count.setFont(new Font("Arial",Font.BOLD, 20));

for(int i = 0; i < tf.length; i++) tf = new JTextField(3);
Container frame = getContentPane();
JPanel jp = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel mid = new JPanel(new FlowLayout());
JPanel bottom = new JPanel(new FlowLayout());
top.add(msg);
mid.add(lblSec);
mid.add(tf[2]);
bottom.add(btn);
bottom.add(count);
jp.add(top,BorderLayout.NORTH);
jp.add(mid,BorderLayout.CENTER);
jp.add(bottom,BorderLayout.SOUTH);
frame.add(jp);
setLocation(700,200);
setContentPane(frame);
pack();
setVisible(true);

}


public void actionPerformed(ActionEvent ae) {
startTheClock();
}

private void startTheClock() {
for(int i = 0; i < tf.length; i++)
{
if(tf.getText().equals(""))
{
tf.setText("0");
}
}

Integer.parseInt(tf[2].getText())*1000;


final java.util.Timer tmr = new java.util.Timer();
tmr.scheduleAtFixedRate(new TimerTask()
{
public void run()
{

temp = ""+(ctr%(60*1000)/1000);
if(temp.length() == 1) temp = "0"+temp;
output += temp+" ";
count.setText(output);
ctr -= 1000;
if(ctr < 0) tmr.cancel();
}
},0,1000);
}


public static void main(String args[]){
new Countdown();
}

}
 
Code:
        // Member variables
	ActionListener taskPerformer;
	int delay;
	int end;
	javax.swing.Timer tmr;
	
	public void actionPerformed(ActionEvent ae)
	{    
		end = Integer.parseInt (tf[2].getText ());
		ctr = end-1;
		tmr.start();
	}  

	public Countdown()  
	{    
		super ("Game Timer"); 
                // your code...   
		JPanel bottom = new JPanel(new FlowLayout());
		// new:
		delay = 1000; //milliseconds
		taskPerformer = new ActionListener() 
		{
			public void actionPerformed(ActionEvent evt) 
			{
				output = " " + ctr;
				--ctr;
				if (ctr < 0) 
				{
					count.setText ("0 - one more time?");
					tmr.stop ();
				}
				else count.setText (output);
			}
		};
		tmr = new javax.swing.Timer (delay, taskPerformer);
                // your code ...
		setVisible(true);      
	}  
	
	public static void main(String args[])
	{
		new Countdown();
	}
}
This works and doesn't use util.timer, but swing.timer.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class Countdown extends JFrame implements ActionListener{
JLabel count = new JLabel(" 00 : 00 : 00 "); //secs display
JTextField[] tf = new JTextField[3];
JButton btn = new JButton("Go!"); //button to start the clock
int ctr; //int counter
static int myCtr;
String output = "", temp;

public Countdown() {
super("Game Timer");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel msg = new JLabel("Enter the length of time, you want to play:");
JLabel lblSec = new JLabel("sec:");
btn.addActionListener(this);
count.setFont(new Font("Arial",Font.BOLD, 20));

for(int i = 0; i < tf.length; i++) tf = new JTextField(3);
Container frame = getContentPane();
JPanel jp = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel mid = new JPanel(new FlowLayout());
JPanel bottom = new JPanel(new FlowLayout());
top.add(msg);
mid.add(lblSec);
mid.add(tf[2]);
bottom.add(btn);
bottom.add(count);
jp.add(top,BorderLayout.NORTH);
jp.add(mid,BorderLayout.CENTER);
jp.add(bottom,BorderLayout.SOUTH);
frame.add(jp);
setLocation(700,200);
setContentPane(frame);
pack();
setVisible(true);

}


public void actionPerformed(ActionEvent ae) {
startTheClock();
}

private void startTheClock() {
for(int i = 0; i < tf.length; i++)
{
if(tf.getText().equals(""))
{
tf.setText("0");
}
}

ctr = Integer.parseInt(tf[2].getText())*1000;
myCtr = Integer.parseInt(tf[2].getText());

final java.util.Timer tmr = new java.util.Timer();

TimerTask tt = new TimerTask(){
public void run()
{
System.out.println("myCtr"+myCtr);
temp = ""+(ctr%(60*1000)/1000);
if(temp.length() == 1) temp = "0"+temp;
output = temp+" ";
ctr -= 1000;
count.setText(output);

if (myCtr==0)
{
tmr.cancel();
}
myCtr--;
}
};

tmr.schedule(tt,0,1000);

}


public static void main(String args[]){
new Countdown();

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top