forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.java
More file actions
85 lines (81 loc) · 2.07 KB
/
Copy pathButton.java
File metadata and controls
85 lines (81 loc) · 2.07 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package javaforce.ui;
/** Button
*
* @author pquiring
*/
import javaforce.*;
public class Button extends TextComponent {
private ActionListener action;
private Image icon;
public Button(String text) {
super(false);
setText(text);
setFocusable(true);
}
public Button(Image image) {
super(false);
setImage(image);
setFocusable(true);
}
public Dimension getMinSize() {
if (icon != null) {
return new Dimension(icon.getWidth() + 8, icon.getHeight() + 8);
}
FontMetrics metrics = getFont().getMetrics(getText());
return new Dimension(metrics.getWidth() + 8, metrics.getHeight() + 8);
}
protected void renderText(Image image, int dx, int dy) {
image.drawText(pos.x + dx, pos.y + dy - getFont().getMaxAscent(), getText());
}
public void render(Image image) {
int x = pos.x;
int y = pos.y;
int w = size.width;
int h = size.height;
if (w == 0 || h == 0) return;
if (isEnabled()) {
image.setForeColor(getForeColor());
} else {
image.setForeColor(getDisabledColor());
}
image.setLineStyle(LineStyle.SOLID);
image.drawBox(x, y, w, h);
if (isFocused()) {
image.setLineStyle(LineStyle.DASH);
image.drawBox(x + 1, y + 1, w - 2, h - 2);
image.setLineStyle(LineStyle.SOLID);
}
if (icon == null) {
//TODO : center text
renderText(image, 4, 4);
} else {
//TODO : center icon
image.drawImageBlend(icon, x + ((getWidth() - icon.getWidth()) / 2), y + ((getHeight() - icon.getHeight()) / 2), true);
}
}
public void setImage(Image image) {
icon = image;
}
public String toString() {
return "Button:" + getText();
}
protected void doAction() {
if (!isEnabled()) return;
if (action != null) {
action.actionPerformed(this);
}
}
public void mouseUp(int button) {
if (button == MouseButton.LEFT) {
doAction();
}
}
public void keyReleased(int key) {
if (key == KeyCode.VK_SPACE) {
doAction();
}
}
public void setActionListner(ActionListener action) {
this.action = action;
}
}