AnimationPlayer.java
2.84 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class AnimationPlayer extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private Interface interf;
private JButton backward;
private JButton stop;
private JButton play;
private JButton pause;
private JButton forward;
private boolean go = false;
private boolean stopClock = false;
private static final int TICK_JUMP = 10;
private Thread tickClock = new Thread(new Runnable() {
@Override
public void run() {
while(!stopClock) {
if(!go) { // pause
try { Thread.sleep(100); }
catch (InterruptedException ignored) {}
continue;
}
long before = System.currentTimeMillis();
interf.computeTick();
interf.tick += TICK_JUMP;
long after = System.currentTimeMillis();
if(after - before < TICK_JUMP) {
try { Thread.sleep(TICK_JUMP - after + before);}
catch (InterruptedException ignored) {}
} else if(after - before > TICK_JUMP) {
System.err.println("Animation were slowed down by " + String.valueOf(after - before - TICK_JUMP) + " ticks !");
}
}
}
});
public void stopTickClock() {stopClock = true;}
public AnimationPlayer(Interface interf) { this(interf, BoxLayout.LINE_AXIS); }
public AnimationPlayer(Interface interf, int direction) {
this.interf = interf;
setLayout(new BoxLayout(this, direction));
add(Box.createHorizontalGlue());
// backward button
backward = new JButton(new ImageIcon(getClass().getResource("/icons/backward.png")));
backward.addActionListener(this);
add(backward);
// stop button
stop = new JButton(new ImageIcon(getClass().getResource("/icons/stop.png")));
stop.addActionListener(this);
add(stop);
// play button
play = new JButton(new ImageIcon(getClass().getResource("/icons/play.png")));
play.addActionListener(this);
add(play);
// pause button
pause = new JButton(new ImageIcon(getClass().getResource("/icons/pause.png")));
pause.addActionListener(this);
add(pause);
// forward button
forward = new JButton(new ImageIcon(getClass().getResource("/icons/forward.png")));
forward.addActionListener(this);
add(forward);
add(Box.createHorizontalGlue());
tickClock.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == backward) {
interf.tick -= 100;
if(interf.tick < 0)
interf.tick = 0;
interf.computeTick();
}else if(e.getSource() == stop) {
go = false;
interf.tick = 0;
interf.computeTick();
}else if(e.getSource() == play) {
go = true;
}else if(e.getSource() == pause) {
go = false;
}else if(e.getSource() == forward) {
interf.tick += 100;
interf.computeTick();
}
}
}