LightLabel.java
2 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
64
65
66
67
68
69
70
package org.mote.wiimote.whiteboard.gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import org.uweschmidt.wiimote.whiteboard.ds.IRDot;
@SuppressWarnings("serial")
public class LightLabel extends JLabel {
private static final int RADIUS = 10;
private final int id, number;
private Point2D[][] lights;
private JPanel canvas;
public LightLabel(JPanel canvas, Point2D[][] lights, int id, int number) {
this(canvas, lights, id, number, WiimoteIcon.COLORS[id - 1]);
}
public LightLabel(JPanel canvas, Point2D[][] lights, int id, int number, Color bg) {
super(String.valueOf(number));
this.id = id;
this.number = number;
this.lights = lights;
this.canvas = canvas;
setHorizontalAlignment(SwingConstants.CENTER);
setForeground(Color.black);
setBackground(bg);
}
public void update() {
Point2D l = lights[id][number - 1];
if (l != null) {
int x = (int) Math.round(l.getX() * canvas.getWidth());
int y = canvas.getHeight() - (int) Math.round(l.getY() * canvas.getHeight());
this.setBounds(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
setVisible(true);
} else {
setVisible(false);
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (lights[id][number - 1] != null) {
g2d.setColor(this.getBackground());
g2d.fillOval(1, 1, RADIUS * 2 - 2, RADIUS * 2 - 2);
g2d.setColor(this.getForeground());
g2d.drawOval(1, 1, RADIUS * 2 - 2, RADIUS * 2 - 2);
double scale = lights[id][number - 1] instanceof IRDot ? ((IRDot)lights[id][number - 1]).getSize() : -1;
if (scale != -1) {
g2d.setColor(Color.cyan);
final int d = (int)Math.round(RADIUS * scale * 10);
g2d.fillOval(RADIUS - d/2, RADIUS - d/2, d, d);
}
}
super.paintComponent(g);
}
}