Blame view

VRGNYMusicLights/Sources/LightCanvasJPanel.java 1.92 KB
742429d1   pfrison   VRGNYMusicLight b...
1
2
3
4
5
6
7
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  
  import javax.swing.JPanel;
  
  public class LightCanvasJPanel extends JPanel {
742429d1   pfrison   VRGNYMusicLight b...
8
9
10
11
12
13
  	private static final long serialVersionUID = 1L;
  	private static final int WIDTH = 320; // = 300 + padding * 2
  	private static final int HEIGHT = 220; // = 200 + padding * 2
  	private static final int LIGHT_RADIUS = 4;
  	private static final int PADDING = 10;
  	private double[][] lightsCoords;
a89c030d   pfrison   VRGNYMusicLights ...
14
  	private double[] lights = null;
742429d1   pfrison   VRGNYMusicLight b...
15
  	
a89c030d   pfrison   VRGNYMusicLights ...
16
  	public LightCanvasJPanel() { this(MusicPath.LIGHT_COORDS); }
742429d1   pfrison   VRGNYMusicLight b...
17
18
19
20
21
  	public LightCanvasJPanel(double[][] lightsCoords) {
  		this.lightsCoords = lightsCoords;
  		setPreferredSize(new Dimension(WIDTH, HEIGHT));
  	}
  	
a89c030d   pfrison   VRGNYMusicLights ...
22
23
24
25
26
  	public void paintLights(double[] lights) {
  		this.lights = lights;
  		repaint();
  	}
  	
742429d1   pfrison   VRGNYMusicLight b...
27
28
  	@Override
  	protected void paintComponent(Graphics g) {
a89c030d   pfrison   VRGNYMusicLights ...
29
30
  		super.paintComponent(g);
  		
742429d1   pfrison   VRGNYMusicLight b...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  		// background
  		g.setColor(Color.WHITE);
  		g.fillRect(0, 0, WIDTH, HEIGHT);
  		
  		// lights
  		if(lightsCoords == null) return;
  		g.setColor(Color.BLACK);
  		for(double[] light : lightsCoords) {
  			if(light.length != 2)
  				throw new MalformedCoordinates();
  			int centerX = (int) (light[0] * (WIDTH - PADDING * 2)) + PADDING;
  			int centerY = (int) (light[1] * (HEIGHT - PADDING * 2)) + PADDING;
  			
  			g.fillOval(centerX - LIGHT_RADIUS, centerY - LIGHT_RADIUS, LIGHT_RADIUS * 2, LIGHT_RADIUS * 2);
  		}
a89c030d   pfrison   VRGNYMusicLights ...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  		
  		// lights values
  		if(lights == null || lights.length != lightsCoords.length)
  			return;
  		g.setColor(Color.YELLOW);
  		for(int i=0; i<lights.length; i++) {
  			if(lightsCoords[i].length != 2)
  				throw new MalformedCoordinates();
  			int centerX = (int) (lightsCoords[i][0] * (WIDTH - PADDING * 2)) + PADDING;
  			int centerY = (int) (lightsCoords[i][1] * (HEIGHT - PADDING * 2)) + PADDING;
  			
  			g.fillOval((int) (centerX - (LIGHT_RADIUS * lights[i])),
  					(int) (centerY - (LIGHT_RADIUS * lights[i])),
  					(int) (LIGHT_RADIUS * lights[i] * 2),
  					(int) (LIGHT_RADIUS * lights[i] * 2));
  		}
742429d1   pfrison   VRGNYMusicLight b...
62
63
  	}
  }