1import java.applet.Applet;
2import java.awt.*;
3import java.net.*;
4
5
6
7public
8class ImageAnimation
9 extends Applet
10 implements Runnable
11{
12
13
14
15 Image [] images;
16 int currImage = 0;
17 int direction = +1;
18 int delay = 500;
19
20
21
22 public
23 void init() {
24
25
26
27 int noOfImages = Integer.parseInt(getParameter("noOfImages"));
28 URL documentBase = getDocumentBase();
29 String imageName = getParameter("image");
30
31 try {
32 delay = Integer.parseInt(getParameter("delay"));
33 }
34 catch (Exception e) { }
35
36
37
38 showStatus(noOfImages + " to load");
39
40 images = new Image[noOfImages];
41 for (int i = 0;
42 i < noOfImages;
43 ++i) {
44 String n = imageName + i + ".gif";
45
46 showStatus("loading image " + imageName);
47 images[i] = getImage(documentBase,n);
48 }
49 }
50
51
52
53 public
54 void destroy() {
55
56
57 for (int i = 0;
58 i < images.length;
59 ++i) {
60 images[i].flush();
61 }
62 }
63
64
65
66 public
67 void paint(Graphics g) {
68 showStatus("drawing image " + currImage);
69 g.drawImage(images[currImage],0,0,this);
70 }
71
72 public
73 void update(Graphics g) {
74 paint(g);
75 }
76
77
78
79
80 Thread animatorThread;
81
82 public
83 void start() {
84
85
86
87 if ( animatorThread == null ) {
88 animatorThread = new Thread(this);
89 }
90 animatorThread.start();
91 }
92
93
94
95 public
96 void stop() {
97
98 animatorThread = null;
99 }
100
101
102
103
104 public
105 void run() {
106
107 while (Thread.currentThread() == animatorThread) {
108 currImage += direction;
109 if (currImage == images.length || currImage == -1) {
110 direction = -direction;
111 currImage += 2 * direction;
112 }
113
114 repaint();
115 sleepAWhile();
116 }
117 }
118
119
120
121
122 protected
123 void sleepAWhile() {
124 try {
125 animatorThread.sleep(delay);
126 }
127 catch (InterruptedException e) {
128 }
129 }
130}
131
132