1 | /**
|
---|
2 | *
|
---|
3 | */
|
---|
4 | package org.expeditee.items;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Graphics2D;
|
---|
8 | import java.awt.Polygon;
|
---|
9 | import java.awt.Rectangle;
|
---|
10 | import java.awt.geom.Point2D;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.LinkedList;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * @author root
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class Circle extends XRayable {
|
---|
19 |
|
---|
20 | private Item _center;
|
---|
21 |
|
---|
22 | private Line _line;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Construct a circle
|
---|
26 | *
|
---|
27 | * @param _source
|
---|
28 | */
|
---|
29 | public Circle(Text source) {
|
---|
30 | super(source);
|
---|
31 | // Collection<Item> connected = source.getAllConnected();
|
---|
32 | // assert (connected.size() == 4);
|
---|
33 | _line = source.getLines().get(0);
|
---|
34 | _center = _line.getOppositeEnd(_source);
|
---|
35 | _center.addEnclosure(this);
|
---|
36 | _line.setHidden(true);
|
---|
37 | updatePolygon();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public Collection<Item> getItemsToSave() {
|
---|
41 | Collection<Item> toSave = super.getItemsToSave();
|
---|
42 | toSave.add(_center);
|
---|
43 | return toSave;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Collection<Item> getConnected() {
|
---|
48 | Collection<Item> conn = super.getConnected();
|
---|
49 | conn.add(_center);
|
---|
50 | conn.add(_line);
|
---|
51 | return conn;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void addAllConnected(Collection<Item> connected) {
|
---|
56 | super.addAllConnected(connected);
|
---|
57 | if (!connected.contains(_center)) {
|
---|
58 | connected.add(_center);
|
---|
59 | connected.add(_line);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public boolean isFloating() {
|
---|
65 | return _center.isFloating() || super.isFloating();
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public boolean isEnclosed() {
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public Polygon getEnclosedShape() {
|
---|
75 | // assert(_poly != null);
|
---|
76 | // Ensure that vector items will gradient fill are painted OK!!
|
---|
77 | if (_poly == null) {
|
---|
78 | updatePolygon();
|
---|
79 | }
|
---|
80 | return _poly;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public double getEnclosedArea() {
|
---|
85 | double radius = getRadius();
|
---|
86 | return Math.PI * radius * radius;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public Collection<Item> getEnclosingDots() {
|
---|
91 | Collection<Item> enclosed = new LinkedList<Item>();
|
---|
92 | enclosed.add(this);
|
---|
93 | enclosed.add(_center);
|
---|
94 | return enclosed;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * (non-Javadoc)
|
---|
99 | *
|
---|
100 | * @see org.expeditee.items.Item#copy()
|
---|
101 | */
|
---|
102 | @Override
|
---|
103 | public Item copy() {
|
---|
104 | Collection<Item> toCopy = new LinkedList<Item>();
|
---|
105 | toCopy.add(_source);
|
---|
106 | toCopy.add(_line);
|
---|
107 | toCopy.add(_center);
|
---|
108 |
|
---|
109 | Collection<Item> newItems = ItemUtils.CopyItems(toCopy);
|
---|
110 | assert (newItems.size() == 3);
|
---|
111 | // find the Source item from the three items
|
---|
112 | Text newSource = null;
|
---|
113 | for (Item i : newItems) {
|
---|
114 | if (i instanceof Text) {
|
---|
115 | newSource = (Text) i;
|
---|
116 | if (ItemUtils.startsWithTag(i, "@c"))
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | assert (newSource != null);
|
---|
121 | Circle newCircle = new Circle(newSource);
|
---|
122 | Item.DuplicateItem(this, newCircle);
|
---|
123 | newCircle._line.setVisible(_line.isVisible());
|
---|
124 | newCircle._source.setVisible(_source.isVisible());
|
---|
125 | newCircle.updatePolygon();
|
---|
126 | return newCircle;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Gets the radius of this circle.
|
---|
131 | *
|
---|
132 | * @return the radius of the cicle
|
---|
133 | */
|
---|
134 | private double getRadius() {
|
---|
135 | return _line.getLength();
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public boolean contains(int x, int y) {
|
---|
140 | double radius = getRadius();
|
---|
141 |
|
---|
142 | double distance = Math.sqrt(Math.pow(Math.abs(_center.getX() - x), 2)
|
---|
143 | + Math.pow(Math.abs(_center.getY() - y), 2));
|
---|
144 |
|
---|
145 | return Math.abs(distance - radius) < getGravity() * 2;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * (non-Javadoc)
|
---|
150 | *
|
---|
151 | * @see org.expeditee.items.Item#paint(java.awt.Graphics2D)
|
---|
152 | */
|
---|
153 | @Override
|
---|
154 | public void paint(Graphics2D g) {
|
---|
155 | int radius = (int) Math.round(getRadius());
|
---|
156 | int diameter = radius * 2;
|
---|
157 | Color fillColor = getFillColor();
|
---|
158 | if (fillColor != null) {
|
---|
159 | setFillPaint(g);
|
---|
160 | g.fillOval(_center.getX() - radius, _center.getY() - radius,
|
---|
161 | diameter, diameter);
|
---|
162 | }
|
---|
163 | if (getThickness() > 0 || fillColor == null) {
|
---|
164 | Color lineColor = getPaintColor();
|
---|
165 | g.setColor(lineColor);
|
---|
166 | g.setStroke(_line.getStroke());
|
---|
167 | g.drawOval(_center.getX() - radius, _center.getY() - radius,
|
---|
168 | diameter, diameter);
|
---|
169 | }
|
---|
170 | // Arc version, same result but allows for portions of the circle to be
|
---|
171 | // drawn
|
---|
172 | // g.drawArc(end.getX() - (distance / 2), end.getY() - (distance / 2),
|
---|
173 | // distance, distance, 0, 360);
|
---|
174 |
|
---|
175 | if (isHighlighted()) {
|
---|
176 | // Flag the background color of the circle so that the item will be
|
---|
177 | // drawn with alternate color if the background is the same as
|
---|
178 | // the highlight}
|
---|
179 | _center.paint(g);
|
---|
180 | Color highlightColor = getHighlightColor();
|
---|
181 | g.setColor(highlightColor);
|
---|
182 | g.setStroke(HIGHLIGHT_STROKE);
|
---|
183 | g.drawOval(_center.getX() - radius, _center.getY() - radius,
|
---|
184 | diameter, diameter);
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public void setHighlightMode(HighlightMode mode, Color color) {
|
---|
190 | _center.setHighlightMode(mode, color);
|
---|
191 | super.setHighlightMode(mode, color);
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public int setHighlightColor(Color c) {
|
---|
196 | _center.setHighlightColor(c);
|
---|
197 | return super.setHighlightColor(c);
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public void setFillColor(Color c) {
|
---|
202 | super.setFillColor(c);
|
---|
203 | _center.setColor(c);
|
---|
204 | invalidateCommonTrait(ItemAppearence.FillColor);
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public void setGradientColor(Color c) {
|
---|
209 | super.setGradientColor(c);
|
---|
210 | invalidateCommonTrait(ItemAppearence.GradientColor);
|
---|
211 | }
|
---|
212 |
|
---|
213 | // @Override
|
---|
214 | // public void setPosition(float x, float y) {
|
---|
215 | // // float deltaX = x - _source._x;
|
---|
216 | // // float deltaY = y - _source._y;
|
---|
217 | // // _center.setPosition(_center._x + deltaX, _center._y + deltaY);
|
---|
218 | // _source.setPosition(x, y);
|
---|
219 | //
|
---|
220 | // updatePolygon();
|
---|
221 | // }
|
---|
222 |
|
---|
223 | // TODO use an algorithm to get more precicely for contains and intersects
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * (non-Javadoc)
|
---|
227 | *
|
---|
228 | * @see org.expeditee.items.Item#setAnnotation(boolean)
|
---|
229 | */
|
---|
230 | @Override
|
---|
231 | public void setAnnotation(boolean val) {
|
---|
232 | // TODO Auto-generated method stub
|
---|
233 |
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * (non-Javadoc)
|
---|
238 | *
|
---|
239 | * @see org.expeditee.items.Item#updatePolygon()
|
---|
240 | */
|
---|
241 | @Override
|
---|
242 | public void updatePolygon() {
|
---|
243 | double radius = getRadius();
|
---|
244 | // Approximation of circle for mouse interaction
|
---|
245 | int points = 20;
|
---|
246 |
|
---|
247 | double radians = 0.0;
|
---|
248 | int xPoints[] = new int[points];
|
---|
249 | int yPoints[] = new int[xPoints.length];
|
---|
250 |
|
---|
251 | for (int i = 0; i < xPoints.length; i++) {
|
---|
252 | xPoints[i] = (int) Math.round(radius * Math.cos(radians));
|
---|
253 | yPoints[i] = (int) Math.round(radius * Math.sin(radians));
|
---|
254 | radians += (2.0 * Math.PI) / xPoints.length;
|
---|
255 | }
|
---|
256 |
|
---|
257 | _poly = new Polygon(xPoints, yPoints, xPoints.length);
|
---|
258 | _poly.translate(_center.getX(), _center.getY());
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public float getSize() {
|
---|
264 | return (float) getRadius();
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Resizes the circle from the center.
|
---|
269 | */
|
---|
270 | @Override
|
---|
271 | public void setSize(float size) {
|
---|
272 | double ratio = size / getRadius();
|
---|
273 | ratio = (1.0 - ratio) * 2 + 1.0;
|
---|
274 | // Extend the line along the same plane as the underlying line
|
---|
275 | _source.translate(_center.getPosition(), ratio);
|
---|
276 |
|
---|
277 | updatePolygon();
|
---|
278 | }
|
---|
279 |
|
---|
280 | @Override
|
---|
281 | public void setLinePattern(int[] pattern) {
|
---|
282 | _line.setLinePattern(pattern);
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public int[] getLinePattern() {
|
---|
287 | return _line.getLinePattern();
|
---|
288 | }
|
---|
289 |
|
---|
290 | @Override
|
---|
291 | public void translate(Point2D origin, double ratio) {
|
---|
292 | updatePolygon();
|
---|
293 | // _center.translate(origin, ratio);
|
---|
294 | // super.translate(origin, ratio);
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public Rectangle[] getDrawingArea() {
|
---|
299 |
|
---|
300 | float thickness = getThickness();
|
---|
301 | double radius = getRadius();
|
---|
302 |
|
---|
303 | int size = (int) ((2 * radius) + 3.0 + thickness);
|
---|
304 |
|
---|
305 | return new Rectangle[] { new Rectangle((int) (_center.getX() - radius
|
---|
306 | - 0.5 - (thickness / 2.0)), (int) (_center.getY() - radius
|
---|
307 | - 0.5 - (thickness / 2.0)), size, size) };
|
---|
308 | }
|
---|
309 |
|
---|
310 | // @Override
|
---|
311 | // public void setPosition(float x, float y){
|
---|
312 | // float deltaX = x - _source._x;
|
---|
313 | // float deltaY = y = _source._y;
|
---|
314 | // _center.setPosition(deltaX, deltaY);
|
---|
315 | // super.setPosition(x,y);
|
---|
316 | // }
|
---|
317 | public Item getCenter() {
|
---|
318 | return _center;
|
---|
319 | }
|
---|
320 |
|
---|
321 | @Override
|
---|
322 | public void setPermission(UserAppliedPermission p) {
|
---|
323 | super.setPermission(p);
|
---|
324 | _center.setPermission(p);
|
---|
325 | _line.setPermission(p);
|
---|
326 | }
|
---|
327 |
|
---|
328 | @Override
|
---|
329 | public void scale(Float scale, int originX, int originY) {
|
---|
330 | getCenter().scale(scale, originX, originY);
|
---|
331 | super.scale(scale, originX, originY);
|
---|
332 | }
|
---|
333 |
|
---|
334 | @Override
|
---|
335 | public void setThickness(float thick, boolean setConnected) {
|
---|
336 | super.setThickness(thick, setConnected);
|
---|
337 | _line.refreshStroke(thick);
|
---|
338 | }
|
---|
339 |
|
---|
340 | @Override
|
---|
341 | public boolean isConnectedToAnnotation() {
|
---|
342 | return false;
|
---|
343 | }
|
---|
344 | }
|
---|