|
1
|
|
|
package org.gannacademy.cdf.graphics; |
|
2
|
|
|
|
|
3
|
|
|
import org.gannacademy.cdf.graphics.ui.DrawingPanel; |
|
4
|
|
|
|
|
5
|
|
|
import java.awt.*; |
|
6
|
|
|
import java.awt.geom.Rectangle2D; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* <p>Draw text</p> |
|
10
|
|
|
* |
|
11
|
|
|
* <p><img src="doc-files/Text.png" alt="Diagram of Text parameters"></p> |
|
12
|
|
|
* |
|
13
|
|
|
* @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a> |
|
14
|
|
|
*/ |
|
15
|
|
|
public class Text extends Drawable { |
|
16
|
|
|
private double x, y; |
|
17
|
|
|
private String text; |
|
|
|
|
|
|
18
|
|
|
private Font font = new Font("Arial", Font.PLAIN, 20); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* <p>Construct a new text object</p> |
|
22
|
|
|
* |
|
23
|
|
|
* <p><img src="doc-files/Text.png" alt="Diagram of Text parameters"></p> |
|
24
|
|
|
* |
|
25
|
|
|
* <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis |
|
26
|
|
|
* increasing from top to bottom. All window coordinates exist in the first quadrant.</p> |
|
27
|
|
|
* |
|
28
|
|
|
* <p><img src="doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p> |
|
29
|
|
|
* |
|
30
|
|
|
* @param text to draw |
|
31
|
|
|
* @param x coordinate of baseline origin |
|
32
|
|
|
* @param y coordinate of baseline origin |
|
33
|
|
|
* @param drawingPanel on which to draw |
|
34
|
|
|
*/ |
|
35
|
|
|
public Text(String text, double x, double y, DrawingPanel drawingPanel) { |
|
36
|
|
|
setText(text); |
|
37
|
|
|
setX(x); |
|
38
|
|
|
setY(y); |
|
39
|
|
|
setDrawingPanel(drawingPanel); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
@Override |
|
43
|
|
|
public Shape getShape() { |
|
44
|
|
|
return new Rectangle2D.Double(x, y - getMaxAscent(), getWidth(), getHeight()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
@Override |
|
48
|
|
|
public double getX() { |
|
49
|
|
|
return x; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@Override |
|
53
|
|
|
public void setX(double x) { |
|
54
|
|
|
this.x = x; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
@Override |
|
58
|
|
|
public double getY() { |
|
59
|
|
|
return y; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
@Override |
|
63
|
|
|
public void setY(double y) { |
|
64
|
|
|
this.y = y; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
@Override |
|
68
|
|
|
public double getWidth() { |
|
69
|
|
|
return getDrawingPanel().getFontMetrics(getFont()).getStringBounds(getText(), getDrawingPanel().getGraphics()).getWidth(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Attempts to rescale the font size to match desired width |
|
74
|
|
|
* |
|
75
|
|
|
* @param width to use |
|
76
|
|
|
*/ |
|
77
|
|
|
@Override |
|
78
|
|
|
public void setWidth(double width) { |
|
79
|
|
|
double scale = width / getWidth(); |
|
80
|
|
|
setFont(new Font(getFont().getFontName(), getFont().getStyle(), (int) (scale * getFont().getSize()))); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
@Override |
|
84
|
|
|
public double getHeight() { |
|
85
|
|
|
return getDrawingPanel().getFontMetrics(getFont()).getHeight(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Attempts to rescale the font size to match desired height |
|
90
|
|
|
* |
|
91
|
|
|
* @param height to use |
|
92
|
|
|
*/ |
|
93
|
|
|
@Override |
|
94
|
|
|
public void setHeight(double height) { |
|
95
|
|
|
double scale = height / getHeight(); |
|
96
|
|
|
setFont(new Font(getFont().getFontName(), getFont().getStyle(), (int) (scale * getFont().getSize()))); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Current text |
|
101
|
|
|
* |
|
102
|
|
|
* @return Current text |
|
103
|
|
|
*/ |
|
104
|
|
|
public String getText() { |
|
105
|
|
|
return text; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Replace text |
|
110
|
|
|
* |
|
111
|
|
|
* @param text to use |
|
112
|
|
|
*/ |
|
113
|
|
|
public void setText(String text) { |
|
114
|
|
|
this.text = text; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Current {@link Font} |
|
119
|
|
|
* |
|
120
|
|
|
* @return Current {@link Font} |
|
121
|
|
|
*/ |
|
122
|
|
|
public Font getFont() { |
|
123
|
|
|
return font; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Replace current font |
|
128
|
|
|
* |
|
129
|
|
|
* @param font to use |
|
130
|
|
|
*/ |
|
131
|
|
|
public void setFont(Font font) { |
|
132
|
|
|
this.font = font; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public Rectangle2D getBounds() { |
|
136
|
|
|
return getBounds(getDrawingPanel()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private Rectangle2D getBounds(DrawingPanel drawingPanel) { |
|
140
|
|
|
return drawingPanel.getGraphics().getFontMetrics(getFont()) |
|
141
|
|
|
.getStringBounds(getText(), getDrawingPanel().getGraphics()) |
|
142
|
|
|
.getBounds2D(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Height of character ascent in current font |
|
147
|
|
|
* |
|
148
|
|
|
* @return Height of maximum ascent in current font |
|
149
|
|
|
*/ |
|
150
|
|
|
public double getMaxAscent() { |
|
151
|
|
|
return getDrawingPanel().getGraphics().getFontMetrics(getFont()).getMaxAscent(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Height of character descent in current font |
|
157
|
|
|
* |
|
158
|
|
|
* @return height of maximum descent in current font |
|
159
|
|
|
*/ |
|
160
|
|
|
public double getMaxDescent() { |
|
161
|
|
|
return getDrawingPanel().getGraphics().getFontMetrics(getFont()).getMaxDescent(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
@Override |
|
165
|
|
|
public void translate(double dx, double dy) { |
|
166
|
|
|
setX(x + dx); |
|
167
|
|
|
setY(y + dy); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
@Override |
|
171
|
|
|
public void setLocation(double x, double y) { |
|
172
|
|
|
setX(x); |
|
173
|
|
|
setY(y); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
@Override |
|
177
|
|
|
public void draw(Graphics2D graphics) { |
|
178
|
|
|
if (getFillColor() != TRANSPARENT) { |
|
179
|
|
|
graphics.setPaint(getFillColor()); |
|
180
|
|
|
graphics.fill(getShape()); |
|
181
|
|
|
} |
|
182
|
|
|
graphics.setPaint(getStrokeColor()); |
|
183
|
|
|
graphics.setFont(getFont()); |
|
184
|
|
|
graphics.drawString(getText(), (float) getX(), (float) getY()); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|