Results 1 to 8 of 8
- 09-19-2011, 05:40 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Swing application for stopwatch producing different output after a few runs
Hi,
I know this may sound very very stupid and highly improbable , but a swing application that i wrote for
implementing a stop watch produces different output after few times of build and run. The actual problem
is that for the first 4-5 times , my stop watch runs perfectly when i compare it side by side with the
stop-watch(in my wrist watch). But after that, it goes slow(the seconds get visibly longer and loses sync
with my wrist-watch). It actually does(i know it sounds ridiculous) . I tried exporting to a zip file and
switching to another workspace, but the problem comes back after 4-5 runs.
Somebody please help.
Java Code:import java.awt.FlowLayout; import javax.swing.*; //import javax.swing.JPopupMenu.Separator; //import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /*import java.awt.event.ContainerEvent; import java.awt.event.ContainerListener; import java.awt.*; import java.text.*;*/ import javax.swing.Timer; public class TimerUIClass implements ActionListener{ public int mm1=0,mm2=0,ss1=0,ss2=0,hs1=0,hs2=0,temp=0; int start_counter=0,pause=0,reset=0,x=0,y=0,z=0; JFrame myframe=new JFrame("Timer"); JPanel mypanel=new JPanel(new FlowLayout()); JPanel mypanel2=new JPanel(new FlowLayout()); JTextField mytextarea1=new JTextField(1); JTextField mytextarea2=new JTextField(1); JTextField mytextarea3=new JTextField(1); JTextField mytextarea4=new JTextField(1); JTextField mytextarea5=new JTextField(1); JTextField mytextarea6=new JTextField(1); JLabel mylabel=new JLabel(); JButton button_start= new JButton("Start"); JButton button_reset= new JButton("Reset"); JButton button_pause= new JButton("Pause"); Timer myperhundredsecondstimer=new Timer(10,new ActionListener(){ public void actionPerformed(ActionEvent evt) { updatePerHundredSecondsValue(); } }); public void uiSetUpFunction() { myframe.setVisible(true); myframe.setSize(300,200); mylabel.setText(" "); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mypanel.add(mytextarea1); mypanel.add(mytextarea2);mypanel.add(mytextarea3); mypanel.add(mytextarea4);mypanel.add(mytextarea5); mypanel.add(mytextarea6); mypanel.add(mylabel); mypanel.add(button_start); mypanel.add(button_reset);mypanel.add(button_pause); myframe.add(mypanel); button_start.addActionListener(this); button_reset.addActionListener(this); button_pause.addActionListener(this); clearValues(); } @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent e) { Object source=e.getSource(); if(source==button_start) { if(reset==1) start_counter=0; start_counter++; if(start_counter==1) button_start.disable(); if(pause==1) { myperhundredsecondstimer.start(); pause=0; } myperhundredsecondstimer.start(); } if(source==button_reset) { reset=1; resetValues(); } if(source==button_pause) { pause=1; myperhundredsecondstimer.stop(); } } public void updatePerHundredSecondsValue() { hs2++; if(hs2==10) { hs1++; hs2=0; } if(hs1==10) { hs1=0;hs2=0; updateSecondsValue(); } mytextarea5.setText(Integer.toString(hs1)); mytextarea6.setText(Integer.toString(hs2)); myperhundredsecondstimer.start(); } public void updateSecondsValue() { ss2++; if(ss2==10) { ss1++; ss2=0; myperhundredsecondstimer.start(); } if(ss1==6) { ss1=0;ss2=0;hs1=0;hs2=0; updateMinutesValue(); } mytextarea3.setText(Integer.toString(ss1)); mytextarea4.setText(Integer.toString(ss2)); myperhundredsecondstimer.start(); } public void updateMinutesValue() { mm2++; if(mm2==10) { mm1++; mm2=0; myperhundredsecondstimer.start(); } if(mm1==6) { normalReset(); } mytextarea1.setText(Integer.toString(mm1)); mytextarea2.setText(Integer.toString(mm2)); myperhundredsecondstimer.start(); } public void resetValues() { if(pause==1) { myperhundredsecondstimer.stop(); clearValues(); } else if(reset==1 && start_counter==1) { myperhundredsecondstimer.stop(); clearValues(); start_counter=0; reset=0; myperhundredsecondstimer.start(); } else if(reset==1) clearValues(); } public void pauseValues() { clearValues(); } public void clearValues() { mytextarea1.setText("0"); mytextarea2.setText("0"); mytextarea3.setText("0"); mytextarea4.setText("0"); mytextarea5.setText("0"); mytextarea6.setText("0"); ss1=0;ss2=0;mm1=0;mm2=0;hs1=0;hs2=0; } public void normalReset() { clearValues(); myperhundredsecondstimer.start(); } public static void main(String[] args) { TimerUIClass obj=new TimerUIClass(); obj.uiSetUpFunction(); } }Last edited by princehektor; 09-19-2011 at 05:54 AM.
-
Re: Urgent: Swing application for stopwatch producing different output after a few ru
If you edit your post and place a [code] tag above your block of code and the tag [/code] below your block of code, and then if you get rid of all references to "urgent" in your post and all mention of deadlines, I'll take a look at it, but you'll have to be "urgent" about it as I'm just about to go to bed. Also, you need to add a main method so we can run your code.
Last edited by Fubarable; 09-19-2011 at 05:46 AM.
-
Re: Urgent: Swing application for stopwatch producing different output after a few ru
After looking at your code, I would suggest that you do things differently. The time interval of a Swing Timer is not that reliable, especially at the very short interval of 10 milliseconds. So instead, I recommend that you set your textfield numbers using the difference between your start system time and the current system time (in milliseconds). This way, even if the Swing Timer runs slower, the output is still pretty reliable.
Last edited by Fubarable; 09-19-2011 at 06:01 AM.
- 09-19-2011, 06:57 AM #4
Re: Urgent: Swing application for stopwatch producing different output after a few ru
Also, all GUI setup code should set the frame visible after, not before, all components have been added. And go through Code Conventions for the Java(TM) Programming Language: Contents -- your indentations are bad, and variable names like myperhundredsecondstimer don't conform to convention and are difficult to read.
db
- 09-19-2011, 08:58 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Swing application for stopwatch producing different output after a few runs
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-20-2011, 05:00 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Re: Urgent: Swing application for stopwatch producing different output after a few ru
Thanks everyone for the help. I have decided to use this implementation but need reasons as to why its not accurate for a period of 10ms. I've googled a lot about this, but can't find anything explaining as to why a swing timer is not reliable. Maybe i am looking at the wrong places or not searching enough.
Can someone please point me in the right direction?
- 09-20-2011, 05:20 PM #7
Re: Urgent: Swing application for stopwatch producing different output after a few ru
It's not that "swing timer is not reliable". It's that Swing Timers are OS-dependent. Your OS probably doesn't have timing down to the single ms level, so a Swing Timer can't either.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 09-20-2011, 05:21 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Urgent - Browser Integration In Swing!
By tavy88 in forum Advanced JavaReplies: 7Last Post: 04-13-2011, 07:20 PM -
Urgent help! Swing app.
By M.a.T in forum AWT / SwingReplies: 2Last Post: 04-15-2010, 03:43 PM -
Sending J2ME application by blue tooth (by J2ME application). Very URGENT!!!
By maruffaiz in forum CLDC and MIDPReplies: 0Last Post: 04-22-2009, 01:30 PM -
Swing Tables - urgent
By varun2002 in forum AWT / SwingReplies: 1Last Post: 04-24-2008, 05:44 PM -
Java Swing class capturing output to the console
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:24 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks