Blame view

VRGNYMusicLights/Sources/PatternJPanel.java 2.26 KB
278cdee0   pfrison   VRGNYMusicLights ...
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
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  
  import javax.swing.JPanel;
  
  public class PatternJPanel extends JPanel {
  	private static final long serialVersionUID = 1L;
  	private static final int HEIGHT = 50;
  	private static final int PADDING = 2;
  	public static final int ZOOM_MULTIPLIER = 10;
  	
  	private Music music;
  	private TimeLineJPanel timeLine;
  	private int end;
  
  	public PatternJPanel(Music music, TimeLineJPanel timeLine) {
  		this.music = music;
  		this.timeLine = timeLine;
  		end = transformZoon(music.getMusicPattern().getStop());
  		setPreferredSize(new Dimension((int) end, HEIGHT));
  	}
  	
  	public void refreshPreferedSize() {
  		end = transformZoon(music.getMusicPattern().getStop());
  		setPreferredSize(new Dimension((int) end, HEIGHT));
  	}
  	
  	public int getEnd() { return end; }
  	public Music getMusic() { return music; }
  	
  	@Override
  	protected void paintComponent(Graphics g) {
  		super.paintComponent(g);
  
  		g.setColor(Color.WHITE);
  		int begin = transformZoon(music.getMusicPattern().getBegin());
  		g.fillRect(begin, 0, end, HEIGHT);
  		
  		MusicPattern pattern = music.getMusicPattern();
  		g.setColor(Color.BLACK);
  		for(int i=0; i<pattern.getRepeat(); i++) {
  			int repeatAddition = transformZoon(pattern.getTotalLength() * i) + begin;
  			g.drawLine(repeatAddition, HEIGHT - PADDING,
  					transformZoon(pattern.getPatternTiming()[0]) + repeatAddition, HEIGHT - PADDING);
  			g.drawLine(transformZoon(pattern.getPatternTiming()[0]) + repeatAddition, HEIGHT - PADDING,
  					transformZoon(pattern.getPatternTiming()[1]) + repeatAddition, PADDING);
  			g.drawLine(transformZoon(pattern.getPatternTiming()[1]) + repeatAddition, PADDING,
  					transformZoon(pattern.getPatternTiming()[2]) + repeatAddition, PADDING);
  			g.drawLine(transformZoon(pattern.getPatternTiming()[2]) + repeatAddition, PADDING,
  					transformZoon(pattern.getPatternTiming()[3]) + repeatAddition, HEIGHT - PADDING);
  			g.drawLine(transformZoon(pattern.getPatternTiming()[3]) + repeatAddition, HEIGHT - PADDING, 
  					transformZoon(pattern.getPatternTiming()[4]) + repeatAddition, HEIGHT - PADDING);
  		}
  	}
  
  	private int transformZoon(int n) {
  		return transformZoon((long) n);
  	}
  	private int transformZoon(long n) {
  		return (int) ((double) n / (double) ZOOM_MULTIPLIER * timeLine.getZoomLevel());
  	}
  }