-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.java
More file actions
68 lines (59 loc) · 1.36 KB
/
Animation.java
File metadata and controls
68 lines (59 loc) · 1.36 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
package start;
import java.awt.Image;
import java.util.ArrayList;;
public class Animation {
private ArrayList scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
//CONSTRUCTOR
public Animation() {
scenes = new ArrayList();
totalTime = 0;
start();
}
// add scene to ArrayList and set time for each scene
public synchronized void addScene(Image i, long t) {
totalTime += t;
scenes.add(new OneScene(i, totalTime));
}
//start animation from beginning
public synchronized void start() {
movieTime = 0;
sceneIndex = 0;
}
//change scenes
public synchronized void update(long timePassed) {
if(scenes.size()>1) {
movieTime += timePassed;
if(movieTime >= totalTime) {
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime) {
sceneIndex++;
}
}
}
//get animations current scene (aka image)
public synchronized Image getImage() {
if(scenes.size()==0) {
return null;
}else {
return getScene(sceneIndex).pic;
}
}
// get scene
private OneScene getScene(int x) {
return (OneScene)scenes.get(x);
}
//////// PRIVATE INNER CLASS////////
private class OneScene {
Image pic;
long endTime;
public OneScene(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
}