source: trunk/src/org/expeditee/items/Circle.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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