home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-22 | 1.1 KB | 62 lines |
- import java.applet.*;
- import java.awt.*;
-
- public class EX12A extends Applet implements Runnable
- {
- protected Thread CounterThread;
- protected Label CountLabel;
- protected int count = 10;
-
- public void init()
- {
-
- // provide something to look at
- add(new Label("Count:"));
- CountLabel = new Label(Integer.toString(count));
- add(CountLabel);
- }
-
- public void start()
- {
- if (CounterThread == null) {
- // allocate the thread, using the local run method
- CounterThread = new Thread(this);
- CounterThread.start();
- }
- }
-
- public void stop()
- {
- if (CounterThread != null) {
- // stop the thread
- CounterThread.stop();
- CounterThread = null;
-
- }
- }
-
- public void run()
- {
- while (true) {
- try {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {}
-
- CountDown();
- }
- }
-
- protected void CountDown()
- {
- count--;
-
- CountLabel.setText(Integer.toString(count));
-
- if (count == 0)
- // reset to 11 since the first call will move it back
- // down to 10 and then display
- count = 11;
- }
- }
-