Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Urgent: Swing application for stopwatch producing different output after a few runs

  1. #1
    princehektor is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Swing application for stopwatch producing different output after a few runs

    Quote Originally Posted by princehektor View Post
    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).
    Maybe the stop watch in your wrist watch suddenly speeds up after a few seconds ...

    kind regards,

    Jos ;-)
    KevinWorkman likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    princehektor is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default 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?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Urgent: Swing application for stopwatch producing different output after a few ru

    Google for "timer accuracy Windows" and see the dramatic results ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Urgent - Browser Integration In Swing!
    By tavy88 in forum Advanced Java
    Replies: 7
    Last Post: 04-13-2011, 07:20 PM
  2. Urgent help! Swing app.
    By M.a.T in forum AWT / Swing
    Replies: 2
    Last Post: 04-15-2010, 03:43 PM
  3. Replies: 0
    Last Post: 04-22-2009, 01:30 PM
  4. Swing Tables - urgent
    By varun2002 in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:44 PM
  5. Java Swing class capturing output to the console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •