Results 1 to 2 of 2
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Stopwatch not working.

    I have created a stop watch, and it is not working.

    The stop watch format is hh:mm:ss:mm
    However, it always display as 0:0:0:1 no matter how long it is active.


    Java Code:
    package clock.core;
    
    enum Direction
    {
    	POSETIVE,
    	NEGATIVE;
    }
    
    public class Clock 
    {
    	public static void main (String... args) throws InterruptedException
    	{
    		Clock c = new Clock();
    		c.start();
    		Thread.sleep(25);
    		c.stop();
    		System.out.println(c.toString());
    	}
    	
    	private int hour,minute,second,milli;
    	private boolean running, posetive, stop;
    	private Direction direction;
    	private Thread clock;
    	private TimerThread counterThread;
    	private transient int debugCounter = 0;
    	
    	public Clock () {}
    
    	public void start ()
    	{
    		if (running)
    			throw new IllegalStateException ("Can not start a clock that is already running.");
    		
    		init ();		
    		clock = new Thread ()
    		{
    			@Override
    			public void run ()
    			{
    				int currentTime = 0;
    				counterThread = new TimerThread (Clock.this);
    				counterThread.start();
    				
    				while (!stop)
    				{
    					if (counterThread.millisecond <= currentTime)
    						continue;
    					else
    						currentTime = counterThread.millisecond;
    					
    					if (posetive && direction == Direction.POSETIVE ||
    					   !posetive && direction == Direction.NEGATIVE)
    						increment ();
    					else if (posetive && direction == Direction.NEGATIVE)
    					{
    						decrement ();
    						if (hour == 0 && minute == 0 && second == 0 && milli == 0)
    							posetive = false;
    					}
    					else if (!posetive && direction == Direction.POSETIVE)
    					{
    						decrement ();
    						if (hour == 0 && minute == 0 && second == 0 && milli == 0)
    							posetive = true;
    					}
    				}				
    			}
    		};
    		clock.start();
    		running = true;
    	}
    	
    	public void stop ()
    	{
    		stop = true;
    		running = false;
    	}
    	
    	public void shift ()
    	{
    		direction = (direction == Direction.POSETIVE) ? Direction.NEGATIVE : Direction.POSETIVE;
    	}
    	
    	@Override
    	public String toString ()
    	{
    		StringBuilder time = new StringBuilder (13);
    		if (!posetive)
    			time.append("-");
    		time.append(hour).append(":").append(minute).append(":").append(second).append(":").append(milli);
    		return time.toString();
    	}
    	
    	private void increment ()
    	{
    		if (++milli == 999)
    		{
    			milli = 0;
    			if (++second == 59 )
    			{
    				second = 0;
    				if (++minute > 59)
    				{
    					minute = 0;
    					hour++;
    				}
    			}
    		}
    	}
    	
    	private void decrement ()
    	{
    		if (--milli == 0)
    		{
    			milli = 999;
    			if (--second == 0)
    			{
    				second = 59;
    				if (--minute == 0)
    				{
    					minute = 59;
    					hour--;
    				}
    			}
    		}
    	}
    	
    	private void init ()
    	{
    		hour   = 0; 
    		minute = 0; 
    		second = 0; 
    		milli  = 0;
    		running = false;
    		posetive = true;
    		stop = false;
    		direction = Direction.POSETIVE;
    	}
    	
    	private static class TimerThread extends Thread
    	{
    		int millisecond;
    		Clock clock;
    		
    		TimerThread (Clock clock) 
    		{
    			this.clock = clock;
    			millisecond = 0;
    		}
    
    		@Override
    		public void run () 
    		{
    			try 
    			{
    				Thread.sleep(1);
    			} 
    			catch (InterruptedException e) {}
    			millisecond++;
    			
    			if (clock.stop)
    				return;
    		}		
    	}
    }
    I have added a system.out.println debug at line:50, and it seems like the code is only executed once.
    I cant figure it out. Some help?

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

    Default Re: Stopwatch not working.

    Not an advanced topic. Moving to New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Stopwatch return wrong values
    By deadManN in forum New To Java
    Replies: 9
    Last Post: 12-22-2011, 01:38 PM
  2. Replies: 7
    Last Post: 09-20-2011, 05:21 PM
  3. Collisions are working & not working
    By Jayayoh in forum Java 2D
    Replies: 1
    Last Post: 06-24-2011, 05:21 PM
  4. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 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
  •