import java.awt.AWTEventMulticaster; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class cootimer implements Runnable { Thread worker = null; private long time = 0; private boolean m_timing = false; private long timingInterval = 1000; private ActionListener actionListener = null; public boolean isRunTimer( ) { return m_timing; } public void setRunTimer( boolean t ) { m_timing = t; if(m_timing) { if (worker != null) { worker.stop( ); worker = null; } worker = new Thread( this ); worker.start( ); //System.err.println("In Timer: after start worker=" +worker); time = System.currentTimeMillis( ); time += timingInterval; } else { if(worker != null) { worker.stop( ); worker = null; } } } public long getTimingInterval( ) { return timingInterval; } public void setTimingInterval( long i ) { if( i >= 100 ) timingInterval = i; } public void run( ) { while ( true ) { long currentTime = System.currentTimeMillis( ); //System.err.println("In Timer run: after start currentTime=" + currentTime + " time=" + time); if( currentTime >= time ) { time += timingInterval; processActionEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null ) ); } try { Thread.sleep( timingInterval/10 ); } catch ( InterruptedException e ) { break; } } } public synchronized void addActionListener( ActionListener l ) { actionListener = AWTEventMulticaster.add( actionListener, l ); } public synchronized void removeActionListener( ActionListener l ) { actionListener = AWTEventMulticaster.remove( actionListener, l ); } protected void processActionEvent( ActionEvent e ) { if ( actionListener != null ) actionListener.actionPerformed( e ); } }