1 | package jpacman.controller; |
2 | |
3 | import java.awt.event.ActionEvent; |
4 | import java.awt.event.ActionListener; |
5 | |
6 | import javax.swing.Timer; |
7 | |
8 | /** |
9 | * The primary responsibility of this class is |
10 | * to trigger the board viewer to display the |
11 | * next animation. |
12 | * |
13 | * @author Arie van Deursen, 2007. |
14 | * @version $Id: Animator.java,v 1.5 2008/02/03 19:43:38 arie Exp $ |
15 | * |
16 | */ |
17 | public class Animator { |
18 | |
19 | /** |
20 | * The viewer that must be informed to show the |
21 | * next animation. |
22 | */ |
23 | private BoardViewer boardViewer; |
24 | |
25 | /** |
26 | * The timer used. |
27 | */ |
28 | private Timer timer; |
29 | |
30 | /** |
31 | * The delay between two animations. |
32 | */ |
33 | private static final int DELAY = 200; |
34 | |
35 | /** |
36 | * Create an animator for a particular board viewer. |
37 | * @param bv The view to be animated. |
38 | */ |
39 | public Animator(BoardViewer bv) { |
40 | boardViewer = bv; |
41 | timer = new Timer(DELAY, |
42 | new ActionListener() { |
43 | public void actionPerformed(ActionEvent e) { |
44 | boardViewer.nextAnimation(); |
45 | } |
46 | } |
47 | ); |
48 | } |
49 | |
50 | /** |
51 | * Stop triggering animation events. |
52 | */ |
53 | public void stop() { |
54 | timer.stop(); |
55 | } |
56 | |
57 | /** |
58 | * Start triggering animation events. |
59 | */ |
60 | public void start() { |
61 | timer.start(); |
62 | } |
63 | } |