-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStripAnimation.java
More file actions
84 lines (57 loc) · 1.81 KB
/
StripAnimation.java
File metadata and controls
84 lines (57 loc) · 1.81 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
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
The StripAnimation class creates an animation from a strip file.
*/
public class StripAnimation {
Animation animation;
private int x; // x position of animation
private int y; // y position of animation
private int width;
private int height;
private int dx; // increment to move along x-axis
private int dy; // increment to move along y-axis
public StripAnimation(String name, int pieces, boolean loop) {
animation = new Animation(loop); // run animation once
dx = 0; // increment to move along x-axis
dy = 0; // increment to move along y-axis
// load images from strip file
Image stripImage = ImageManager.loadImage(name);
int imageWidth = (int) stripImage.getWidth(null) / pieces;
int imageHeight = stripImage.getHeight(null);
for (int i=0; i<pieces; i++) {
BufferedImage frameImage = new BufferedImage (imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) frameImage.getGraphics();
g.drawImage(stripImage,
0, 0, imageWidth, imageHeight,
i*imageWidth, 0, (i*imageWidth)+imageWidth, imageHeight,
null);
animation.addFrame(frameImage, 100);
}
}
public void start(int xPos, int yPos) {
x = xPos;
y = yPos;
animation.start();
}
public void stop(){
animation.stop();
}
public void update() {
if (!animation.isStillActive())
return;
animation.update();
x = x + dx;
y = y + dy;
}
public void draw(Graphics2D g2, double cameraX, double cameraY) {
int drawX = (int)(x - cameraX);
int drawY = (int)(y - cameraY);
if (!animation.isStillActive())
return;
g2.drawImage(animation.getImage(), drawX, drawY, 70, 50, null);
}
}