| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
import java.awt.*; |
|---|
| 34 |
import java.awt.event.*; |
|---|
| 35 |
import java.awt.geom.*; |
|---|
| 36 |
import java.awt.image.*; |
|---|
| 37 |
import java.util.*; |
|---|
| 38 |
import javax.swing.*; |
|---|
| 39 |
import java.math.*; |
|---|
| 40 |
|
|---|
| 41 |
class JGraphViewer extends JPanel implements GraphChangeListener { |
|---|
| 42 |
|
|---|
| 43 |
public JGraphViewer(Graph graph, double scaleX, double scaleY, BackgroundMap backgroundMap) { |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
super(new BorderLayout()); |
|---|
| 54 |
|
|---|
| 55 |
add(scrollPane); |
|---|
| 56 |
|
|---|
| 57 |
scale = AffineTransform.getScaleInstance(scaleX, scaleY); |
|---|
| 58 |
setGraph(graph); |
|---|
| 59 |
setBackgroundMap(backgroundMap); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
public void graphChanged() { |
|---|
| 92 |
graphRectangle = null; |
|---|
| 93 |
objectsToPainters = null; |
|---|
| 94 |
paintersToObjectLists = null; |
|---|
| 95 |
|
|---|
| 96 |
drawingPane.repaint(); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
public void addSpace(double top, double bottom, double left, double right) { |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
} |
|---|
| 108 |
public void setSpace(double top, double bottom, double left, double right) { |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
public Graph getGraph() { return graph; } |
|---|
| 118 |
public void setGraph(Graph graph) { |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
scrollPane.getViewport().setViewPosition(new Point(0, 0)); |
|---|
| 122 |
|
|---|
| 123 |
clearAllSpecialNodes(); |
|---|
| 124 |
this.graph = graph; |
|---|
| 125 |
if(graph != null) graph.addGraphChangeListener(this); |
|---|
| 126 |
graphChanged(); |
|---|
| 127 |
computeAndChangePreferredSize(); |
|---|
| 128 |
drawingPane.repaint(); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
public double getScaleX() { return scale.getScaleX(); } |
|---|
| 133 |
public double getScaleY() { return scale.getScaleY(); } |
|---|
| 134 |
public void setScale(double scaleX, double scaleY) { |
|---|
| 135 |
Point2D.Double center = getVisibleRegionCenter(); |
|---|
| 136 |
scale = AffineTransform.getScaleInstance(scaleX, scaleY); |
|---|
| 137 |
realToScreen = null; |
|---|
| 138 |
setVisibleRegionCenter(center); |
|---|
| 139 |
drawingPane.repaint(); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
public void addPointListener(PointListener pointListener) { |
|---|
| 144 |
pointListeners.add(pointListener); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
public Collection getPointListeners() { return pointListeners; } |
|---|
| 148 |
public void removePointListener(PointListener pointListener) { |
|---|
| 149 |
pointListeners.remove(pointListener); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
public void setBackground(Color bg) { |
|---|
| 155 |
if(drawingPane != null) drawingPane.setBackground(bg); |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
public Path getPath() { return path; } |
|---|
| 160 |
public void setPath(Path path) { |
|---|
| 161 |
|
|---|
| 162 |
this.path = path; |
|---|
| 163 |
objectsToPainters = null; |
|---|
| 164 |
drawingPane.repaint(); |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
public Object getSelectedObject() { return selectedObject; } |
|---|
| 168 |
public void setSelectedObject(Object object) { |
|---|
| 169 |
selectedObject = object; |
|---|
| 170 |
objectsToPainters = null; |
|---|
| 171 |
drawingPane.repaint(); |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
public BackgroundMap getBackgroundMap() { return backgroundMap; } |
|---|
| 175 |
|
|---|
| 176 |
public void setBackgroundMap(BackgroundMap backgroundMap) { |
|---|
| 177 |
Point2D.Double center = getVisibleRegionCenter(); |
|---|
| 178 |
this.backgroundMap = backgroundMap; |
|---|
| 179 |
graphRectangle = null; |
|---|
| 180 |
setVisibleRegionCenter(center); |
|---|
| 181 |
drawingPane.repaint(); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
public void addCustomObjectPainter(Object object, int zOrder, ObjectPainter painter) { |
|---|
| 187 |
objectsToCustomPainters[zOrder].put(object, painter); |
|---|
| 188 |
objectsToPainters = null; |
|---|
| 189 |
drawingPane.repaint(); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
public void clearCustomObjectPainters() { |
|---|
| 193 |
for(int i = 0; i < objectsToCustomPainters.length; i++) { |
|---|
| 194 |
objectsToCustomPainters[i].clear(); |
|---|
| 195 |
} |
|---|
| 196 |
objectsToPainters = null; |
|---|
| 197 |
drawingPane.repaint(); |
|---|
| 198 |
} |
|---|
| 199 |
public void clearAllSpecialNodes() { |
|---|
| 200 |
setPath(null); |
|---|
| 201 |
setSelectedObject(null); |
|---|
| 202 |
clearCustomObjectPainters(); |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
public PhysicalNodePainter getDefaultNodePainter() { return defaultNodePainter; } |
|---|
| 206 |
public void setDefaultNodePainter(PhysicalNodePainter painter) { |
|---|
| 207 |
defaultNodePainter = painter; |
|---|
| 208 |
objectsToPainters = null; |
|---|
| 209 |
drawingPane.repaint(); |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
public PhysicalEdgePainter getDefaultEdgePainter() { return defaultEdgePainter; } |
|---|
| 213 |
public void setDefaultEdgePainter(PhysicalEdgePainter painter) { |
|---|
| 214 |
defaultEdgePainter = painter; |
|---|
| 215 |
objectsToPainters = null; |
|---|
| 216 |
drawingPane.repaint(); |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
public void setPathNodePainter(PhysicalNodePainter painter) { |
|---|
| 220 |
pathNodePainter = painter; |
|---|
| 221 |
objectsToPainters = null; |
|---|
| 222 |
drawingPane.repaint(); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
public void setPathEdgePainter(PhysicalEdgePainter painter) { |
|---|
| 226 |
pathEdgePainter = painter; |
|---|
| 227 |
objectsToPainters = null; |
|---|
| 228 |
drawingPane.repaint(); |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
public void setSelectedNodePainter(PhysicalNodePainter painter) { |
|---|
| 232 |
selectedNodePainter = painter; |
|---|
| 233 |
objectsToPainters = null; |
|---|
| 234 |
drawingPane.repaint(); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
public void setSelectedEdgePainter(PhysicalEdgePainter painter) { |
|---|
| 238 |
selectedEdgePainter = painter; |
|---|
| 239 |
objectsToPainters = null; |
|---|
| 240 |
drawingPane.repaint(); |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
public Object closestMatch(Point2D.Double realPoint, double maxDistance) { |
|---|
| 247 |
updateRealToScreen(); |
|---|
| 248 |
updateObjectsToPainters(); |
|---|
| 249 |
|
|---|
| 250 |
Object closestMatch = null; |
|---|
| 251 |
|
|---|
| 252 |
Iterator iterator = objectsToPainters.entrySet().iterator(); |
|---|
| 253 |
while(iterator.hasNext()) { |
|---|
| 254 |
Map.Entry entry = (Map.Entry)iterator.next(); |
|---|
| 255 |
ObjectPainter painter = (ObjectPainter)entry.getValue(); |
|---|
| 256 |
Object object = entry.getKey(); |
|---|
| 257 |
|
|---|
| 258 |
double distance = painter.visualDistanceTo(object, realPoint, backgroundMap, realToScreen); |
|---|
| 259 |
|
|---|
| 260 |
if(distance < maxDistance) { |
|---|
| 261 |
closestMatch = object; |
|---|
| 262 |
} |
|---|
| 263 |
} |
|---|
| 264 |
return closestMatch; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
private boolean updateObjectsToPainters() { |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
if(objectsToPainters != null) |
|---|
| 289 |
return true; |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
if(graph == null) { |
|---|
| 296 |
objectsToPainters = new LinkedHashMap(); |
|---|
| 297 |
return false; |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
objectsToPainters = new LinkedHashMap(graph.getNodes().size() + graph.getEdges().size()); |
|---|
| 301 |
|
|---|
| 302 |
Iterator iterator; |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
if(defaultEdgePainter != null) { |
|---|
| 306 |
iterator = graph.getEdges().iterator(); |
|---|
| 307 |
while(iterator.hasNext()) { |
|---|
| 308 |
Edge edge = (Edge)iterator.next(); |
|---|
| 309 |
if(!(edge instanceof PhysicalEdge)) continue; |
|---|
| 310 |
objectsToPainters.put(edge, defaultEdgePainter); |
|---|
| 311 |
} |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
if(defaultNodePainter != null) { |
|---|
| 316 |
iterator = graph.getNodes().iterator(); |
|---|
| 317 |
while(iterator.hasNext()) { |
|---|
| 318 |
Node node = (Node)iterator.next(); |
|---|
| 319 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 320 |
objectsToPainters.put(node, defaultNodePainter); |
|---|
| 321 |
} |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
objectsToPainters.putAll(objectsToCustomPainters[2]); |
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
if(path != null) { |
|---|
| 330 |
|
|---|
| 331 |
if(pathEdgePainter != null) { |
|---|
| 332 |
iterator = path.edges.iterator(); |
|---|
| 333 |
while(iterator.hasNext()) { |
|---|
| 334 |
Edge edge = (Edge)iterator.next(); |
|---|
| 335 |
if(!(edge instanceof PhysicalEdge)) continue; |
|---|
| 336 |
objectsToPainters.put(edge, pathEdgePainter); |
|---|
| 337 |
} |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
if(pathNodePainter != null) { |
|---|
| 342 |
iterator = path.nodes.iterator(); |
|---|
| 343 |
while(iterator.hasNext()) { |
|---|
| 344 |
Node node = (Node)iterator.next(); |
|---|
| 345 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 346 |
objectsToPainters.put(node, pathNodePainter); |
|---|
| 347 |
} |
|---|
| 348 |
} |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
objectsToPainters.putAll(objectsToCustomPainters[1]); |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
if(selectedObject instanceof PhysicalEdge) { |
|---|
| 356 |
objectsToPainters.put(selectedObject, selectedEdgePainter); |
|---|
| 357 |
} else if(selectedObject instanceof PhysicalNode) { |
|---|
| 358 |
objectsToPainters.put(selectedObject, selectedNodePainter); |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
objectsToPainters.putAll(objectsToCustomPainters[0]); |
|---|
| 362 |
|
|---|
| 363 |
paintersToObjectLists = null; |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
return false; |
|---|
| 367 |
} |
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
private boolean updatePaintersToObjectLists() { |
|---|
| 378 |
|
|---|
| 379 |
boolean upToDate = true; |
|---|
| 380 |
upToDate &= updateObjectsToPainters(); |
|---|
| 381 |
upToDate &= (paintersToObjectLists != null); |
|---|
| 382 |
if(upToDate) return true; |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
paintersToObjectLists = new LinkedHashMap(); |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
Iterator iterator = objectsToPainters.entrySet().iterator(); |
|---|
| 391 |
while(iterator.hasNext()) { |
|---|
| 392 |
Map.Entry entry = (Map.Entry)iterator.next(); |
|---|
| 393 |
|
|---|
| 394 |
ObjectPainter painter = (ObjectPainter)entry.getValue(); |
|---|
| 395 |
|
|---|
| 396 |
LinkedList list = (LinkedList)paintersToObjectLists.get(painter); |
|---|
| 397 |
if(list == null) { |
|---|
| 398 |
list = new LinkedList(); |
|---|
| 399 |
paintersToObjectLists.put(painter, list); |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
list.add(entry.getKey()); |
|---|
| 403 |
} |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
return false; |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
private boolean updateGraphRectangle() { |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
boolean upToDate = true; |
|---|
| 418 |
upToDate &= (graphRectangle != null); |
|---|
| 419 |
if(upToDate) return true; |
|---|
| 420 |
|
|---|
| 421 |
graphRectangle = new Rectangle2D.Double(); |
|---|
| 422 |
if(backgroundMap != null) |
|---|
| 423 |
graphRectangle.setRect(backgroundMap.getRealBoundingRectangle()); |
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
return false; |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
private boolean updateRealToScreen() { |
|---|
| 435 |
|
|---|
| 436 |
boolean upToDate = true; |
|---|
| 437 |
upToDate &= updateGraphRectangle(); |
|---|
| 438 |
upToDate &= (realToScreen != null); |
|---|
| 439 |
|
|---|
| 440 |
if(upToDate) return true; |
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
computeAndChangePreferredSize(); |
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
Point2D.Double corners[] = { new Point2D.Double(graphRectangle.x, graphRectangle.y), |
|---|
| 448 |
new Point2D.Double(graphRectangle.x + graphRectangle.width, |
|---|
| 449 |
graphRectangle.y + graphRectangle.height)}; |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
scale.transform(corners, 0, corners, 0, 2); |
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
double minX = Math.min(corners[0].x, corners[1].x); |
|---|
| 458 |
double minY = Math.min(corners[0].y, corners[1].y); |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
realToScreen = AffineTransform.getTranslateInstance((double)(int)-minX, (double)(int)-minY); |
|---|
| 464 |
realToScreen.concatenate(scale); |
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
return false; |
|---|
| 468 |
} |
|---|
| 469 |
|
|---|
| 470 |
private void computeAndChangePreferredSize() { |
|---|
| 471 |
if(dontComputeAndChangePreferredSize) return; |
|---|
| 472 |
|
|---|
| 473 |
updateGraphRectangle(); |
|---|
| 474 |
|
|---|
| 475 |
|
|---|
| 476 |
drawingPane.setPreferredSize(new Dimension( |
|---|
| 477 |
(int)(graphRectangle.width * scale.getScaleX() + 1), |
|---|
| 478 |
(int)(graphRectangle.height * scale.getScaleY() + 1) |
|---|
| 479 |
)); |
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
Dimension viewportSize = ((JViewport)drawingPane.getParent()).getSize(); |
|---|
| 484 |
|
|---|
| 485 |
Dimension preferredSize = drawingPane.getPreferredSize(); |
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
scrollPane.getHorizontalScrollBar().setUnitIncrement( |
|---|
| 490 |
Math.min( |
|---|
| 491 |
Math.max(1, (preferredSize.width - viewportSize.width) / 30), |
|---|
| 492 |
Math.max(1, viewportSize.width / 15) |
|---|
| 493 |
) |
|---|
| 494 |
); |
|---|
| 495 |
scrollPane.getVerticalScrollBar().setUnitIncrement( |
|---|
| 496 |
Math.min( |
|---|
| 497 |
Math.max(1, (preferredSize.height - viewportSize.height) / 30), |
|---|
| 498 |
Math.max(1, viewportSize.height / 15) |
|---|
| 499 |
) |
|---|
| 500 |
); |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
} |
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
private class DrawingPane extends JPanel implements MouseListener, MouseMotionListener { |
|---|
| 514 |
public DrawingPane() { |
|---|
| 515 |
addMouseListener(this); |
|---|
| 516 |
addMouseMotionListener(this); |
|---|
| 517 |
} |
|---|
| 518 |
|
|---|
| 519 |
protected void paintComponent(Graphics graphics) { |
|---|
| 520 |
super.paintComponent(graphics); |
|---|
| 521 |
if(dontPaint) { |
|---|
| 522 |
System.out.println("superfluous paint, please report"); |
|---|
| 523 |
} |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
Graphics2D g = (Graphics2D)graphics.create(); |
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 |
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, |
|---|
| 533 |
RenderingHints.VALUE_STROKE_PURE); |
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
if(!highQuality || |
|---|
| 538 |
scrollPane.getHorizontalScrollBar().getValueIsAdjusting() || |
|---|
| 539 |
scrollPane.getVerticalScrollBar().getValueIsAdjusting() || |
|---|
| 540 |
temporaryLowQuality) { |
|---|
| 541 |
|
|---|
| 542 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
|---|
| 543 |
RenderingHints.VALUE_ANTIALIAS_OFF); |
|---|
| 544 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
|---|
| 545 |
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); |
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
if(highQuality) |
|---|
| 549 |
goodRepainter.scheduleGoodRepaint(); |
|---|
| 550 |
} else { |
|---|
| 551 |
|
|---|
| 552 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
|---|
| 553 |
RenderingHints.VALUE_ANTIALIAS_ON); |
|---|
| 554 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
|---|
| 555 |
RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
|---|
| 556 |
} |
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
updatePaintersToObjectLists(); |
|---|
| 560 |
updateRealToScreen(); |
|---|
| 561 |
|
|---|
| 562 |
if(backgroundMap != null) { |
|---|
| 563 |
if(backgroundMap instanceof TiledMap) { |
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
backgroundMap.paint(g, realToScreen, new ImageObserver() { |
|---|
| 569 |
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { |
|---|
| 570 |
if((infoflags & ImageObserver.ALLBITS) != 0) { |
|---|
| 571 |
System.out.println("the image is loaded"); |
|---|
| 572 |
SwingUtilities.invokeLater(new Runnable() { |
|---|
| 573 |
public void run() { |
|---|
| 574 |
repaint(); |
|---|
| 575 |
} |
|---|
| 576 |
}); |
|---|
| 577 |
return false; |
|---|
| 578 |
} else { |
|---|
| 579 |
return true; |
|---|
| 580 |
} |
|---|
| 581 |
} |
|---|
| 582 |
}); |
|---|
| 583 |
} else { |
|---|
| 584 |
backgroundMap.paint(g, realToScreen, this); |
|---|
| 585 |
} |
|---|
| 586 |
} |
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
Iterator iterator = paintersToObjectLists.entrySet().iterator(); |
|---|
| 590 |
while(iterator.hasNext()) { |
|---|
| 591 |
Map.Entry entry = (Map.Entry)iterator.next(); |
|---|
| 592 |
((ObjectPainter)entry.getKey()).paintObjects( |
|---|
| 593 |
((Collection)entry.getValue()), realToScreen, backgroundMap, g |
|---|
| 594 |
); |
|---|
| 595 |
} |
|---|
| 596 |
} |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
public class GoodRepainter { |
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
public void scheduleGoodRepaint() { |
|---|
| 604 |
if(needRepaint == false) { |
|---|
| 605 |
needRepaint = true; |
|---|
| 606 |
(new Thread(new Runnable() { |
|---|
| 607 |
public void run() { |
|---|
| 608 |
while(scrollPane.getHorizontalScrollBar().getValueIsAdjusting() || |
|---|
| 609 |
scrollPane.getVerticalScrollBar().getValueIsAdjusting() || |
|---|
| 610 |
temporaryLowQuality) { |
|---|
| 611 |
try { |
|---|
| 612 |
Thread.sleep(25); |
|---|
| 613 |
} catch(InterruptedException e) {} |
|---|
| 614 |
} |
|---|
| 615 |
SwingUtilities.invokeLater(new Runnable() { |
|---|
| 616 |
public void run() { |
|---|
| 617 |
repaint(); |
|---|
| 618 |
needRepaint = false; |
|---|
| 619 |
} |
|---|
| 620 |
}); |
|---|
| 621 |
} |
|---|
| 622 |
}, "GoodRepainter")).start(); |
|---|
| 623 |
} |
|---|
| 624 |
} |
|---|
| 625 |
private boolean needRepaint; |
|---|
| 626 |
} |
|---|
| 627 |
|
|---|
| 628 |
public void setPreferredSize(Dimension preferredSize) { |
|---|
| 629 |
super.setPreferredSize(preferredSize); |
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
revalidate(); |
|---|
| 633 |
} |
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
public void mouseClicked (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 644 |
public void mouseEntered (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 645 |
public void mouseExited (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 646 |
public void mousePressed (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 647 |
public void mouseReleased(MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 648 |
|
|---|
| 649 |
public void mouseDragged (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 650 |
public void mouseMoved (MouseEvent event) { dispatchMouseEvent(event); } |
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
private void dispatchMouseEvent(MouseEvent event) { |
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
if(realToScreen == null) return; |
|---|
| 665 |
try { |
|---|
| 666 |
|
|---|
| 667 |
Point2D.Double screenPoint = new Point2D.Double(event.getX(), event.getY()); |
|---|
| 668 |
Point2D.Double realPoint = new Point2D.Double(); |
|---|
| 669 |
realToScreen.inverseTransform(screenPoint, realPoint); |
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
Iterator iterator = pointListeners.iterator(); |
|---|
| 673 |
while(iterator.hasNext()) { |
|---|
| 674 |
PointListener pointListener = (PointListener)iterator.next(); |
|---|
| 675 |
pointListener.pointReceived(event, realPoint); |
|---|
| 676 |
} |
|---|
| 677 |
} catch(NoninvertibleTransformException exception) { |
|---|
| 678 |
|
|---|
| 679 |
System.out.println("Couldn't inverse transform the point"); |
|---|
| 680 |
} |
|---|
| 681 |
} |
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
private GoodRepainter goodRepainter = new GoodRepainter(); |
|---|
| 685 |
}; |
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
public void scaleRelative(double zoom) { |
|---|
| 689 |
System.out.println("scaleRelative: " + zoom + " " + temporaryLowQuality); |
|---|
| 690 |
if(backgroundMap == null) return; |
|---|
| 691 |
double fudge = 1; |
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
Dimension currentVisibleSize = scrollPane.getViewport().getExtentSize(); |
|---|
| 695 |
|
|---|
| 696 |
updateGraphRectangle(); |
|---|
| 697 |
|
|---|
| 698 |
double zeroScale = Math.min( |
|---|
| 699 |
currentVisibleSize.width * fudge / graphRectangle.width, |
|---|
| 700 |
currentVisibleSize.height * fudge / graphRectangle.height |
|---|
| 701 |
); |
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
double oneScale = backgroundMap.getPixelForPixelScale(); |
|---|
| 705 |
double scale; |
|---|
| 706 |
if(zoom <= 1) |
|---|
| 707 |
scale = (oneScale - zeroScale) * zoom + zeroScale; |
|---|
| 708 |
else |
|---|
| 709 |
scale = oneScale * zoom; |
|---|
| 710 |
|
|---|
| 711 |
setScale(scale, scale); |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
public Point2D.Double getVisibleRegionCenter() { |
|---|
| 715 |
updateRealToScreen(); |
|---|
| 716 |
|
|---|
| 717 |
Rectangle rect = scrollPane.getViewport().getViewRect(); |
|---|
| 718 |
Point2D.Double point = new Point2D.Double( |
|---|
| 719 |
rect.x + rect.width / 2, |
|---|
| 720 |
rect.y + rect.height / 2 |
|---|
| 721 |
); |
|---|
| 722 |
|
|---|
| 723 |
try { |
|---|
| 724 |
realToScreen.inverseTransform(point, point); |
|---|
| 725 |
} catch(NoninvertibleTransformException e) { |
|---|
| 726 |
System.out.println("Couldn't invert the transform."); |
|---|
| 727 |
} |
|---|
| 728 |
|
|---|
| 729 |
return point; |
|---|
| 730 |
} |
|---|
| 731 |
|
|---|
| 732 |
public void setVisibleRegionCenter(Point2D.Double newCenter) { |
|---|
| 733 |
dontComputeAndChangePreferredSize = true; |
|---|
| 734 |
updateRealToScreen(); |
|---|
| 735 |
dontComputeAndChangePreferredSize = false; |
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
Point screenPoint = new Point(); |
|---|
| 739 |
realToScreen.transform(newCenter, screenPoint); |
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
JViewport viewport = scrollPane.getViewport(); |
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
Rectangle rect = viewport.getViewRect(); |
|---|
| 748 |
|
|---|
| 749 |
int oldScrollMode = viewport.getScrollMode(); |
|---|
| 750 |
viewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE); |
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
dontPaint = true; |
|---|
| 754 |
computeAndChangePreferredSize(); |
|---|
| 755 |
dontPaint = false; |
|---|
| 756 |
viewport.setViewPosition(new Point( |
|---|
| 757 |
(int)(screenPoint.x - rect.width / 2), |
|---|
| 758 |
(int)(screenPoint.y - rect.height / 2) |
|---|
| 759 |
)); |
|---|
| 760 |
|
|---|
| 761 |
viewport.setScrollMode(oldScrollMode); |
|---|
| 762 |
} |
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
public void setHighQuality(boolean b) { highQuality = b; } |
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
public void setTemporaryLowQuality(boolean b) { temporaryLowQuality = b; } |
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
private Graph graph; |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
private Rectangle2D.Double graphRectangle; |
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
private AffineTransform scale; |
|---|
| 779 |
|
|---|
| 780 |
private AffineTransform realToScreen; |
|---|
| 781 |
|
|---|
| 782 |
|
|---|
| 783 |
private DrawingPane drawingPane = new DrawingPane(); |
|---|
| 784 |
|
|---|
| 785 |
private JScrollPane scrollPane = new JScrollPane(drawingPane); |
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
private Map objectsToPainters; |
|---|
| 792 |
private Map paintersToObjectLists; |
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
private PhysicalNodePainter defaultNodePainter = new PhysicalNodePainter(Color.BLACK, 3); |
|---|
| 796 |
private PhysicalEdgePainter defaultEdgePainter = new PhysicalEdgePainter(Color.BLACK, (float)1.5); |
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
private Path path; |
|---|
| 800 |
|
|---|
| 801 |
private PhysicalNodePainter pathNodePainter = new PhysicalNodePainter(Color.BLUE, 5); |
|---|
| 802 |
private PhysicalEdgePainter pathEdgePainter = new PhysicalEdgePainter(Color.BLUE, 5); |
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
private Object selectedObject; |
|---|
| 806 |
|
|---|
| 807 |
private PhysicalNodePainter selectedNodePainter = new PhysicalNodePainter(Color.RED, 7); |
|---|
| 808 |
private PhysicalEdgePainter selectedEdgePainter = new PhysicalEdgePainter(Color.RED, 7); |
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
private Map objectsToCustomPainters [] = {new HashMap(), new HashMap(), new HashMap()}; |
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
private Collection pointListeners = new LinkedList(); |
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
private BackgroundMap backgroundMap; |
|---|
| 818 |
|
|---|
| 819 |
private boolean dontComputeAndChangePreferredSize = false; |
|---|
| 820 |
private boolean dontPaint = false; |
|---|
| 821 |
|
|---|
| 822 |
private boolean highQuality = true; |
|---|
| 823 |
private boolean temporaryLowQuality = false; |
|---|
| 824 |
} |
|---|