| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| 22 |
import java.awt.Color; |
| 23 |
import java.awt.Rectangle; |
| 24 |
import java.awt.geom.GeneralPath; |
| 25 |
import java.awt.geom.Rectangle2D; |
| •26 |
import java.awt.geom.Ellipse2D; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Iterator; |
| 30 |
|
| •31 |
import org.stathissideris.ascii2image.core.RenderingOptions; |
| 32 |
import org.stathissideris.ascii2image.text.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class DiagramShape extends DiagramComponent { |
| 39 |
|
| 40 |
private static final boolean DEBUG = false; |
| 41 |
|
| 42 |
public static final int TYPE_SIMPLE = 0; |
| 43 |
public static final int TYPE_ARROWHEAD = 1; |
| 44 |
public static final int TYPE_POINT_MARKER = 2; |
| 45 |
public static final int TYPE_DOCUMENT = 3; |
| 46 |
public static final int TYPE_STORAGE = 4; |
| 47 |
public static final int TYPE_IO = 5; |
| 48 |
public static final int TYPE_DECISION = 6; |
| 49 |
public static final int TYPE_MANUAL_OPERATION = 7; |
| 50 |
public static final int TYPE_TRAPEZOID = 8; |
| 51 |
public static final int TYPE_ELLIPSE = 9; |
| 52 |
public static final int TYPE_CUSTOM = 9999; |
| 53 |
|
| •54 |
|
| 55 |
public static final float SHAPE_SLOPE = 8; |
| 56 |
|
| 57 |
protected int type = TYPE_SIMPLE; |
| 58 |
|
| 59 |
private Color fillColor = null; |
| 60 |
private Color strokeColor = Color.black; |
| 61 |
|
| 62 |
private boolean isClosed = false; |
| 63 |
private boolean isStrokeDashed = false; |
| 64 |
|
| •65 |
protected ArrayList<ShapePoint> points = new ArrayList<ShapePoint>(); |
| 66 |
|
| 67 |
CustomShapeDefinition definition = null; |
| 68 |
|
| 69 |
public static void main(String[] args) { |
| 70 |
} |
| 71 |
|
| 72 |
public static DiagramShape createArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 73 |
if(!grid.isArrowhead(cell)) return null; |
| 74 |
if(grid.isNorthArrowhead(cell)) return createNorthArrowhead(grid, cell, cellXSize, cellYSize); |
| 75 |
if(grid.isSouthArrowhead(cell)) return createSouthArrowhead(grid, cell, cellXSize, cellYSize); |
| 76 |
if(grid.isWestArrowhead(cell)) return createWestArrowhead(grid, cell, cellXSize, cellYSize); |
| 77 |
if(grid.isEastArrowhead(cell)) return createEastArrowhead(grid, cell, cellXSize, cellYSize); |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
private static DiagramShape createNorthArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 82 |
if(!grid.isNorthArrowhead(cell)) return null; |
| 83 |
DiagramShape shape = new DiagramShape(); |
| 84 |
shape.addToPoints(new ShapePoint( |
| 85 |
Diagram.getCellMidX(cell,cellXSize), |
| 86 |
Diagram.getCellMinY(cell,cellYSize))); |
| 87 |
shape.addToPoints(new ShapePoint( |
| 88 |
Diagram.getCellMinX(cell,cellXSize), |
| 89 |
Diagram.getCellMaxY(cell,cellYSize))); |
| 90 |
shape.addToPoints(new ShapePoint( |
| 91 |
Diagram.getCellMaxX(cell,cellXSize), |
| 92 |
Diagram.getCellMaxY(cell,cellYSize))); |
| 93 |
shape.setClosed(true); |
| 94 |
shape.setFillColor(Color.black); |
| 95 |
shape.setStrokeColor(Color.black); |
| 96 |
shape.setType(TYPE_ARROWHEAD); |
| 97 |
return shape; |
| 98 |
} |
| 99 |
|
| 100 |
private static DiagramShape createSouthArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 101 |
if(!grid.isSouthArrowhead(cell)) return null; |
| 102 |
DiagramShape shape = new DiagramShape(); |
| 103 |
shape.addToPoints(new ShapePoint( |
| 104 |
Diagram.getCellMinX(cell,cellXSize), |
| 105 |
Diagram.getCellMinY(cell,cellYSize))); |
| 106 |
shape.addToPoints(new ShapePoint( |
| 107 |
Diagram.getCellMidX(cell,cellXSize), |
| 108 |
Diagram.getCellMaxY(cell,cellYSize))); |
| 109 |
shape.addToPoints(new ShapePoint( |
| 110 |
Diagram.getCellMaxX(cell,cellXSize), |
| 111 |
Diagram.getCellMinY(cell,cellYSize))); |
| 112 |
shape.setClosed(true); |
| 113 |
shape.setFillColor(Color.black); |
| 114 |
shape.setStrokeColor(Color.black); |
| 115 |
shape.setType(TYPE_ARROWHEAD); |
| 116 |
return shape; |
| 117 |
} |
| 118 |
|
| 119 |
private static DiagramShape createWestArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 120 |
if(!grid.isWestArrowhead(cell)) return null; |
| 121 |
DiagramShape shape = new DiagramShape(); |
| 122 |
shape.addToPoints(new ShapePoint( |
| 123 |
Diagram.getCellMaxX(cell,cellXSize), |
| 124 |
Diagram.getCellMinY(cell,cellYSize))); |
| 125 |
shape.addToPoints(new ShapePoint( |
| 126 |
Diagram.getCellMinX(cell,cellXSize), |
| 127 |
Diagram.getCellMidY(cell,cellYSize))); |
| 128 |
shape.addToPoints(new ShapePoint( |
| 129 |
Diagram.getCellMaxX(cell,cellXSize), |
| 130 |
Diagram.getCellMaxY(cell,cellYSize))); |
| 131 |
shape.setClosed(true); |
| 132 |
shape.setFillColor(Color.black); |
| 133 |
shape.setStrokeColor(Color.black); |
| 134 |
shape.setType(TYPE_ARROWHEAD); |
| 135 |
return shape; |
| 136 |
} |
| 137 |
|
| 138 |
private static DiagramShape createEastArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 139 |
if(!grid.isEastArrowhead(cell)) return null; |
| 140 |
DiagramShape shape = new DiagramShape(); |
| 141 |
shape.addToPoints(new ShapePoint( |
| 142 |
Diagram.getCellMinX(cell,cellXSize), |
| 143 |
Diagram.getCellMinY(cell,cellYSize))); |
| 144 |
shape.addToPoints(new ShapePoint( |
| 145 |
Diagram.getCellMaxX(cell,cellXSize), |
| 146 |
Diagram.getCellMidY(cell,cellYSize))); |
| 147 |
shape.addToPoints(new ShapePoint( |
| 148 |
Diagram.getCellMinX(cell,cellXSize), |
| 149 |
Diagram.getCellMaxY(cell,cellYSize))); |
| 150 |
shape.setClosed(true); |
| 151 |
shape.setFillColor(Color.black); |
| 152 |
shape.setStrokeColor(Color.black); |
| 153 |
shape.setType(TYPE_ARROWHEAD); |
| 154 |
return shape; |
| 155 |
} |
| 156 |
|
| 157 |
public static DiagramShape createSmallLine(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) { |
| 158 |
if (grid.isLine(cell)) { |
| 159 |
DiagramShape shape = new DiagramShape(); |
| 160 |
if (grid.isHorizontalLine(cell)) { |
| 161 |
shape.addToPoints( |
| 162 |
new ShapePoint( |
| 163 |
cell.x * cellXSize, |
| 164 |
cell.y * cellYSize + cellYSize / 2)); |
| 165 |
shape.addToPoints( |
| 166 |
new ShapePoint( |
| 167 |
cell.x * cellXSize + cellXSize - 1, |
| 168 |
cell.y * cellYSize + cellYSize / 2)); |
| 169 |
} else if (grid.isVerticalLine(cell)) { |
| 170 |
shape.addToPoints( |
| 171 |
new ShapePoint( |
| 172 |
cell.x * cellXSize + cellXSize / 2, |
| 173 |
cell.y * cellYSize)); |
| 174 |
shape.addToPoints( |
| 175 |
new ShapePoint( |
| 176 |
cell.x * cellXSize + cellXSize / 2, |
| 177 |
cell.y * cellYSize + cellYSize - 1)); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
return shape; |
| 185 |
} |
| 186 |
return null; |
| 187 |
} |
| 188 |
|
| 189 |
public void addToPoints(ShapePoint point){ |
| 190 |
points.add(point); |
| 191 |
} |
| 192 |
|
| 193 |
public Iterator getPointsIterator(){ |
| 194 |
return points.iterator(); |
| 195 |
} |
| 196 |
|
| 197 |
public void scale(float factor){ |
| 198 |
Iterator it = getPointsIterator(); |
| 199 |
while(it.hasNext()){ |
| 200 |
ShapePoint point = (ShapePoint) it.next(); |
| 201 |
point.x *= factor; |
| 202 |
point.y *= factor; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
public boolean isEmpty(){ |
| 207 |
return points.isEmpty(); |
| 208 |
} |
| 209 |
|
| 210 |
public boolean isFilled(){ |
| 211 |
return (fillColor != null); |
| 212 |
} |
| 213 |
|
| 214 |
public void setIsNotFilled(){ |
| 215 |
fillColor = null; |
| 216 |
} |
| 217 |
|
| 218 |
public boolean isPointLinesEnd(ShapePoint point){ |
| 219 |
if(isClosed()) return false; |
| 220 |
if(point == points.get(0)) return true; |
| 221 |
if(point == points.get(points.size() - 1)) return true; |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
public boolean isRectangle(){ |
| 227 |
if(points.size() != 4) return false; |
| 228 |
ShapePoint p1 = (ShapePoint) points.get(0); |
| 229 |
ShapePoint p2 = (ShapePoint) points.get(1); |
| 230 |
ShapePoint p3 = (ShapePoint) points.get(2); |
| 231 |
ShapePoint p4 = (ShapePoint) points.get(3); |
| 232 |
if(p1.isInLineWith(p2) |
| 233 |
&& p2.isInLineWith(p3) |
| 234 |
&& p3.isInLineWith(p4) |
| 235 |
&& p4.isInLineWith(p1)) return true; |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public boolean isSmallerThan(DiagramShape other){ |
| 248 |
Rectangle bounds = getBounds(); |
| 249 |
Rectangle otherBounds = other.getBounds(); |
| 250 |
|
| 251 |
int area = bounds.height * bounds.width; |
| 252 |
int otherArea = otherBounds.height * otherBounds.width; |
| 253 |
|
| 254 |
if(area < otherArea) { |
| 255 |
return true; |
| 256 |
} |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public Color getFillColor() { |
| 264 |
return fillColor; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public Color getStrokeColor() { |
| 271 |
return strokeColor; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public void setFillColor(Color color) { |
| 278 |
fillColor = color; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public void setStrokeColor(Color color) { |
| 285 |
strokeColor = color; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public boolean isClosed() { |
| 292 |
return isClosed; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public void setClosed(boolean b) { |
| 299 |
isClosed = b; |
| 300 |
} |
| 301 |
|
| 302 |
public void printDebug(){ |
| 303 |
System.out.print("DiagramShape: "); |
| 304 |
System.out.println(points.size()+" points"); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public ArrayList getPoints() { |
| 311 |
return points; |
| 312 |
} |
| 313 |
|
| 314 |
public ShapePoint getPoint(int i) { |
| 315 |
return (ShapePoint) points.get(i); |
| 316 |
} |
| 317 |
|
| 318 |
public void setPoint(int i, ShapePoint point) { |
| 319 |
points.set(i, point); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
public boolean equals(Object object){ |
| 324 |
DiagramShape shape = null; |
| 325 |
if(!(object instanceof DiagramShape)) { return false; } |
| 326 |
else shape = (DiagramShape) object; |
| 327 |
if(getPoints().size() != shape.getPoints().size()) return false; |
| 328 |
|
| 329 |
if(DEBUG) System.out.println("comparing shapes:"); |
| 330 |
|
| 331 |
if(DEBUG) System.out.println("points1: "); |
| 332 |
HashMap points1 = new HashMap(); |
| 333 |
Iterator it = getPointsIterator(); |
| 334 |
while(it.hasNext()){ |
| 335 |
ShapePoint point = (ShapePoint) it.next(); |
| 336 |
points1.put( ""+((int) point.x)+","+((int) point.y), null); |
| 337 |
if(DEBUG) System.out.println(((int) point.x)+", "+((int) point.y)); |
| 338 |
} |
| 339 |
|
| 340 |
if(DEBUG) System.out.println("points2: "); |
| 341 |
HashMap points2 = new HashMap(); |
| 342 |
it = shape.getPointsIterator(); |
| 343 |
while(it.hasNext()){ |
| 344 |
ShapePoint point = (ShapePoint) it.next(); |
| 345 |
points2.put( ""+((int) point.x)+","+((int) point.y), null); |
| 346 |
if(DEBUG) System.out.println(((int) point.x)+", "+((int) point.y)); |
| 347 |
} |
| 348 |
|
| 349 |
it = points1.keySet().iterator(); |
| 350 |
while(it.hasNext()){ |
| 351 |
String key = (String) it.next(); |
| 352 |
if(!points2.containsKey(key)) { |
| 353 |
if (DEBUG) |
| 354 |
System.out.println("\tare not equal"); |
| 355 |
return false; |
| 356 |
} |
| 357 |
} |
| 358 |
if (DEBUG) |
| 359 |
System.out.println("\tare equal"); |
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
public GeneralPath makeIntoPath() { |
| 364 |
int size = getPoints().size(); |
| 365 |
|
| 366 |
if(size < 2) return null; |
| 367 |
|
| 368 |
GeneralPath path = new GeneralPath(); |
| 369 |
ShapePoint point = (ShapePoint) getPoints().get(0); |
| 370 |
path.moveTo((int) point.x, (int) point.y); |
| 371 |
for(int i = 1; i < size; i++){ |
| 372 |
point = (ShapePoint) getPoints().get(i); |
| 373 |
path.lineTo((int) point.x, (int) point.y); |
| 374 |
} |
| 375 |
if(isClosed() && size > 2){ |
| 376 |
path.closePath(); |
| 377 |
} |
| 378 |
return path; |
| 379 |
} |
| 380 |
|
| 381 |
public GeneralPath makeMarkerPath(Diagram diagram){ |
| 382 |
if(points.size() != 1) return null; |
| 383 |
ShapePoint center = (ShapePoint) this.getPoint(0); |
| 384 |
float diameter = |
| 385 |
(float) 0.7 * Math.min(diagram.getCellWidth(), diagram.getCellHeight()); |
| 386 |
return new GeneralPath(new Ellipse2D.Float( |
| 387 |
center.x - diameter/2, |
| 388 |
center.y - diameter/2, |
| 389 |
diameter, |
| 390 |
diameter)); |
| 391 |
} |
| 392 |
|
| 393 |
public Rectangle getBounds(){ |
| 394 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 395 |
return bounds; |
| 396 |
} |
| 397 |
|
| •398 |
public GeneralPath makeIntoRenderPath(Diagram diagram, RenderingOptions options) { |
| 399 |
int size = getPoints().size(); |
| 400 |
|
| 401 |
if(getType() == TYPE_POINT_MARKER){ |
| 402 |
return makeMarkerPath(diagram); |
| 403 |
} |
| 404 |
|
| 405 |
if(getType() == TYPE_DOCUMENT && points.size() == 4){ |
| 406 |
return makeDocumentPath(diagram); |
| 407 |
} |
| 408 |
|
| 409 |
if(getType() == TYPE_STORAGE && points.size() == 4){ |
| 410 |
return makeStoragePath(diagram); |
| 411 |
} |
| 412 |
|
| 413 |
if(getType() == TYPE_IO && points.size() == 4){ |
| •414 |
return makeIOPath(diagram, options); |
| 415 |
} |
| 416 |
|
| 417 |
if(getType() == TYPE_DECISION && points.size() == 4){ |
| 418 |
return makeDecisionPath(diagram); |
| 419 |
} |
| 420 |
|
| 421 |
if(getType() == TYPE_MANUAL_OPERATION && points.size() == 4){ |
| •422 |
return makeTrapezoidPath(diagram, options, true); |
| 423 |
} |
| 424 |
|
| 425 |
if(getType() == TYPE_TRAPEZOID && points.size() == 4){ |
| •426 |
return makeTrapezoidPath(diagram, options, false); |
| 427 |
} |
| 428 |
|
| 429 |
if(getType() == TYPE_ELLIPSE && points.size() == 4){ |
| 430 |
return makeEllipsePath(diagram); |
| 431 |
} |
| 432 |
|
| 433 |
if(size < 2) return null; |
| 434 |
|
| 435 |
GeneralPath path = new GeneralPath(); |
| 436 |
ShapePoint point = (ShapePoint) getPoints().get(0); |
| 437 |
TextGrid.Cell cell = diagram.getCellFor(point); |
| 438 |
|
| 439 |
ShapePoint previous = (ShapePoint) getPoints().get(size - 1); |
| 440 |
ShapePoint next = (ShapePoint) getPoints().get(1); |
| 441 |
ShapePoint entryPoint; |
| 442 |
ShapePoint exitPoint; |
| 443 |
|
| 444 |
if(point.getType() == ShapePoint.TYPE_NORMAL){ |
| 445 |
|
| 446 |
path.moveTo((int) point.x, (int) point.y); |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
} else if(point.getType() == ShapePoint.TYPE_ROUND){ |
| 452 |
entryPoint = getCellEdgePointBetween(point, previous, diagram); |
| 453 |
exitPoint = getCellEdgePointBetween(point, next, diagram); |
| 454 |
path.moveTo(entryPoint.x, entryPoint.y); |
| 455 |
path.quadTo(point.x, point.y, exitPoint.x, exitPoint.y); |
| 456 |
} |
| 457 |
|
| 458 |
for(int i = 1; i < size; i++){ |
| 459 |
previous = point; |
| 460 |
point = (ShapePoint) getPoints().get(i); |
| 461 |
if(i < size - 1) |
| 462 |
next = (ShapePoint) getPoints().get(i + 1); |
| 463 |
else next = (ShapePoint) getPoints().get(0); |
| 464 |
|
| 465 |
cell = diagram.getCellFor(point); |
| 466 |
|
| 467 |
if(point.getType() == ShapePoint.TYPE_NORMAL) |
| 468 |
|
| 469 |
path.lineTo((int) point.x, (int) point.y); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
else if(point.getType() == ShapePoint.TYPE_ROUND){ |
| 475 |
entryPoint = getCellEdgePointBetween(point, previous, diagram); |
| 476 |
exitPoint = getCellEdgePointBetween(point, next, diagram); |
| 477 |
|
| 478 |
path.lineTo(entryPoint.x, entryPoint.y); |
| 479 |
path.quadTo(point.x, point.y, exitPoint.x, exitPoint.y); |
| 480 |
|
| 481 |
if(next.getType() == ShapePoint.TYPE_NORMAL) |
| 482 |
path.lineTo(next.x, next.y); |
| 483 |
else if(next.getType() == ShapePoint.TYPE_ROUND){ |
| 484 |
entryPoint = getCellEdgePointBetween(next, point, diagram); |
| 485 |
path.lineTo(entryPoint.x, entryPoint.y); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
if(isClosed() && size > 2){ |
| 495 |
path.closePath(); |
| 496 |
} |
| 497 |
return path; |
| 498 |
} |
| 499 |
|
| 500 |
public ArrayList getEdges(){ |
| 501 |
ArrayList edges = new ArrayList(); |
| 502 |
if(this.points.size() == 1) return edges; |
| 503 |
int noOfPoints = points.size(); |
| 504 |
for(int i = 0; i < noOfPoints - 1; i++){ |
| 505 |
ShapePoint startPoint = (ShapePoint) points.get(i); |
| 506 |
ShapePoint endPoint = (ShapePoint) points.get(i + 1); |
| 507 |
ShapeEdge edge = new ShapeEdge(startPoint, endPoint, this); |
| 508 |
edges.add(edge); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
if(this.isClosed()){ |
| 513 |
ShapePoint firstPoint = (ShapePoint) points.get(0); |
| 514 |
ShapePoint lastPoint = (ShapePoint) points.get(points.size() - 1); |
| 515 |
ShapeEdge edge = new ShapeEdge(lastPoint, firstPoint, this); |
| 516 |
edges.add(edge); |
| 517 |
} |
| 518 |
return edges; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public ShapePoint getCellEdgePointBetween(ShapePoint pointInCell, ShapePoint otherPoint, Diagram diagram){ |
| 541 |
if(pointInCell == null || otherPoint == null || diagram == null) |
| 542 |
throw new IllegalArgumentException("None of the parameters can be null"); |
| 543 |
if(pointInCell.equals(otherPoint)) |
| 544 |
throw new IllegalArgumentException("The two points cannot be the same"); |
| 545 |
|
| 546 |
ShapePoint result = null; |
| 547 |
TextGrid.Cell cell = diagram.getCellFor(pointInCell); |
| 548 |
|
| 549 |
if(cell == null) |
| 550 |
throw new RuntimeException("Upexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram); |
| 551 |
|
| 552 |
if(otherPoint.isNorthOf(pointInCell)) |
| 553 |
result = new ShapePoint(pointInCell.x, |
| 554 |
diagram.getCellMinY(cell)); |
| 555 |
else if(otherPoint.isSouthOf(pointInCell)) |
| 556 |
result = new ShapePoint(pointInCell.x, |
| 557 |
diagram.getCellMaxY(cell)); |
| 558 |
else if(otherPoint.isWestOf(pointInCell)) |
| 559 |
result = new ShapePoint(diagram.getCellMinX(cell), |
| 560 |
pointInCell.y); |
| 561 |
else if(otherPoint.isEastOf(pointInCell)) |
| 562 |
result = new ShapePoint(diagram.getCellMaxX(cell), |
| 563 |
pointInCell.y); |
| 564 |
|
| 565 |
if(result == null) |
| 566 |
throw new RuntimeException("Upexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram); |
| 567 |
|
| 568 |
|
| 569 |
return result; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public ShapePoint getCellEdgeProjectionPointBetween(ShapePoint pointInCell, ShapePoint otherPoint, Diagram diagram){ |
| 593 |
if(pointInCell == null || otherPoint == null || diagram == null) |
| 594 |
throw new IllegalArgumentException("None of the parameters can be null"); |
| 595 |
if(pointInCell.equals(otherPoint)) |
| 596 |
throw new IllegalArgumentException("The two points cannot be the same: "+pointInCell+" and "+otherPoint+" passed"); |
| 597 |
|
| 598 |
ShapePoint result = null; |
| 599 |
TextGrid.Cell cell = diagram.getCellFor(pointInCell); |
| 600 |
|
| 601 |
if(cell == null) |
| •602 |
throw new RuntimeException("Unexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram); |
| 603 |
|
| 604 |
if(otherPoint.isNorthOf(pointInCell)) |
| 605 |
result = new ShapePoint(pointInCell.x, |
| 606 |
diagram.getCellMaxY(cell)); |
| 607 |
else if(otherPoint.isSouthOf(pointInCell)) |
| 608 |
result = new ShapePoint(pointInCell.x, |
| 609 |
diagram.getCellMinY(cell)); |
| 610 |
else if(otherPoint.isWestOf(pointInCell)) |
| 611 |
result = new ShapePoint(diagram.getCellMaxX(cell), |
| 612 |
pointInCell.y); |
| 613 |
else if(otherPoint.isEastOf(pointInCell)) |
| 614 |
result = new ShapePoint(diagram.getCellMinX(cell), |
| 615 |
pointInCell.y); |
| 616 |
|
| 617 |
if(result == null) |
| •618 |
throw new RuntimeException("Unexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram); |
| 619 |
|
| 620 |
|
| 621 |
return result; |
| 622 |
} |
| 623 |
|
| 624 |
public boolean contains(ShapePoint point){ |
| 625 |
GeneralPath path = makeIntoPath(); |
| 626 |
if(path != null) return path.contains(point); |
| 627 |
return false; |
| 628 |
} |
| 629 |
|
| 630 |
public boolean contains(Rectangle2D rect){ |
| 631 |
GeneralPath path = makeIntoPath(); |
| 632 |
if(path != null) return path.contains(rect); |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
public boolean intersects(Rectangle2D rect){ |
| 637 |
GeneralPath path = makeIntoPath(); |
| 638 |
if(path != null) return path.intersects(rect); |
| 639 |
return false; |
| 640 |
} |
| 641 |
|
| 642 |
public boolean dropsShadow(){ |
| 643 |
return (isClosed() |
| 644 |
&& getType() != DiagramShape.TYPE_ARROWHEAD |
| 645 |
&& getType() != DiagramShape.TYPE_POINT_MARKER |
| 646 |
&& !isStrokeDashed()); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public int getType() { |
| 653 |
return type; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
public void setType(int i) { |
| 660 |
type = i; |
| 661 |
} |
| 662 |
|
| 663 |
public void moveEndsToCellEdges(TextGrid grid, Diagram diagram){ |
| 664 |
if(isClosed()) return; |
| 665 |
|
| 666 |
ShapePoint linesEnd = (ShapePoint) points.get(0); |
| 667 |
ShapePoint nextPoint = (ShapePoint) points.get(1); |
| 668 |
|
| 669 |
ShapePoint projectionPoint = getCellEdgeProjectionPointBetween(linesEnd, nextPoint, diagram); |
| 670 |
|
| 671 |
linesEnd.moveTo(projectionPoint); |
| 672 |
|
| 673 |
linesEnd = (ShapePoint) points.get(points.size() - 1); |
| 674 |
nextPoint = (ShapePoint) points.get(points.size() - 2); |
| 675 |
|
| 676 |
projectionPoint = getCellEdgeProjectionPointBetween(linesEnd, nextPoint, diagram); |
| 677 |
|
| 678 |
linesEnd.moveTo(projectionPoint); |
| 679 |
} |
| 680 |
|
| 681 |
public void connectEndsToAnchors(TextGrid grid, Diagram diagram){ |
| 682 |
if(isClosed()) return; |
| 683 |
|
| 684 |
ShapePoint linesEnd; |
| 685 |
ShapePoint nextPoint; |
| 686 |
|
| 687 |
linesEnd = (ShapePoint) points.get(0); |
| 688 |
nextPoint = (ShapePoint) points.get(1); |
| 689 |
|
| 690 |
connectEndToAnchors(grid, diagram, nextPoint, linesEnd); |
| 691 |
|
| 692 |
linesEnd = (ShapePoint) points.get(points.size() - 1); |
| 693 |
nextPoint = (ShapePoint) points.get(points.size() - 2); |
| 694 |
|
| 695 |
connectEndToAnchors(grid, diagram, nextPoint, linesEnd); |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
private void connectEndToAnchors( |
| 702 |
TextGrid grid, |
| 703 |
Diagram diagram, |
| 704 |
ShapePoint nextPoint, |
| 705 |
ShapePoint linesEnd){ |
| 706 |
|
| 707 |
if(isClosed()) return; |
| 708 |
|
| 709 |
TextGrid.Cell anchorCell; |
| 710 |
anchorCell = getPossibleAnchorCell(linesEnd, nextPoint, diagram); |
| 711 |
|
| 712 |
if(grid.isArrowhead(anchorCell)){ |
| 713 |
linesEnd.x = diagram.getCellMidX(anchorCell); |
| 714 |
linesEnd.y = diagram.getCellMidY(anchorCell); |
| 715 |
linesEnd.setLocked(true); |
| 716 |
} else if (grid.isCorner(anchorCell) || grid.isIntersection(anchorCell)){ |
| 717 |
linesEnd.x = diagram.getCellMidX(anchorCell); |
| 718 |
linesEnd.y = diagram.getCellMidY(anchorCell); |
| 719 |
linesEnd.setLocked(true); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
private static TextGrid.Cell getPossibleAnchorCell( |
| 734 |
ShapePoint linesEnd, |
| 735 |
ShapePoint nextPoint, |
| 736 |
Diagram diagram |
| 737 |
){ |
| 738 |
ShapePoint cellPoint = null; |
| 739 |
|
| 740 |
if(nextPoint.isNorthOf(linesEnd)) |
| 741 |
cellPoint = new ShapePoint(linesEnd.x, linesEnd.y + diagram.getCellHeight()); |
| 742 |
if(nextPoint.isSouthOf(linesEnd)) |
| 743 |
cellPoint = new ShapePoint(linesEnd.x, linesEnd.y - diagram.getCellHeight()); |
| 744 |
if(nextPoint.isWestOf(linesEnd)) |
| 745 |
cellPoint = new ShapePoint(linesEnd.x + diagram.getCellWidth(), linesEnd.y); |
| 746 |
if(nextPoint.isEastOf(linesEnd)) |
| 747 |
cellPoint = new ShapePoint(linesEnd.x - diagram.getCellWidth(), linesEnd.y); |
| 748 |
|
| 749 |
return diagram.getCellFor(cellPoint); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
public String toString(){ |
| 754 |
String s = "DiagramShape, "+points.size()+" points: "; |
| 755 |
Iterator it = getPointsIterator(); |
| 756 |
while(it.hasNext()){ |
| 757 |
ShapePoint point = (ShapePoint) it.next(); |
| 758 |
s += point; |
| 759 |
if(it.hasNext()) s += " "; |
| 760 |
} |
| 761 |
return s; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
public boolean isStrokeDashed() { |
| 768 |
return isStrokeDashed; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public void setStrokeDashed(boolean b) { |
| 775 |
isStrokeDashed = b; |
| 776 |
} |
| 777 |
|
| 778 |
private GeneralPath makeStoragePath(Diagram diagram) { |
| 779 |
if(points.size() != 4) return null; |
| 780 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 781 |
ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY()); |
| 782 |
ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY()); |
| 783 |
ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY()); |
| 784 |
ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY()); |
| 785 |
|
| 786 |
ShapePoint pointMidTop = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMinY()); |
| 787 |
ShapePoint pointMidBottom = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY()); |
| 788 |
|
| 789 |
float diameterX = bounds.width; |
| 790 |
float diameterY = 0.75f * diagram.getCellHeight(); |
| 791 |
|
| 792 |
|
| 793 |
float cpOffsetX = bounds.width / 6; |
| 794 |
float cpOffsetYTop = diagram.getCellHeight() / 2; |
| 795 |
float cpOffsetYBottom = 10 * diagram.getCellHeight() / 14; |
| 796 |
|
| 797 |
|
| 798 |
GeneralPath path = new GeneralPath(); |
| 799 |
|
| 800 |
|
| 801 |
path.moveTo(point1.x, point1.y); |
| 802 |
path.curveTo( |
| 803 |
point1.x + cpOffsetX, point1.y + cpOffsetYTop, |
| 804 |
point2.x - cpOffsetX, point2.y + cpOffsetYTop, |
| 805 |
point2.x, point2.y |
| 806 |
); |
| 807 |
path.curveTo( |
| 808 |
point2.x - cpOffsetX, point2.y - cpOffsetYTop, |
| 809 |
point1.x + cpOffsetX, point1.y - cpOffsetYTop, |
| 810 |
point1.x, point1.y |
| 811 |
); |
| 812 |
|
| 813 |
|
| 814 |
path.moveTo(point1.x, point1.y); |
| 815 |
path.lineTo(point4.x, point4.y); |
| 816 |
|
| 817 |
path.curveTo( |
| 818 |
point4.x + cpOffsetX, point4.y + cpOffsetYBottom, |
| 819 |
point3.x - cpOffsetX, point3.y + cpOffsetYBottom, |
| 820 |
point3.x, point3.y |
| 821 |
); |
| 822 |
|
| 823 |
path.lineTo(point2.x, point2.y); |
| 824 |
|
| 825 |
return path; |
| 826 |
} |
| 827 |
|
| 828 |
private GeneralPath makeDocumentPath(Diagram diagram) { |
| 829 |
if(points.size() != 4) return null; |
| 830 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 831 |
ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY()); |
| 832 |
ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY()); |
| 833 |
ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY()); |
| 834 |
ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY()); |
| 835 |
|
| 836 |
ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY()); |
| 837 |
|
| 838 |
GeneralPath path = new GeneralPath(); |
| 839 |
path.moveTo(point1.x, point1.y); |
| 840 |
path.lineTo(point2.x, point2.y); |
| 841 |
path.lineTo(point3.x, point3.y); |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
int controlDX = bounds.width / 6; |
| 847 |
int controlDY = bounds.height / 8; |
| 848 |
|
| 849 |
path.quadTo(pointMid.x + controlDX, pointMid.y - controlDY, pointMid.x, pointMid.y); |
| 850 |
path.quadTo(pointMid.x - controlDX, pointMid.y + controlDY, point4.x, point4.y); |
| 851 |
path.closePath(); |
| 852 |
|
| 853 |
return path; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
private static final float KAPPA = 4f * ((float) Math.sqrt(2) - 1) / 3f; |
| 860 |
|
| 861 |
private GeneralPath makeEllipsePath(Diagram diagram) { |
| 862 |
if(points.size() != 4) return null; |
| 863 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 864 |
float xOff = (float) bounds.getWidth() * 0.5f * KAPPA; |
| 865 |
float yOff = (float) bounds.getHeight() * 0.5f * KAPPA; |
| 866 |
ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY()); |
| 867 |
|
| 868 |
ShapePoint left = new ShapePoint((float)bounds.getMinX(), (float)pointMid.getY()); |
| 869 |
ShapePoint right = new ShapePoint((float)bounds.getMaxX(), (float)pointMid.getY()); |
| 870 |
ShapePoint top = new ShapePoint((float)pointMid.getX(), (float)bounds.getMinY()); |
| 871 |
ShapePoint bottom = new ShapePoint((float)pointMid.getX(), (float)bounds.getMaxY()); |
| 872 |
|
| 873 |
GeneralPath path = new GeneralPath(); |
| 874 |
path.moveTo(top.x, top.y); |
| 875 |
path.curveTo(top.x + xOff, top.y, right.x, right.y - yOff, right.x, right.y); |
| 876 |
path.curveTo(right.x, right.y + yOff, bottom.x + xOff, bottom.y, bottom.x, bottom.y); |
| 877 |
path.curveTo(bottom.x - xOff, bottom.y, left.x, left.y + yOff, left.x, left.y); |
| 878 |
path.curveTo(left.x, left.y - yOff, top.x - xOff, top.y, top.x, top.y); |
| 879 |
path.closePath(); |
| 880 |
|
| 881 |
return path; |
| 882 |
} |
| 883 |
|
| •884 |
private GeneralPath makeTrapezoidPath(Diagram diagram, RenderingOptions options, boolean inverted) { |
| 885 |
if(points.size() != 4) return null; |
| 886 |
Rectangle bounds = makeIntoPath().getBounds(); |
| •887 |
float offset = options.isFixedSlope() ? bounds.height / SHAPE_SLOPE : diagram.getCellWidth() * 0.5f; |
| 888 |
if (inverted) offset = -offset; |
| 889 |
ShapePoint ul = new ShapePoint((float)bounds.getMinX() + offset, (float)bounds.getMinY()); |
| 890 |
ShapePoint ur = new ShapePoint((float)bounds.getMaxX() - offset, (float)bounds.getMinY()); |
| 891 |
ShapePoint br = new ShapePoint((float)bounds.getMaxX() + offset, (float)bounds.getMaxY()); |
| 892 |
ShapePoint bl = new ShapePoint((float)bounds.getMinX() - offset, (float)bounds.getMaxY()); |
| 893 |
|
| 894 |
ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY()); |
| 895 |
|
| 896 |
GeneralPath path = new GeneralPath(); |
| 897 |
path.moveTo(ul.x, ul.y); |
| 898 |
path.lineTo(ur.x, ur.y); |
| 899 |
path.lineTo(br.x, br.y); |
| 900 |
path.lineTo(bl.x, bl.y); |
| 901 |
path.closePath(); |
| 902 |
|
| 903 |
return path; |
| 904 |
} |
| 905 |
|
| 906 |
private GeneralPath makeDecisionPath(Diagram diagram) { |
| 907 |
if(points.size() != 4) return null; |
| 908 |
Rectangle bounds = makeIntoPath().getBounds(); |
| •909 |
ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY()); |
| 910 |
ShapePoint left = new ShapePoint((float)bounds.getMinX(), (float)pointMid.getY()); |
| 911 |
ShapePoint right = new ShapePoint((float)bounds.getMaxX(), (float)pointMid.getY()); |
| 912 |
ShapePoint top = new ShapePoint((float)pointMid.getX(), (float)bounds.getMinY()); |
| 913 |
ShapePoint bottom = new ShapePoint((float)pointMid.getX(), (float)bounds.getMaxY()); |
| 914 |
|
| 915 |
GeneralPath path = new GeneralPath(); |
| 916 |
path.moveTo(left.x, left.y); |
| 917 |
path.lineTo(top.x, top.y); |
| 918 |
path.lineTo(right.x, right.y); |
| 919 |
path.lineTo(bottom.x, bottom.y); |
| 920 |
|
| 921 |
path.closePath(); |
| 922 |
|
| 923 |
return path; |
| 924 |
} |
| 925 |
|
| •926 |
private GeneralPath makeIOPath(Diagram diagram, RenderingOptions options) { |
| 927 |
if(points.size() != 4) return null; |
| 928 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 929 |
ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY()); |
| 930 |
ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY()); |
| 931 |
ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY()); |
| 932 |
ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY()); |
| 933 |
|
| •934 |
float offset = options.isFixedSlope() ? bounds.height / SHAPE_SLOPE : diagram.getCellWidth() * 0.5f; |
| 935 |
|
| 936 |
GeneralPath path = new GeneralPath(); |
| 937 |
path.moveTo(point1.x + offset, point1.y); |
| 938 |
path.lineTo(point2.x + offset, point2.y); |
| 939 |
path.lineTo(point3.x - offset, point3.y); |
| 940 |
path.lineTo(point4.x - offset, point4.y); |
| 941 |
path.closePath(); |
| 942 |
|
| 943 |
return path; |
| 944 |
} |
| 945 |
|
| 946 |
public CustomShapeDefinition getDefinition() { |
| 947 |
return definition; |
| 948 |
} |
| 949 |
|
| 950 |
public void setDefinition(CustomShapeDefinition definition) { |
| 951 |
this.definition = definition; |
| 952 |
} |
| 953 |
|
| •954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
public double calculateArea() { |
| 960 |
if(points.size() == 0) return 0; |
| 961 |
|
| 962 |
double area = 0; |
| 963 |
|
| 964 |
for(int i = 0; i < points.size() - 1; i++){ |
| 965 |
ShapePoint point1 = points.get(i); |
| 966 |
ShapePoint point2 = points.get(i + 1); |
| 967 |
area += point1.x * point2.y; |
| 968 |
area -= point2.x * point1.y; |
| 969 |
} |
| 970 |
ShapePoint point1 = points.get(points.size() - 1); |
| 971 |
ShapePoint point2 = points.get(0); |
| 972 |
area += point1.x * point2.y; |
| 973 |
area -= point2.x * point1.y; |
| 974 |
|
| 975 |
return Math.abs(area / 2); |
| 976 |
} |
| 977 |
|
| 978 |
} |