-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathDrawableTextLine.java
More file actions
288 lines (262 loc) · 8.77 KB
/
DrawableTextLine.java
File metadata and controls
288 lines (262 loc) · 8.77 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import org.opensourcephysics.controls.XML;
import org.opensourcephysics.controls.XMLControl;
import org.opensourcephysics.controls.XMLLoader;
/**
* DrawableTextLine draws short Strings with subscripts and superscripts.
*
* @author Wolfgang Christian
* @version 1.0
*/
public class DrawableTextLine extends TextLine implements Drawable {
double x, y;
double theta = 0;
protected boolean pixelXY = false; // x and y are given in pixels
/**
* Constructs a DrawableTextLine with the given text and location.
*
* @param text String
* @param x double
* @param y double
*/
public DrawableTextLine(String text, double x, double y) {
super(text);
this.x = x;
this.y = y;
color = Color.BLACK;
}
/**
* Sets the pixelPosition flag.
*
* Pixels are used to position the object.
*
* @param enable boolean
*/
public void setPixelXY(boolean enable) {
this.pixelXY = enable;
}
/**
* Sets the x coordinate.
*
* @param x double
*/
public void setX(double x) {
this.x = x;
}
/**
* Sets the angle.
*
* @param theta double
*/
public void setTheta(double theta) {
this.theta = theta;
}
/**
* Gets the x coordinate.
*
* @return double
*/
public double getX() {
return x;
}
/**
* Sets the y coordinate.
*
* @param y double
*/
public void setY(double y) {
this.y = y;
}
/**
* Gets the y coordinate.
*
* @return double
*/
public double getY() {
return y;
}
/**
* Draws the TextLine.
*
* @param panel DrawingPanel
* @param g Graphics
*/
public void draw(DrawingPanel panel, Graphics g) {
if((text==null)||text.equals("")) { //$NON-NLS-1$
return;
}
Font oldFont = g.getFont();
if(this.pixelXY) {
drawWithPix(panel, g);
} else {
drawWithWorld(panel, g);
}
g.setFont(oldFont);
}
/**
* Draws the TextLine using world units for x and y.
*
* @param panel DrawingPanel
* @param g Graphics
*/
void drawWithPix(DrawingPanel panel, Graphics g) {
if(OSPRuntime.isMac()){
drawWithPixMac( panel, g);
}else{
drawWithPixWindows( panel, g);
}
}
private void drawWithPixWindows(DrawingPanel panel, Graphics g) {
if(theta!=0) {
((Graphics2D) g).transform(AffineTransform.getRotateInstance(-theta, x, y));
drawText(g, (int) x, (int) y);
((Graphics2D) g).transform(AffineTransform.getRotateInstance(theta, x, y));
} else {
drawText(g, (int) x, (int) y);
}
}
private void drawWithPixMac(DrawingPanel panel, Graphics g) {
if(theta==0){
drawWithPixWindows( panel, g);
return;
}
int w=g.getFontMetrics().stringWidth(text)+7;
int h=g.getFontMetrics().getHeight()+10;
BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D imageGraphics=image.createGraphics();
imageGraphics.setFont(g.getFont());
//imageGraphics.setColor(Color.RED); // debug
//imageGraphics.fillRect(0, 0, w, h);
imageGraphics.setColor(Color.BLACK);
drawText(imageGraphics, w/2-2, h-5);
imageGraphics.dispose();
Graphics2D g2d=(Graphics2D) g;
g2d.translate(x-h-2,y+w/2);
AffineTransform at= AffineTransform.getRotateInstance(-theta, 0, 0);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(image,at, panel);
g2d.translate(-x+h+2,-y-w/2);
}
/**
* Draws the TextLine using world units for x and y.
*
* @param panel DrawingPanel
* @param g Graphics
*/
void drawWithWorld(DrawingPanel panel, Graphics g) {
if(OSPRuntime.isMac()){
drawWithWorldMac( panel, g);
}else{ // windows
drawWithWorldWindows( panel, g);
}
}
private void drawWithWorldMac(DrawingPanel panel, Graphics g) {
if(theta==0){
drawWithWorldWindows( panel, g);
return;
}
int w=g.getFontMetrics().stringWidth(text)+7;
int h=g.getFontMetrics().getHeight()+10;
BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D imageGraphics=image.createGraphics();
imageGraphics.setColor(Color.BLACK);
imageGraphics.setFont(g.getFont());
drawText(imageGraphics, w/2-2, h-5);
imageGraphics.dispose();
Graphics2D g2d=(Graphics2D) g;
Point2D pt = new Point2D.Double(x, y);
pt = panel.getPixelTransform().transform(pt, pt);
g2d.translate(pt.getX()-h-2,pt.getY()+w/2);
AffineTransform at= AffineTransform.getRotateInstance(-theta, 0, 0);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(image,at, panel);
g2d.translate(-pt.getX()+h+2,-pt.getY()-w/2);
}
/**
* Draws the TextLine using world units for x and y.
*
* @param panel DrawingPanel
* @param g Graphics
*/
private void drawWithWorldWindows(DrawingPanel panel, Graphics g) {
Point2D pt = new Point2D.Double(x, y);
pt = panel.getPixelTransform().transform(pt, pt);
if(theta!=0) {
((Graphics2D) g).transform(AffineTransform.getRotateInstance(-theta, pt.getX(), pt.getY()));
drawText(g, (int) pt.getX(), (int) pt.getY());
((Graphics2D) g).transform(AffineTransform.getRotateInstance(theta, pt.getX(), pt.getY()));
} else {
drawText(g, (int) pt.getX(), (int) pt.getY());
}
}
/**
* Gets the XML object loader for this class.
* @return ObjectLoader
*/
public static XML.ObjectLoader getLoader() {
return new DrawableTextLineLoader();
}
/**
* A class to save and load InteractiveArrow in an XMLControl.
*/
protected static class DrawableTextLineLoader extends XMLLoader {
public void saveObject(XMLControl control, Object obj) {
DrawableTextLine drawableTextLine = (DrawableTextLine) obj;
control.setValue("text", drawableTextLine.getText()); //$NON-NLS-1$
control.setValue("x", drawableTextLine.x); //$NON-NLS-1$
control.setValue("y", drawableTextLine.y); //$NON-NLS-1$
control.setValue("theta", drawableTextLine.theta); //$NON-NLS-1$
control.setValue("color", drawableTextLine.color); //$NON-NLS-1$
control.setValue("pixel position", drawableTextLine.pixelXY); //$NON-NLS-1$
}
public Object createObject(XMLControl control) {
return new DrawableTextLine("", 0, 0); //$NON-NLS-1$
}
public Object loadObject(XMLControl control, Object obj) {
DrawableTextLine drawableTextLine = (DrawableTextLine) obj;
drawableTextLine.x = control.getDouble("x"); //$NON-NLS-1$
drawableTextLine.y = control.getDouble("y"); //$NON-NLS-1$
drawableTextLine.theta = control.getDouble("theta"); //$NON-NLS-1$
drawableTextLine.pixelXY = control.getBoolean("pixel position"); //$NON-NLS-1$
drawableTextLine.setText(control.getString("text")); //$NON-NLS-1$
drawableTextLine.color = (Color) control.getObject("color"); //$NON-NLS-1$
return obj;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/