-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation4Thread.java
More file actions
91 lines (78 loc) · 2.32 KB
/
Animation4Thread.java
File metadata and controls
91 lines (78 loc) · 2.32 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Animation4Thread extends JFrame {
final int frameCount = 10;
BufferedImage[] pics;
int xloc = 100;
int yloc = 100;
final int xIncr = 1;
final int yIncr = 1;
final int picSize = 165;
final int frameStartSize = 800;
final int drawDelay = 30; //msec
DrawPanel drawPanel = new DrawPanel();
Action drawAction;
public Animation4Thread() {
drawAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
drawPanel.repaint();
}
};
add(drawPanel);
BufferedImage img = createImage();
pics = new BufferedImage[frameCount];//get this dynamically
for(int i = 0; i < frameCount; i++)
pics[i] = img.getSubimage(picSize*i, 0, picSize, picSize);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(frameStartSize, frameStartSize);
setVisible(true);
pack();
}
@SuppressWarnings("serial")
private class DrawPanel extends JPanel {
int picNum = 0;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.gray);
setBackground(Color.gray);
picNum = (picNum + 1) % frameCount;
g.drawImage(pics[picNum], xloc+=xIncr, yloc+=yIncr, Color.gray, this);
}
public Dimension getPreferredSize() {
return new Dimension(frameStartSize, frameStartSize);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
Animation4Thread a = new Animation4Thread();
Timer t = new Timer(a.drawDelay, a.drawAction);
t.start();
}
});
}
//Read image from file and return
private BufferedImage createImage(){
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new File("images/orc/orc_forward_southeast.png"));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}