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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package gjt;
import java.awt.*;
/**
* A selectable label. Clients can set the insets around the
* label via setInsets(Insets).
*
* LabelCanvases generate SelectionEvents when they are
* selected or deselected.<p>
*
* @version 1.0, Apr 1 1996
* @author David Geary
* @see SelectionEvent
* @see gjt.test.LabelCanvasTest
*/
public class LabelCanvas extends Canvas {
private String label;
private boolean selected = false;
private Insets insets = new Insets(2,2,2,2);
private Point labelLoc = new Point(0,0);
public LabelCanvas(String label) {
this.label = label;
}
public void paint(Graphics g) {
if(selected == true) paintSelected(g);
else
g.drawString(label, labelLoc.x, labelLoc.y);
}
public void setInsets(Insets insets) {
this.insets = insets;
repaint();
}
public String getLabel () { return label; }
public boolean isSelected() { return selected; }
public void select () { selected = true; repaint(); }
public void deselect () { selected = false; repaint(); }
public void resize(int w, int h) {
reshape(location().x, location().y, w, h);
}
public void reshape(int x, int y, int w, int h) {
super.reshape(x, y, w, h);
labelLoc = labelLocation(getGraphics());
}
public Dimension minimumSize() {
return preferredSize();
}
public Dimension preferredSize() {
FontMetrics fm = getFontMetrics(getFont());
return new Dimension(
insets.left + fm.stringWidth(label) +
insets.right,
insets.top + fm.getHeight() +
insets.bottom);
}
public boolean mouseDown(Event event, int x, int y) {
if(selected) deselect();
else select ();
int eventType = isSelected() ?
SelectionEvent.SELECT :
SelectionEvent.DESELECT;
Event newEvent = new SelectionEvent(this,
event,
eventType);
deliverEvent(newEvent);
return true;
}
protected void paintSelected(Graphics g) {
Point labelLoc = labelLocation(g);
g.setColor(getForeground());
g.fillRect(0,0,size().width,size().height);
g.setColor(getBackground());
g.drawString(label, labelLoc.x, labelLoc.y);
}
protected String paramString() {
return super.paramString() + ",text=" + label;
}
private Point labelLocation(Graphics g) {
Dimension size = size();
FontMetrics fm = g.getFontMetrics();
int x = (size.width/2) - (fm.stringWidth(label)/2);
int y = (size.height/2) + (fm.getAscent()/2) -
fm.getLeading();
return new Point(x,y);
}
}
|