[109] | 1 | /**
|
---|
[919] | 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/>.
|
---|
[109] | 17 | */
|
---|
[919] | 18 |
|
---|
[109] | 19 | package org.expeditee.items;
|
---|
| 20 |
|
---|
| 21 | import java.util.Collection;
|
---|
| 22 | import java.util.LinkedList;
|
---|
| 23 |
|
---|
[1102] | 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 |
|
---|
[109] | 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 | */
|
---|
[1102] | 49 | public Circle(Text source)
|
---|
| 50 | {
|
---|
[109] | 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);
|
---|
[1102] | 58 | invalidateBounds();
|
---|
[109] | 59 | }
|
---|
[121] | 60 |
|
---|
[109] | 61 | public Collection<Item> getItemsToSave() {
|
---|
[121] | 62 | Collection<Item> toSave = super.getItemsToSave();
|
---|
[109] | 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
|
---|
[1102] | 95 | public PolygonBounds getEnclosedShape() {
|
---|
[147] | 96 | // assert(_poly != null);
|
---|
| 97 | // Ensure that vector items will gradient fill are painted OK!!
|
---|
[1102] | 98 | return ((EllipticalBounds) updateBounds()).getPolygon(16);
|
---|
[109] | 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());
|
---|
[1102] | 143 | newCircle.invalidateBounds();
|
---|
[109] | 144 | return newCircle;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Gets the radius of this circle.
|
---|
| 149 | *
|
---|
| 150 | * @return the radius of the cicle
|
---|
| 151 | */
|
---|
[795] | 152 | public double getRadius() {
|
---|
[109] | 153 | return _line.getLength();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | @Override
|
---|
[1102] | 157 | public boolean contains(Point p) {
|
---|
[109] | 158 | double radius = getRadius();
|
---|
| 159 |
|
---|
[1143] | 160 | double distance = Math.sqrt(Math.pow(Math.abs(_center.getX() - p.getX()), 2)
|
---|
| 161 | + Math.pow(Math.abs(_center.getY() - p.getY()), 2));
|
---|
[109] | 162 |
|
---|
[376] | 163 | return Math.abs(distance - radius) < getGravity() * 2;
|
---|
[109] | 164 | }
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * (non-Javadoc)
|
---|
| 168 | *
|
---|
| 169 | * @see org.expeditee.items.Item#paint(java.awt.Graphics2D)
|
---|
| 170 | */
|
---|
| 171 | @Override
|
---|
[1102] | 172 | public void paint() {
|
---|
[109] | 173 | int radius = (int) Math.round(getRadius());
|
---|
[1102] | 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();
|
---|
[109] | 185 | }
|
---|
[1102] | 186 |
|
---|
| 187 | EcosystemManager.getGraphicsManager().drawOval(centre, diameters, 0.0f, fill, lineColour, lineStroke);
|
---|
| 188 |
|
---|
[109] | 189 | if (isHighlighted()) {
|
---|
[1102] | 190 | _center.paint();
|
---|
[109] | 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | @Override
|
---|
[1102] | 195 | public void setHighlightModeAndColour(HighlightMode mode, Colour color) {
|
---|
| 196 | _center.setHighlightModeAndColour(mode, color);
|
---|
| 197 | super.setHighlightModeAndColour(mode, color);
|
---|
[109] | 198 | }
|
---|
| 199 |
|
---|
| 200 | @Override
|
---|
[1102] | 201 | public void setHighlightColor(Colour c) {
|
---|
[115] | 202 | _center.setHighlightColor(c);
|
---|
[1102] | 203 | /*return*/ super.setHighlightColor(c);
|
---|
[109] | 204 | }
|
---|
[121] | 205 |
|
---|
[115] | 206 | @Override
|
---|
[1102] | 207 | public void setFillColor(Colour c) {
|
---|
[115] | 208 | super.setFillColor(c);
|
---|
| 209 | _center.setColor(c);
|
---|
[143] | 210 | invalidateCommonTrait(ItemAppearence.FillColor);
|
---|
[115] | 211 | }
|
---|
[147] | 212 |
|
---|
[145] | 213 | @Override
|
---|
[1102] | 214 | public void setGradientColor(Colour c) {
|
---|
[145] | 215 | super.setGradientColor(c);
|
---|
| 216 | invalidateCommonTrait(ItemAppearence.GradientColor);
|
---|
| 217 | }
|
---|
[109] | 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
|
---|
[1102] | 248 | public EllipticalBounds updateBounds()
|
---|
| 249 | {
|
---|
| 250 | return new EllipticalBounds(_center.getPosition(), (int) (getRadius() * 2));
|
---|
[109] | 251 | }
|
---|
| 252 |
|
---|
| 253 | @Override
|
---|
| 254 | public float getSize() {
|
---|
[121] | 255 | return (float) getRadius();
|
---|
[109] | 256 | }
|
---|
| 257 |
|
---|
[1102] | 258 | /** Resizes the circle from the center. */
|
---|
[109] | 259 | @Override
|
---|
[1102] | 260 | public void setSize(float size)
|
---|
| 261 | {
|
---|
[109] | 262 | double ratio = size / getRadius();
|
---|
[870] | 263 |
|
---|
[109] | 264 | // Extend the line along the same plane as the underlying line
|
---|
| 265 | _source.translate(_center.getPosition(), ratio);
|
---|
| 266 |
|
---|
[1102] | 267 | invalidateBounds();
|
---|
[109] | 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 |
|
---|
[1102] | 280 | // TODO: Why is this commented out? cts16
|
---|
[109] | 281 | @Override
|
---|
[1102] | 282 | public void translate(Point origin, double ratio)
|
---|
| 283 | {
|
---|
| 284 | invalidateBounds();
|
---|
[121] | 285 | // _center.translate(origin, ratio);
|
---|
| 286 | // super.translate(origin, ratio);
|
---|
[109] | 287 | }
|
---|
[121] | 288 |
|
---|
| 289 | @Override
|
---|
[1102] | 290 | public AxisAlignedBoxBounds getDrawingArea() {
|
---|
[121] | 291 |
|
---|
| 292 | float thickness = getThickness();
|
---|
| 293 | double radius = getRadius();
|
---|
| 294 |
|
---|
| 295 | int size = (int) ((2 * radius) + 3.0 + thickness);
|
---|
| 296 |
|
---|
[1102] | 297 | return new AxisAlignedBoxBounds((int) (_center.getX() - radius
|
---|
[121] | 298 | - 0.5 - (thickness / 2.0)), (int) (_center.getY() - radius
|
---|
[1102] | 299 | - 0.5 - (thickness / 2.0)), size, size);
|
---|
[121] | 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() {
|
---|
[115] | 310 | return _center;
|
---|
| 311 | }
|
---|
[147] | 312 |
|
---|
| 313 | @Override
|
---|
[1402] | 314 | public void setPermission(PermissionTriple permissionPair) {
|
---|
[737] | 315 | super.setPermission(permissionPair);
|
---|
| 316 | _center.setPermission(permissionPair);
|
---|
| 317 | _line.setPermission(permissionPair);
|
---|
[147] | 318 | }
|
---|
[311] | 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 | }
|
---|
[376] | 331 |
|
---|
| 332 | @Override
|
---|
| 333 | public boolean isConnectedToAnnotation() {
|
---|
| 334 | return false;
|
---|
| 335 | }
|
---|
[109] | 336 | }
|
---|