AnimationPlayer.java 2.95 KB
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 LightCanvasJPanel lightCanvas;
	private MusicList musicList;
	
	private JButton backward;
	private JButton stop;
	private JButton play;
	private JButton pause;
	private JButton forward;
	
	private long tick = 0;
	private boolean go = false;
	private boolean stopClock = false;
	private static final int TICK_JUMP = 20;
	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();
				lightCanvas.paintLights(musicList.render(tick));
				tick += TICK_JUMP;
				long after = System.currentTimeMillis();
				
				if(after - before < TICK_JUMP) {
					try { Thread.sleep(10 - after + before);}
					catch (InterruptedException ignored) {}
				} else if(after - before > TICK_JUMP) {
					System.err.println("Animation were slowed down by " + String.valueOf(after - before - 10) + " ticks !");
				}
			}
		}
	});
	public void stopTickClock() {stopClock = true;}

	public AnimationPlayer(LightCanvasJPanel lightCanvas, MusicList musicList) { this(lightCanvas, musicList, BoxLayout.LINE_AXIS); }
	public AnimationPlayer(LightCanvasJPanel lightCanvas, MusicList musicList, int direction) {
		this.lightCanvas = lightCanvas;
		this.musicList = musicList;
		
		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) {
		// TODO player actions
		if(e.getSource() == backward) {
			tick -= 500;
			if(tick < 0)
				tick = 0;
		}else if(e.getSource() == stop) {
			go = false;
			tick = 0;
		}else if(e.getSource() == play) {
			go = true;
		}else if(e.getSource() == pause) {
			go = false;
		}else if(e.getSource() == forward) {
			tick += 500;
		}
	}
}