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