Embed
Email

Timer

Document Sample

Categories
Tags
Stats
views:
2
posted:
10/24/2011
language:
English
pages:
1
The Timer Class



When a new timer is created, specify a time t in seconds. You must also activate the timer, providing an

ActionListener as input via the addActionListener method, and define an object obj via the

setActionObject method. Then the timer can be started. If started, it will fire an action event every t seconds

whose source appears to be obj. To stop the timer, call its stop method.



import java.awt.*;

import java.awt.event.*;



public class Timer implements Runnable

{

private ActionListener listener = null;

private Object object = null;

private Thread thread = null;

private int timer = 0;

private int time = 5;



public Timer(int time)

{ this.time = time;

}

public void setActionObject(Object object)

{ this.object = object;

}



public void addActionListener(ActionListener listener)

{ this.listener = AWTEventMulticaster.add(this.listener, listener);

}



public void start()

{

thread = null;

if ((listener != null) && (object != null))

{

thread = new Thread(this);

thread.start();

}

}

public void stop()

{ thread = null;

}



public void run()

{ Thread currentThread = Thread.currentThread();

while ((currentThread == thread) && (timer = time * 1000)

{

ActionEvent e = new ActionEvent(object,

ActionEvent.ACTION_PERFORMED,

"time up");

listener.actionPerformed(e);

timer = 0;

}

}

}

}



Related docs
Other docs by Stariya Js @ B...
reflection on audacity
Views: 1  |  Downloads: 0
12FFIS
Views: 0  |  Downloads: 0
Sujatha_QA
Views: 3  |  Downloads: 0
ch20
Views: 0  |  Downloads: 0
jeux_pedagogiques1
Views: 5  |  Downloads: 0
ManuEpidemics
Views: 0  |  Downloads: 0
Lab_2_MSWO_data_sheet
Views: 1  |  Downloads: 0
07-grand_prix_f1
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!