-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseRunnable.java
More file actions
83 lines (76 loc) · 1.44 KB
/
Copy pathUseRunnable.java
File metadata and controls
83 lines (76 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.awt.*;
import java.applet.Applet;
public class UseRunnable extends Applet implements Runnable
{
Thread t;
Image imgs[];
int high,h1,h2,h3;
public void init()
{
high = getHeight()/3;
h1 = high;
h2 = high * 2;
h3 = high * 3;
imgs = new Image[3];
for(int i = 0; i < 3; i++)
{
imgs[i] = getImage(getDocumentBase(), "image" + (i+1) +".gif");
}
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stop()
{
t = null;
}
// 实现接口的run方法, 获得动画效果
public void run()
{
while(t != null)
{
try {
Thread.sleep(100);
repaint();
h1--;
// 上移,到顶点时睡眠
if ( h1 == 0)
{
Thread.sleep(3000);
h2 = high;
}
// 上移,到顶点时睡眠
h2--;
if (h2 == 0)
{
Thread.sleep(3000);
h3 = high;
}
// 上移,到顶点时睡眠
h3--;
if (h3 == 0)
{
Thread.sleep(3000);
h1 = high;
}
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
public void paint(Graphics g)
{
// 三幅图片依次显示
g.drawImage(imgs[0], 0, h1, this);
g.drawImage(imgs[1], 0, h2, this);
g.drawImage(imgs[2], 0, h3, this);
}
public void update(Graphics g)
{
paint(g);
}
}