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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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