Blame view

VRGNYMusicLights/Sources/AnimationPlayer.java 2.84 KB
742429d1   pfrison   VRGNYMusicLight b...
1
2
3
4
5
6
7
8
9
10
11
12
  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;
  
278cdee0   pfrison   VRGNYMusicLights ...
13
  	private Interface interf;
a89c030d   pfrison   VRGNYMusicLights ...
14
  	
742429d1   pfrison   VRGNYMusicLight b...
15
16
17
18
19
  	private JButton backward;
  	private JButton stop;
  	private JButton play;
  	private JButton pause;
  	private JButton forward;
a89c030d   pfrison   VRGNYMusicLights ...
20
  	
a89c030d   pfrison   VRGNYMusicLights ...
21
22
  	private boolean go = false;
  	private boolean stopClock = false;
278cdee0   pfrison   VRGNYMusicLights ...
23
  	private static final int TICK_JUMP = 10;
a89c030d   pfrison   VRGNYMusicLights ...
24
25
26
27
28
29
30
31
32
33
34
  	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();
278cdee0   pfrison   VRGNYMusicLights ...
35
36
  				interf.computeTick();
  				interf.tick += TICK_JUMP;
a89c030d   pfrison   VRGNYMusicLights ...
37
38
39
  				long after = System.currentTimeMillis();
  				
  				if(after - before < TICK_JUMP) {
278cdee0   pfrison   VRGNYMusicLights ...
40
  					try { Thread.sleep(TICK_JUMP - after + before);}
a89c030d   pfrison   VRGNYMusicLights ...
41
42
  					catch (InterruptedException ignored) {}
  				} else if(after - before > TICK_JUMP) {
278cdee0   pfrison   VRGNYMusicLights ...
43
  					System.err.println("Animation were slowed down by " + String.valueOf(after - before - TICK_JUMP) + " ticks !");
a89c030d   pfrison   VRGNYMusicLights ...
44
45
46
47
48
  				}
  			}
  		}
  	});
  	public void stopTickClock() {stopClock = true;}
742429d1   pfrison   VRGNYMusicLight b...
49
  
278cdee0   pfrison   VRGNYMusicLights ...
50
51
52
  	public AnimationPlayer(Interface interf) { this(interf, BoxLayout.LINE_AXIS); }
  	public AnimationPlayer(Interface interf, int direction) {
  		this.interf = interf;
a89c030d   pfrison   VRGNYMusicLights ...
53
  		
742429d1   pfrison   VRGNYMusicLight b...
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
  		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());
a89c030d   pfrison   VRGNYMusicLights ...
84
85
  		
  		tickClock.start();
742429d1   pfrison   VRGNYMusicLight b...
86
87
88
89
  	}
  	
  	@Override
  	public void actionPerformed(ActionEvent e) {
742429d1   pfrison   VRGNYMusicLight b...
90
  		if(e.getSource() == backward) {
278cdee0   pfrison   VRGNYMusicLights ...
91
92
93
94
  			interf.tick -= 100;
  			if(interf.tick < 0)
  				interf.tick = 0;
  			interf.computeTick();
742429d1   pfrison   VRGNYMusicLight b...
95
  		}else if(e.getSource() == stop) {
a89c030d   pfrison   VRGNYMusicLights ...
96
  			go = false;
278cdee0   pfrison   VRGNYMusicLights ...
97
98
  			interf.tick = 0;
  			interf.computeTick();
742429d1   pfrison   VRGNYMusicLight b...
99
  		}else if(e.getSource() == play) {
a89c030d   pfrison   VRGNYMusicLights ...
100
  			go = true;
742429d1   pfrison   VRGNYMusicLight b...
101
  		}else if(e.getSource() == pause) {
a89c030d   pfrison   VRGNYMusicLights ...
102
  			go = false;
742429d1   pfrison   VRGNYMusicLight b...
103
  		}else if(e.getSource() == forward) {
278cdee0   pfrison   VRGNYMusicLights ...
104
105
  			interf.tick += 100;
  			interf.computeTick();
742429d1   pfrison   VRGNYMusicLight b...
106
107
108
  		}
  	}
  }