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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
public class Label extends JComponent {
private static final long serialVersionUID = 1L;
private String value;
private Color color;
private List<String> lines;
public Label() {
value = "";
}
public Label(String text) {
setText(text);
}
public void setText(String text) {
if (text == null) {
value = "";
} else {
value = text;
}
}
public String getText() {
return value;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (color != null) {
g2.setPaint(color);
}
lines = split(g2);
FontRenderContext context = g2.getFontRenderContext();
Font font = g2.getFont();
Rectangle2D bounds = font.getStringBounds(lines.get(0), context);
int x = 0;
int y = 0;
if (getAlignmentY() == CENTER_ALIGNMENT) {
y += (int) (getHeight() - bounds.getHeight() * lines.size()) / 2;
} else if (getAlignmentY() == BOTTOM_ALIGNMENT) {
y += (int) (getHeight() - bounds.getHeight() * lines.size());
}
for (int i = 0; i < lines.size(); i++) {
bounds = font.getStringBounds(lines.get(i), context);
if (getAlignmentX() == CENTER_ALIGNMENT) {
x = (int) ((getWidth() - bounds.getWidth()) / 2);
} else if (getAlignmentX() == RIGHT_ALIGNMENT) {
x = (int) (getWidth() - bounds.getWidth());
}
y += bounds.getHeight();
g2.drawString(lines.get(i), x, y);
}
}
public List<String> split(Graphics2D g) {
ArrayList<String> lines = new ArrayList<>();
int prev = 0;
for (int i = 0; i < value.length(); i++) {
if (value.charAt(i) == '\n') {
lines.add(value.substring(prev, i));
prev = i + 1;
}
}
lines.add(value.substring(prev, value.length()));
FontRenderContext context = g.getFontRenderContext();
Font font = g.getFont();
int limit = findMaxLength(value, font.getStringBounds(value, context).getWidth(), getWidth());
Rectangle2D bounds;
String temp;
for (int i = 0; i < lines.size(); i++) {
temp = lines.get(i);
bounds = font.getStringBounds(temp, context);
if (bounds.getWidth() > getWidth()) {
int j;
for (j = limit; j > 0; j--) {
if (Character.isWhitespace(temp.charAt(j))) {
String leftPart = temp.substring(0, j);
if (font.getStringBounds(leftPart, context).getWidth() > getWidth())
continue;
lines.set(i, leftPart);
lines.add(i + 1, temp.substring(j + 1));
break;
}
}
if (j == 0) {
int newLen = limit - 3;
if (newLen <= temp.length() && newLen >= 0) {
temp = temp.substring(0, newLen).concat("...");
}
lines.set(i, temp);
}
}
}
lines.trimToSize();
return lines;
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
private int findMaxLength(String str, double strWidth, double compWidth) {
return (int) (compWidth / (strWidth / countVisibleCharacters(str)));
}
private int countVisibleCharacters(String s) {
int count = 0;
for (char c : s.toCharArray()) {
if (!Character.isISOControl(c)) {
count++;
}
}
return count;
}
public Dimension getPreferredSize() {
Graphics2D g = (Graphics2D) getGraphics();
FontRenderContext context = g.getFontRenderContext();
Rectangle2D bounds = g.getFont().getStringBounds(value, context);
return new Dimension((int) bounds.getWidth(), (int) bounds.getHeight());
}
public boolean equals(Object otherObject) {
if (otherObject == this) {
return true;
}
if (otherObject == null || getClass() != otherObject.getClass()) {
return false;
}
Label other = (Label) otherObject;
return value.equals(other.getText());
}
public int hashCode() {
return value.hashCode() * 17 + 2;
}
public String toString() {
return "Label[text=\"" + value + "\"]";
}
} |