-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImagePanel.java
57 lines (48 loc) · 1.2 KB
/
ImagePanel.java
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
package chess;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
/**
* A JPanel for displaying a BufferedImage.
*
* @author David Johnson
*/
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private ArrayList<BufferedImage> images;
private int frameNumber;
/***
* Construct a panel with a starter images
* @param img
*/
public ImagePanel(BufferedImage img) {
images = new ArrayList<BufferedImage>();
images.add(img);
frameNumber = 0;
setPreferredSize(new Dimension(256, 256));
}
/***
* Add img to the ArrayList of images
* @param img
*/
public void add(BufferedImage img) {
images.add(img);
}
/***
* Cycle through the frames using the % mod operation
*/
public void nextFrame() {
frameNumber = (frameNumber + 1)%images.size();
}
/***
* Implement the paint method to draw the images
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(images.get(frameNumber), 0, 0, null);
// System.out.println("redraw " + frameNumber);
}
}