1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.awt.image.BufferedImage;
|
---|
5 | import java.util.ArrayList;
|
---|
6 |
|
---|
7 | public class Animation extends Member {
|
---|
8 |
|
---|
9 | public ArrayList<BufferedImage> frames;
|
---|
10 | public long drawInterval;
|
---|
11 | public boolean wrap;
|
---|
12 |
|
---|
13 | int currentFrame;
|
---|
14 | long lastFrameChange;
|
---|
15 | boolean reachedEnd;
|
---|
16 |
|
---|
17 | public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval, boolean wrap) {
|
---|
18 | super(newName, newX, newY, newWidth, newHeight);
|
---|
19 | this.frames = new ArrayList<BufferedImage>();
|
---|
20 | this.currentFrame = 0;
|
---|
21 | this.drawInterval = newInterval;
|
---|
22 | this.lastFrameChange = 0L;
|
---|
23 | this.reachedEnd = false;
|
---|
24 | this.wrap = wrap;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public Animation(Animation copy) {
|
---|
28 | super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight());
|
---|
29 | this.frames = copy.frames;
|
---|
30 | this.currentFrame = copy.currentFrame;
|
---|
31 | this.drawInterval = copy.drawInterval;
|
---|
32 | this.lastFrameChange = 0L;
|
---|
33 | this.reachedEnd = false;
|
---|
34 | this.wrap = copy.wrap;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void addFrame(BufferedImage newFrame) {
|
---|
38 | this.frames.add(newFrame);
|
---|
39 | setWidth(newFrame.getWidth());
|
---|
40 | setHeight(newFrame.getHeight());
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void draw(Graphics g) {
|
---|
44 | if (this.lastFrameChange == 0L)
|
---|
45 | this.lastFrameChange = System.currentTimeMillis();
|
---|
46 | if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) {
|
---|
47 | this.currentFrame++;
|
---|
48 | if (this.currentFrame >= this.frames.size()) {
|
---|
49 | if (this.wrap) {
|
---|
50 | this.currentFrame = 0;
|
---|
51 | } else {
|
---|
52 | this.currentFrame--;
|
---|
53 | }
|
---|
54 | this.reachedEnd = true;
|
---|
55 | }
|
---|
56 | this.lastFrameChange = System.currentTimeMillis();
|
---|
57 | }
|
---|
58 | g.drawImage(this.frames.get(this.currentFrame), getX(), getY(), null);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void draw(Graphics g, int x, int y) {
|
---|
62 | if (this.lastFrameChange == 0L)
|
---|
63 | this.lastFrameChange = System.currentTimeMillis();
|
---|
64 | if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) {
|
---|
65 | this.currentFrame++;
|
---|
66 | if (this.currentFrame >= this.frames.size()) {
|
---|
67 | if (this.wrap) {
|
---|
68 | this.currentFrame = 0;
|
---|
69 | } else {
|
---|
70 | this.currentFrame--;
|
---|
71 | }
|
---|
72 | this.reachedEnd = true;
|
---|
73 | }
|
---|
74 | this.lastFrameChange = System.currentTimeMillis();
|
---|
75 | }
|
---|
76 | g.drawImage(this.frames.get(this.currentFrame), getX() + x - ((BufferedImage)this.frames.get(this.currentFrame)).getWidth() / 2, getY() + y - ((BufferedImage)this.frames.get(this.currentFrame)).getHeight(), null);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public boolean reachedEnd() {
|
---|
80 | return this.reachedEnd;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void reset() {
|
---|
84 | this.reachedEnd = false;
|
---|
85 | this.currentFrame = 0;
|
---|
86 | this.lastFrameChange = 0L;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public int getWidth() {
|
---|
90 | return ((BufferedImage)this.frames.get(this.currentFrame)).getWidth();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public int getHeight() {
|
---|
94 | return ((BufferedImage)this.frames.get(this.currentFrame)).getHeight();
|
---|
95 | }
|
---|
96 | }
|
---|