-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageViewSprite.java
More file actions
65 lines (51 loc) · 1.96 KB
/
ImageViewSprite.java
File metadata and controls
65 lines (51 loc) · 1.96 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
package atChat;
import javafx.animation.AnimationTimer;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ImageViewSprite extends AnimationTimer {
private final ImageView imageView;
private final int totalFrames;
private final float fps;
private final int cols;
private final int rows;
private final int frameWidth;
private final int frameHeight;
private int currentCol = 0;
private int currentRow = 0;
private long lastFrame = 0;
public ImageViewSprite(ImageView imageView, Image image, int columns, int rows, int totalFrames, int frameWidth, int frameHeight, float framesPerSecond) {
this.imageView = imageView;
imageView.setImage(image);
imageView.setViewport(new Rectangle2D(0, 0, frameWidth, frameHeight));
cols = columns;
this.rows = rows;
this.totalFrames = totalFrames;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
fps = framesPerSecond;
lastFrame = System.nanoTime();
}
@Override
public void handle(long now) {
int frameJump = (int) Math.floor((now - lastFrame) / (1000000000 / fps));
if (frameJump >= 1) {
lastFrame = now;
int addRows = (int) Math.floor((float) frameJump / (float) cols);
int frameAdd = frameJump - (addRows * cols);
if (currentCol + frameAdd >= cols) {
currentRow += addRows + 1;
currentCol = frameAdd - (cols - currentCol);
} else {
currentRow += addRows;
currentCol += frameAdd;
}
currentRow = (currentRow >= rows) ? currentRow - ((int) Math.floor((float) currentRow / rows) * rows) : currentRow;
if ((currentRow * cols) + currentCol >= totalFrames) {
currentRow = 0;
currentCol = Math.abs(currentCol - (totalFrames - (int) (Math.floor((float) totalFrames / cols) * cols)));
}
imageView.setViewport(new Rectangle2D(currentCol * frameWidth, currentRow * frameHeight, frameWidth, frameHeight));
}
}
}