svn-gvsig-desktop / trunk / libraries / libAnimation / src / com / iver / cit / gvsig / animation / timer / AnimationTimer.java @ 17542
History | View | Annotate | Download (1.54 KB)
1 |
package com.iver.cit.gvsig.animation.timer; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
// this class is only an example. it isn?t used in librarys
|
11 |
public class AnimationTimer implements Runnable { |
12 |
double milis;
|
13 |
private boolean finish; |
14 |
private List callBackList; |
15 |
private Thread thread; |
16 |
|
17 |
|
18 |
/**
|
19 |
* Lanza un timer cada segundo.
|
20 |
*/
|
21 |
public AnimationTimer()
|
22 |
{ |
23 |
|
24 |
} |
25 |
|
26 |
public void run() { |
27 |
if (isAlive()) {
|
28 |
while (true) { |
29 |
try {
|
30 |
Thread.sleep((long) milis); |
31 |
synchronized (this) { |
32 |
if (finish) {
|
33 |
break;
|
34 |
} |
35 |
} |
36 |
} catch (InterruptedException e) { |
37 |
|
38 |
e.printStackTrace(); |
39 |
} |
40 |
// Updating
|
41 |
Update(); |
42 |
} |
43 |
} |
44 |
} |
45 |
|
46 |
public synchronized void end() { |
47 |
finish = true;
|
48 |
thread.stop(); |
49 |
} |
50 |
|
51 |
|
52 |
private void Update() { |
53 |
// TODO Auto-generated method stub
|
54 |
for (int i = 0; i < callBackList.size(); i++) { |
55 |
IUpdateCallBack element = (IUpdateCallBack) callBackList.get(i); |
56 |
element.update(); |
57 |
} |
58 |
|
59 |
} |
60 |
|
61 |
public void start(double milis) { |
62 |
this.milis = milis;
|
63 |
|
64 |
// Create the thread supplying it with the runnable object
|
65 |
thread = new Thread(this); |
66 |
|
67 |
// Start the thread
|
68 |
thread.start(); |
69 |
} |
70 |
|
71 |
public void addCallBackObject(IUpdateCallBack object) { |
72 |
if (callBackList == null) |
73 |
callBackList = new ArrayList(); |
74 |
if (!callBackList.contains(object)) {
|
75 |
callBackList.add(object); |
76 |
} |
77 |
} |
78 |
|
79 |
public boolean isAlive() { |
80 |
if (thread != null) |
81 |
return thread.isAlive();
|
82 |
else
|
83 |
return false; |
84 |
} |
85 |
|
86 |
|
87 |
} |