LightCanvasJPanel.java
1.92 KB
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 LightCanvasJPanel extends JPanel {
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;
private double[] lights = null;
public LightCanvasJPanel() { this(MusicPath.LIGHT_COORDS); }
public LightCanvasJPanel(double[][] lightsCoords) {
this.lightsCoords = lightsCoords;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
public void paintLights(double[] lights) {
this.lights = lights;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 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);
}
// 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));
}
}
}