No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicDrawing.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. package org.insa.graphics.drawing.components;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.geom.AffineTransform;
  12. import java.awt.geom.NoninvertibleTransformException;
  13. import java.awt.geom.Point2D;
  14. import java.awt.image.BufferedImage;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import javax.swing.JPanel;
  21. import org.insa.graph.Arc;
  22. import org.insa.graph.Graph;
  23. import org.insa.graph.GraphStatistics.BoundingBox;
  24. import org.insa.graph.Node;
  25. import org.insa.graph.Path;
  26. import org.insa.graph.Point;
  27. import org.insa.graphics.drawing.BasicGraphPalette;
  28. import org.insa.graphics.drawing.Drawing;
  29. import org.insa.graphics.drawing.DrawingClickListener;
  30. import org.insa.graphics.drawing.GraphPalette;
  31. import org.insa.graphics.drawing.MercatorProjection;
  32. import org.insa.graphics.drawing.overlays.MarkerOverlay;
  33. import org.insa.graphics.drawing.overlays.MarkerUtils;
  34. import org.insa.graphics.drawing.overlays.Overlay;
  35. import org.insa.graphics.drawing.overlays.PathOverlay;
  36. import org.insa.graphics.drawing.overlays.PointSetOverlay;
  37. /**
  38. * Cette implementation de la classe Dessin produit vraiment un affichage (au
  39. * contraire de la classe DessinInvisible).
  40. */
  41. public class BasicDrawing extends JPanel implements Drawing {
  42. /**
  43. *
  44. */
  45. private static final long serialVersionUID = 96779785877771827L;
  46. private abstract class BasicOverlay implements Overlay {
  47. // Visible?
  48. protected boolean visible;
  49. // Color
  50. protected Color color;
  51. public BasicOverlay(Color color) {
  52. this.visible = true;
  53. this.color = color;
  54. }
  55. @Override
  56. public void setColor(Color color) {
  57. this.color = color;
  58. }
  59. @Override
  60. public Color getColor() {
  61. return this.color;
  62. }
  63. @Override
  64. public void setVisible(boolean visible) {
  65. this.visible = visible;
  66. BasicDrawing.this.repaint();
  67. }
  68. @Override
  69. public boolean isVisible() {
  70. return this.visible;
  71. }
  72. @Override
  73. public void delete() {
  74. synchronized (overlays) {
  75. BasicDrawing.this.overlays.remove(this);
  76. }
  77. BasicDrawing.this.repaint();
  78. }
  79. /**
  80. * Draw the given overlay.
  81. */
  82. public void draw(Graphics2D g) {
  83. if (this.visible) {
  84. drawImpl(g);
  85. }
  86. }
  87. public abstract void drawImpl(Graphics2D g);
  88. public void redraw() {
  89. BasicDrawing.this.repaint();
  90. }
  91. };
  92. private class BasicMarkerOverlay extends BasicOverlay implements MarkerOverlay {
  93. // Marker width and height
  94. public static final int MARKER_WIDTH = 30, MARKER_HEIGHT = 60;
  95. // Point of the marker.
  96. private Point point;
  97. // Image to draw
  98. private Image image;
  99. // Inner color and fill mode.
  100. private Color innerColor;
  101. private final AlphaMode alphaMode;
  102. public BasicMarkerOverlay(Point point, Color color, Color inner, AlphaMode alphaMode) {
  103. super(color);
  104. this.point = point;
  105. this.image = MarkerUtils.getMarkerForColor(color, inner, alphaMode);
  106. this.innerColor = inner;
  107. this.alphaMode = alphaMode;
  108. }
  109. @Override
  110. public Point getPoint() {
  111. return point;
  112. }
  113. @Override
  114. public void setColor(Color color) {
  115. this.innerColor = this.innerColor.equals(this.color) ? color : innerColor;
  116. super.setColor(color);
  117. this.image = MarkerUtils.getMarkerForColor(color, this.innerColor, alphaMode);
  118. }
  119. @Override
  120. public void moveTo(Point point) {
  121. this.point = point;
  122. BasicDrawing.this.repaint();
  123. }
  124. @Override
  125. public void drawImpl(Graphics2D graphics) {
  126. int px = projection.longitudeToPixelX(getPoint().getLongitude());
  127. int py = projection.latitudeToPixelY(getPoint().getLatitude());
  128. graphics.drawImage(this.image, px - MARKER_WIDTH / 2, py - MARKER_HEIGHT, MARKER_WIDTH,
  129. MARKER_HEIGHT, BasicDrawing.this);
  130. }
  131. };
  132. private class BasicPathOverlay extends BasicOverlay implements PathOverlay {
  133. // List of points
  134. private final List<Point> points;
  135. // Origin / Destination markers.
  136. private BasicMarkerOverlay origin, destination;
  137. public BasicPathOverlay(List<Point> points, Color color, BasicMarkerOverlay origin,
  138. BasicMarkerOverlay destination) {
  139. super(color);
  140. this.points = points;
  141. this.origin = origin;
  142. this.destination = destination;
  143. this.color = color;
  144. }
  145. @Override
  146. public void setColor(Color color) {
  147. super.setColor(color);
  148. this.origin.setColor(color);
  149. this.destination.setColor(color);
  150. }
  151. @Override
  152. public void drawImpl(Graphics2D graphics) {
  153. if (!points.isEmpty()) {
  154. graphics.setStroke(new BasicStroke(2));
  155. graphics.setColor(getColor());
  156. Iterator<Point> itPoint = points.iterator();
  157. Point prev = itPoint.next();
  158. while (itPoint.hasNext()) {
  159. Point curr = itPoint.next();
  160. int x1 = projection.longitudeToPixelX(prev.getLongitude());
  161. int x2 = projection.longitudeToPixelX(curr.getLongitude());
  162. int y1 = projection.latitudeToPixelY(prev.getLatitude());
  163. int y2 = projection.latitudeToPixelY(curr.getLatitude());
  164. graphics.drawLine(x1, y1, x2, y2);
  165. prev = curr;
  166. }
  167. }
  168. if (this.origin != null) {
  169. this.origin.draw(graphics);
  170. }
  171. if (this.destination != null) {
  172. this.destination.draw(graphics);
  173. }
  174. }
  175. };
  176. private class BasicPointSetOverlay extends BasicOverlay implements PointSetOverlay {
  177. // Default point width
  178. private static final int DEFAULT_POINT_WIDTH = 5;
  179. // Image for path / points
  180. private final BufferedImage image;
  181. private final Graphics2D graphics;
  182. private int width = DEFAULT_POINT_WIDTH;
  183. public BasicPointSetOverlay() {
  184. super(Color.BLACK);
  185. this.image = new BufferedImage(BasicDrawing.this.width, BasicDrawing.this.height,
  186. BufferedImage.TYPE_4BYTE_ABGR);
  187. this.graphics = image.createGraphics();
  188. this.graphics.setBackground(new Color(0, 0, 0, 0));
  189. }
  190. @Override
  191. public void setColor(Color color) {
  192. super.setColor(color);
  193. this.graphics.setColor(color);
  194. }
  195. @Override
  196. public void setWidth(int width) {
  197. this.width = Math.max(2, width);
  198. }
  199. @Override
  200. public void setWidthAndColor(int width, Color color) {
  201. setWidth(width);
  202. setColor(color);
  203. }
  204. @Override
  205. public void addPoint(Point point) {
  206. int x = projection.longitudeToPixelX(point.getLongitude()) - this.width / 2;
  207. int y = projection.latitudeToPixelY(point.getLatitude()) - this.width / 2;
  208. this.graphics.fillOval(x, y, this.width, this.width);
  209. BasicDrawing.this.repaint();
  210. }
  211. @Override
  212. public void addPoint(Point point, int width) {
  213. setWidth(width);
  214. addPoint(point);
  215. }
  216. @Override
  217. public void addPoint(Point point, Color color) {
  218. setColor(color);
  219. addPoint(point);
  220. }
  221. @Override
  222. public void addPoint(Point point, int width, Color color) {
  223. setWidth(width);
  224. setColor(color);
  225. addPoint(point);
  226. }
  227. @Override
  228. public void drawImpl(Graphics2D g) {
  229. g.drawImage(this.image, 0, 0, BasicDrawing.this);
  230. }
  231. }
  232. // Default path color.
  233. public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
  234. // Default palette.
  235. public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette();
  236. // Maximum width for the drawing (in pixels).
  237. private static final int MAXIMUM_DRAWING_WIDTH = 2000;
  238. private MercatorProjection projection;
  239. // Width and height of the image
  240. private int width, height;
  241. // Zoom controls
  242. private MapZoomControls zoomControls;
  243. private ZoomAndPanListener zoomAndPanListener;
  244. //
  245. private Image graphImage = null;
  246. private Graphics2D graphGraphics = null;
  247. // List of image for markers
  248. private List<BasicOverlay> overlays = Collections
  249. .synchronizedList(new ArrayList<BasicOverlay>());
  250. // Mapping DrawingClickListener -> MouseEventListener
  251. private List<DrawingClickListener> drawingClickListeners = new ArrayList<>();
  252. /**
  253. * Create a new BasicDrawing.
  254. *
  255. */
  256. public BasicDrawing() {
  257. setLayout(null);
  258. this.setBackground(new Color(245, 245, 245));
  259. this.zoomAndPanListener = new ZoomAndPanListener(this,
  260. ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
  261. // Try...
  262. try {
  263. this.zoomControls = new MapZoomControls(this, 0,
  264. ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20);
  265. this.zoomControls.addZoomInListener(new ActionListener() {
  266. @Override
  267. public void actionPerformed(ActionEvent e) {
  268. zoomAndPanListener.zoomIn();
  269. }
  270. });
  271. this.zoomControls.addZoomOutListener(new ActionListener() {
  272. @Override
  273. public void actionPerformed(ActionEvent e) {
  274. zoomAndPanListener.zoomOut();
  275. }
  276. });
  277. }
  278. catch (IOException e) {
  279. e.printStackTrace();
  280. }
  281. this.addMouseListener(new MouseAdapter() {
  282. @Override
  283. public void mouseClicked(MouseEvent evt) {
  284. if (zoomControls.contains(evt.getPoint())) {
  285. return;
  286. }
  287. Point lonlat = null;
  288. try {
  289. lonlat = getLongitudeLatitude(evt);
  290. }
  291. catch (NoninvertibleTransformException e) {
  292. return;
  293. }
  294. for (DrawingClickListener listener: drawingClickListeners) {
  295. listener.mouseClicked(lonlat);
  296. }
  297. }
  298. });
  299. }
  300. @Override
  301. public void paintComponent(Graphics g1) {
  302. super.paintComponent(g1);
  303. Graphics2D g = (Graphics2D) g1;
  304. AffineTransform sTransform = g.getTransform();
  305. g.setColor(this.getBackground());
  306. g.fillRect(0, 0, getWidth(), getHeight());
  307. g.setTransform(zoomAndPanListener.getCoordTransform());
  308. if (graphImage != null) {
  309. // Draw graph
  310. g.drawImage(graphImage, 0, 0, this);
  311. }
  312. // Draw markers
  313. synchronized (overlays) {
  314. for (BasicOverlay overlay: overlays) {
  315. overlay.draw(g);
  316. }
  317. }
  318. g.setTransform(sTransform);
  319. if (this.zoomControls != null) {
  320. this.zoomControls.setZoomLevel(this.zoomAndPanListener.getZoomLevel());
  321. this.zoomControls.draw(g, getWidth() - this.zoomControls.getWidth() - 20,
  322. this.getHeight() - this.zoomControls.getHeight() - 10, this);
  323. }
  324. }
  325. /*
  326. * (non-Javadoc)
  327. *
  328. * @see org.insa.graphics.drawing.Drawing#clear()
  329. */
  330. @Override
  331. public void clear() {
  332. if (this.graphGraphics != null) {
  333. this.graphGraphics.clearRect(0, 0, this.width, this.height);
  334. }
  335. synchronized (overlays) {
  336. this.overlays.clear();
  337. }
  338. this.repaint();
  339. }
  340. /*
  341. * (non-Javadoc)
  342. *
  343. * @see org.insa.graphics.drawing.Drawing#clearOverlays()
  344. */
  345. @Override
  346. public void clearOverlays() {
  347. synchronized (overlays) {
  348. this.overlays.clear();
  349. }
  350. this.repaint();
  351. }
  352. /**
  353. * @return The current ZoomAndPanListener associated with this drawing.
  354. */
  355. public ZoomAndPanListener getZoomAndPanListener() {
  356. return this.zoomAndPanListener;
  357. }
  358. /**
  359. * Return the longitude and latitude corresponding to the given position of the
  360. * MouseEvent.
  361. *
  362. * @param event MouseEvent from which longitude/latitude should be retrieved.
  363. *
  364. * @return Point representing the projection of the MouseEvent position in the
  365. * graph/map.
  366. *
  367. * @throws NoninvertibleTransformException if the actual transformation is
  368. * invalid.
  369. */
  370. protected Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
  371. // Get the point using the inverse transform of the Zoom/Pan object, this gives
  372. // us
  373. // a point within the drawing box (between [0, 0] and [width, height]).
  374. Point2D ptDst = this.zoomAndPanListener.getCoordTransform()
  375. .inverseTransform(event.getPoint(), null);
  376. // Inverse the "projection" on x/y to get longitude and latitude.
  377. return new Point(projection.pixelXToLongitude(ptDst.getX()),
  378. projection.pixelYToLatitude(ptDst.getY()));
  379. }
  380. /*
  381. * (non-Javadoc)
  382. *
  383. * @see
  384. * org.insa.graphics.drawing.Drawing#addDrawingClickListener(org.insa.graphics.
  385. * drawing.DrawingClickListener)
  386. */
  387. @Override
  388. public void addDrawingClickListener(DrawingClickListener listener) {
  389. this.drawingClickListeners.add(listener);
  390. }
  391. /*
  392. * (non-Javadoc)
  393. *
  394. * @see org.insa.graphics.drawing.Drawing#removeDrawingClickListener(org.insa.
  395. * graphics.drawing.DrawingClickListener)
  396. */
  397. @Override
  398. public void removeDrawingClickListener(DrawingClickListener listener) {
  399. this.drawingClickListeners.remove(listener);
  400. }
  401. public BasicMarkerOverlay createMarker(Point point, Color outer, Color inner, AlphaMode mode) {
  402. return new BasicMarkerOverlay(point, outer, inner, mode);
  403. }
  404. @Override
  405. public MarkerOverlay drawMarker(Point point, Color outer, Color inner, AlphaMode mode) {
  406. BasicMarkerOverlay marker = createMarker(point, outer, inner, mode);
  407. synchronized (overlays) {
  408. this.overlays.add(marker);
  409. }
  410. this.repaint();
  411. return marker;
  412. }
  413. @Override
  414. public PointSetOverlay createPointSetOverlay() {
  415. BasicPointSetOverlay ps = new BasicPointSetOverlay();
  416. synchronized (overlays) {
  417. this.overlays.add(ps);
  418. }
  419. return ps;
  420. }
  421. @Override
  422. public PointSetOverlay createPointSetOverlay(int width, Color color) {
  423. PointSetOverlay ps = createPointSetOverlay();
  424. ps.setWidthAndColor(width, color);
  425. return ps;
  426. }
  427. /**
  428. * Draw the given arc.
  429. *
  430. * @param arc Arc to draw.
  431. * @param palette Palette to use to retrieve color and width for arc, or null to
  432. * use current settings.
  433. */
  434. protected void drawArc(Arc arc, GraphPalette palette, boolean repaint) {
  435. List<Point> pts = arc.getPoints();
  436. if (!pts.isEmpty()) {
  437. if (palette != null) {
  438. this.graphGraphics.setColor(palette.getColorForArc(arc));
  439. this.graphGraphics.setStroke(new BasicStroke(palette.getWidthForArc(arc)));
  440. }
  441. Iterator<Point> it1 = pts.iterator();
  442. Point prev = it1.next();
  443. while (it1.hasNext()) {
  444. Point curr = it1.next();
  445. int x1 = projection.longitudeToPixelX(prev.getLongitude());
  446. int x2 = projection.longitudeToPixelX(curr.getLongitude());
  447. int y1 = projection.latitudeToPixelY(prev.getLatitude());
  448. int y2 = projection.latitudeToPixelY(curr.getLatitude());
  449. graphGraphics.drawLine(x1, y1, x2, y2);
  450. prev = curr;
  451. }
  452. }
  453. if (repaint) {
  454. this.repaint();
  455. }
  456. }
  457. /**
  458. * Initialize the drawing for the given graph.
  459. *
  460. * @param graph
  461. */
  462. protected void initialize(Graph graph) {
  463. // Clear everything.
  464. this.clear();
  465. BoundingBox box = graph.getGraphInformation().getBoundingBox();
  466. // Find minimum/maximum longitude and latitude.
  467. float minLon = box.getTopLeftPoint().getLongitude(),
  468. maxLon = box.getBottomRightPoint().getLongitude(),
  469. minLat = box.getBottomRightPoint().getLatitude(),
  470. maxLat = box.getTopLeftPoint().getLatitude();
  471. // Add a little delta to avoid drawing on the edge...
  472. float diffLon = maxLon - minLon, diffLat = maxLat - minLat;
  473. float deltaLon = 0.01f * diffLon, deltaLat = 0.01f * diffLat;
  474. // Create the projection and retrieve width and height for the box.
  475. projection = new MercatorProjection(box.extend(deltaLon, deltaLat, deltaLon, deltaLat),
  476. MAXIMUM_DRAWING_WIDTH);
  477. this.width = (int) projection.getImageWidth();
  478. this.height = (int) projection.getImageHeight();
  479. // Create the image
  480. BufferedImage img = new BufferedImage(this.width, this.height,
  481. BufferedImage.TYPE_3BYTE_BGR);
  482. this.graphImage = img;
  483. this.graphGraphics = img.createGraphics();
  484. this.graphGraphics.setBackground(this.getBackground());
  485. this.graphGraphics.clearRect(0, 0, this.width, this.height);
  486. // Set the zoom and pan listener
  487. double scale = 1 / Math.max(this.width / (double) this.getWidth(),
  488. this.height / (double) this.getHeight());
  489. this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform());
  490. this.zoomAndPanListener.getCoordTransform().translate(
  491. (this.getWidth() - this.width * scale) / 2,
  492. (this.getHeight() - this.height * scale) / 2);
  493. this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
  494. this.zoomAndPanListener.setZoomLevel(0);
  495. this.zoomControls.setZoomLevel(0);
  496. // Repaint
  497. this.repaint();
  498. }
  499. @Override
  500. public void drawGraph(Graph graph, GraphPalette palette) {
  501. int repaintModulo = graph.getNodes().size() / 100;
  502. // Initialize the buffered image
  503. this.initialize(graph);
  504. // Remove zoom and pan listener
  505. this.removeMouseListener(zoomAndPanListener);
  506. this.removeMouseMotionListener(zoomAndPanListener);
  507. this.removeMouseWheelListener(zoomAndPanListener);
  508. for (Node node: graph.getNodes()) {
  509. for (Arc arc: node.getSuccessors()) {
  510. // Draw arcs only if there are one-way arcs or if origin is lower than
  511. // destination, avoid drawing two-ways arc twice.
  512. if (arc.getRoadInformation().isOneWay()
  513. || arc.getOrigin().compareTo(arc.getDestination()) < 0) {
  514. drawArc(arc, palette, false);
  515. }
  516. }
  517. if (node.getId() % repaintModulo == 0) {
  518. this.repaint();
  519. }
  520. }
  521. this.repaint();
  522. // Re-add zoom and pan listener
  523. this.addMouseListener(zoomAndPanListener);
  524. this.addMouseMotionListener(zoomAndPanListener);
  525. this.addMouseWheelListener(zoomAndPanListener);
  526. }
  527. @Override
  528. public void drawGraph(Graph graph) {
  529. drawGraph(graph, DEFAULT_PALETTE);
  530. }
  531. @Override
  532. public PathOverlay drawPath(Path path, Color color, boolean markers) {
  533. List<Point> points = new ArrayList<Point>();
  534. if (!path.isEmpty()) {
  535. points.add(path.getOrigin().getPoint());
  536. for (Arc arc: path.getArcs()) {
  537. Iterator<Point> itPoint = arc.getPoints().iterator();
  538. // Discard origin each time
  539. itPoint.next();
  540. while (itPoint.hasNext()) {
  541. points.add(itPoint.next());
  542. }
  543. }
  544. }
  545. BasicMarkerOverlay origin = null, destination = null;
  546. if (markers && !path.isEmpty()) {
  547. origin = createMarker(path.getOrigin().getPoint(), color, color, AlphaMode.TRANSPARENT);
  548. destination = createMarker(path.getDestination().getPoint(), color, color,
  549. AlphaMode.TRANSPARENT);
  550. }
  551. BasicPathOverlay overlay = new BasicPathOverlay(points, color, origin, destination);
  552. synchronized (overlays) {
  553. this.overlays.add(overlay);
  554. }
  555. this.repaint();
  556. return overlay;
  557. }
  558. @Override
  559. public PathOverlay drawPath(Path path, Color color) {
  560. return drawPath(path, color, true);
  561. }
  562. @Override
  563. public PathOverlay drawPath(Path path) {
  564. return drawPath(path, DEFAULT_PATH_COLOR);
  565. }
  566. @Override
  567. public PathOverlay drawPath(Path path, boolean markers) {
  568. return drawPath(path, DEFAULT_PATH_COLOR, markers);
  569. }
  570. }