-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProtractor.java
More file actions
209 lines (192 loc) · 6.92 KB
/
Protractor.java
File metadata and controls
209 lines (192 loc) · 6.92 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
/*
* 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.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.text.DecimalFormat;
/**
* A Protractor with an arrow that can be used to measure angles.
*
* @author W. Christian
* @version 1.0
*/
public class Protractor extends InteractiveCircle implements Drawable {
static final double PI2 = Math.PI*2;
static final String thetaStr = "$\\theta$="; //$NON-NLS-1$
int protractorRadius, protractorRadius2, arrowLengthPix;
protected Tip tip = new Tip();
protected double arrowTheta = 0, orientation = 0;
protected DecimalFormat f = new DecimalFormat("000"); //$NON-NLS-1$
protected boolean showTheta = false;
protected InteractiveLabel tauBox = new InteractiveLabel(thetaStr+f.format(getTheta()));
/**
* Constructs a protractor with the given pixel size.
*
* @param protractorRadius int
*/
public Protractor(int protractorRadius) {
this.protractorRadius = protractorRadius;
protractorRadius2 = protractorRadius*2;
arrowLengthPix = protractorRadius;
tip.color = Color.BLUE;
tauBox.setOffsetX(-20);
tauBox.setOffsetY(5);
}
/**
* Constructs a protractor with a default radius of 40 pixels.
*/
public Protractor() {
this(40);
}
/**
* Sets the angle of the arrow on the protractor.
* @param theta double
*/
public void setTheta(double angle) {
this.arrowTheta = angle+orientation;
}
/**
* Gets the angle of the arrow on the protractor.
* @return double
*/
public double getTheta() {
// return PBC.separation(arrowTheta-orientation,Math.PI*2);
// inline the method for efficiency and to decouple from the numerics package
double dr = arrowTheta-orientation;
return dr-PI2*Math.floor(dr/PI2+0.5);
}
/**
* Sets the orientation of the protractor.
* @param angle double
*/
public void setOrientation(double angle) {
this.orientation = angle;
}
/**
* Gets the orientation of the protractor.
* @return double
*/
public double getOrientation() {
return orientation;
}
/**
* Shows theta when the protractor is drawn when true.
* @param show boolean
*/
public void setShowTheta(boolean show) {
showTheta = show;
}
/**
* Gets the show theta property.
* @return boolean
*/
public boolean isShowTheta() {
return showTheta;
}
public Interactive findInteractive(DrawingPanel panel, int xpix, int ypix) {
Interactive interactive = super.findInteractive(panel, xpix, ypix);
if(interactive!=null) {
return interactive;
}
interactive = tip.findInteractive(panel, xpix, ypix);
if(interactive!=null) {
return interactive;
}
return tauBox.findInteractive(panel, xpix, ypix);
}
/**
* Draws the protractor on the given drawing panel.
*
* @param panel DrawingPanel
* @param g Graphics
*/
public void draw(DrawingPanel panel, Graphics g) {
Graphics2D g2 = (Graphics2D) g;
double i1 = panel.xToPix(x);
double j1 = panel.yToPix(y);
//draw the vector with angle ticks
g2.setColor(new Color(240, 40, 40, 40));
// replace the circle with two half circles to indicate angle
//g2.fill(new Double(i1-pixRadius,j1-pixRadius,pixRadius2,pixRadius2)); // draws circle
double start = Math.toDegrees(orientation);
g2.fill(new Arc2D.Double(i1-protractorRadius, j1-protractorRadius, protractorRadius2, protractorRadius2, start, 180, Arc2D.PIE));
g2.setColor(new Color(40, 40, 240, 40));
g2.fill(new Arc2D.Double(i1-protractorRadius, j1-protractorRadius, protractorRadius2, protractorRadius2, start+180, 180, Arc2D.PIE));
g2.setColor(Color.gray);
g2.setStroke(new BasicStroke(0.5f));
for(int i = 0; i<36; i++) {
AffineTransform at = g2.getTransform();
at.rotate(-i*Math.PI/18, i1, j1);
g2.setTransform(at);
g2.draw(new Line2D.Double(i1+protractorRadius-5, j1, i1+protractorRadius, j1));
at.rotate(+i*Math.PI/18, i1, j1);
g2.setTransform(at);
}
tauBox.setText(thetaStr+f.format(Math.toDegrees(getTheta())), x, y);
if(showTheta) {
tauBox.draw(panel, g);
}
//tip.draw(panel,g); // draws a small circle at the tip
//drawing the arrow with the red head
AffineTransform at = g2.getTransform();
at.rotate(-arrowTheta, i1, j1);
g2.setTransform(at);
g2.setColor(Color.RED);
Stroke currentStroke = g2.getStroke();
g2.setStroke(new BasicStroke(1.5f));
g2.draw(new Line2D.Double(i1, j1, i1+arrowLengthPix, j1));
GeneralPath arrowHead = new GeneralPath();
g2.draw(new Line2D.Double());
arrowHead.moveTo((float) (i1+arrowLengthPix), (float) j1);
arrowHead.lineTo((float) (i1+arrowLengthPix-15), (float) (j1-5));
arrowHead.lineTo((float) (i1+arrowLengthPix-15), (float) (j1+5));
arrowHead.closePath();
g2.fill(arrowHead);
g2.draw(arrowHead);
at.rotate(+arrowTheta, i1, j1);
g2.setTransform(at);
g2.setStroke(currentStroke);
double length = arrowLengthPix/panel.getXPixPerUnit();
tip.setXY(x+length*Math.cos(arrowTheta), y+length*Math.sin(arrowTheta));
}
public class Tip extends InteractiveCircle {
public void setXY(double x, double y) {
this.x = x;
this.y = y;
arrowTheta = Math.atan2(y-Protractor.this.y, x-Protractor.this.x);
}
}
}
/*
* 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
*/