-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.java
More file actions
138 lines (106 loc) · 3.63 KB
/
Animation.java
File metadata and controls
138 lines (106 loc) · 3.63 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.awt.Image;
import java.util.ArrayList;
/**
The Animation class manages a series of images (frames) and
the amount of time to display each frame.
*/
public class Animation {
private GamePanel panel; // JPanel on which animation is being displayed
private ArrayList<AnimFrame> frames; // collection of frames for animation
private int currFrameIndex; // current frame being displayed
private long animTime; // time that the animation has run for already
private long startTime; // start time of the animation or time since last update
private long totalDuration; // total duration of the animation
private boolean loop;
private boolean isActive;
/**
Creates a new, empty Animation.
*/
public Animation(boolean loop) {
frames = new ArrayList<AnimFrame>();
totalDuration = 0;
this.loop = loop;
isActive = false;
}
/**
Adds an image to the animation with the specified
duration (time to display the image).
*/
public synchronized void addFrame(Image image, long duration)
{
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}
/**
Starts this animation over from the beginning.
*/
public synchronized void start() {
isActive = true;
animTime = 0; // reset time animation has run for to zero
currFrameIndex = 0; // reset current frame to first frame
startTime = System.currentTimeMillis(); // reset start time to current time
}
/**
Terminates this animation.
*/
public synchronized void stop() {
isActive = false;
}
/**
Updates this animation's current image (frame), if
neccesary.
*/
public synchronized void update() {
if (!isActive)
return;
long currTime = System.currentTimeMillis(); // find the current time
long elapsedTime = currTime - startTime; // find how much time has elapsed since last update
startTime = currTime; // set start time to current time
if (frames.size() > 1) {
animTime += elapsedTime; // add elapsed time to amount of time animation has run for
if (animTime >= totalDuration) { // if the time animation has run for > total duration
if (loop) {
animTime = animTime % totalDuration; // reset time animation has run for
currFrameIndex = 0; // reset current frame to first frame
}
else {
isActive = false; // set to false to terminate animation
}
}
if (!isActive)
return;
while (animTime > getFrame(currFrameIndex).endTime) {
currFrameIndex++; // set frame corresponding to time animation has run for
}
}
}
/**
Gets this Animation's current image. Returns null if this
animation has no images.
*/
public synchronized Image getImage() {
if (frames.size() == 0) {
return null;
}
else {
return getFrame(currFrameIndex).image;
}
}
public int getNumFrames() { // find out how many frames in animation
return frames.size();
}
private AnimFrame getFrame(int i) { // returns ith frame in the collection
return frames.get(i);
}
public boolean isStillActive () {
return isActive;
}
private class AnimFrame { // inner class for the frames of the animation
Image image;
long endTime;
public AnimFrame(Image image, long endTime) {
this.image = image;
this.endTime = endTime;
}
}
}