EdgePainter.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.mapView;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.RenderingHints;
  8. import java.awt.geom.Point2D;
  9. import java.util.HashSet;
  10. import org.jxmapviewer.JXMapViewer;
  11. import org.jxmapviewer.painter.Painter;
  12. import org.jxmapviewer.viewer.GeoPosition;
  13. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  14. /**
  15. * Paints a route
  16. *
  17. * @author Dominik Renkel
  18. */
  19. public class EdgePainter implements Painter<JXMapViewer> {
  20. /**
  21. * show edges property
  22. */
  23. private boolean showEdges = true;
  24. /**
  25. * standard color in which edges are drawn
  26. */
  27. private static Color STANDARD = Color.BLACK;
  28. /**
  29. * color in which edges are drawn when clicked
  30. */
  31. private static Color CLICKED = Color.RED;
  32. /**
  33. * color in which edges are drawn when they are used in a placement
  34. */
  35. private static Color PLACEMENT = Color.BLUE;
  36. /**
  37. * the thickness of edges
  38. */
  39. private static int EDGE_THICKNESS = 2;
  40. /**
  41. * anti aliasing property
  42. */
  43. private boolean antiAlias = true;
  44. /**
  45. * the edges of the currently shown graph
  46. */
  47. private static HashSet<MyEdge> edges;
  48. /**
  49. * show weights property
  50. */
  51. private Boolean showWeights = true;
  52. /**
  53. * @param track
  54. * the track
  55. */
  56. public EdgePainter(HashSet<MyEdge> track) {
  57. // copy the list so that changes in the
  58. // original list do not have an effect here
  59. edges = new HashSet<MyEdge>(track);
  60. }
  61. @Override
  62. public void paint(Graphics2D g, JXMapViewer mapViewer, int w, int h) {
  63. if (showEdges) {
  64. g = (Graphics2D) g.create();
  65. // convert from viewport to world bitmap
  66. Rectangle rect = mapViewer.getViewportBounds();
  67. g.translate(-rect.x, -rect.y);
  68. if (antiAlias)
  69. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  70. // do the drawing again
  71. g.setColor(STANDARD);
  72. g.setStroke(new BasicStroke(EDGE_THICKNESS));
  73. drawRoute(g, mapViewer);
  74. g.dispose();
  75. }
  76. }
  77. /**
  78. * Draws a line on the street map for all edges
  79. *
  80. * @param g
  81. * the graphics object
  82. * @param mapViewer
  83. * the map
  84. */
  85. private void drawRoute(Graphics2D g, JXMapViewer mapViewer) {
  86. for (MyEdge edge : edges) {
  87. // Get geo Positions of the two nodes that define the edge
  88. GeoPosition startPos = new GeoPosition(edge.getNode0().getAttribute("lat"),
  89. edge.getNode0().getAttribute("long"));
  90. GeoPosition endPos = new GeoPosition(edge.getNode1().getAttribute("lat"),
  91. edge.getNode1().getAttribute("long"));
  92. // convert geo-coordinate to world bitmap pixel
  93. Point2D startPoint = mapViewer.getTileFactory().geoToPixel(startPos, mapViewer.getZoom());
  94. Point2D endPoint = mapViewer.getTileFactory().geoToPixel(endPos, mapViewer.getZoom());
  95. if (edge.hasAttribute("ui.map.selected") && (boolean) edge.getAttribute("ui.map.selected")) {
  96. // draw red line if edge is selected
  97. g.setColor(CLICKED);
  98. } else if (edge.hasAttribute("usedInPlacement") && (boolean) edge.getAttribute("usedInPlacement")) {
  99. // draw blue line when edge used in placement
  100. g.setColor(PLACEMENT);
  101. } else {
  102. // draw black line if not selected
  103. g.setColor(STANDARD);
  104. }
  105. g.drawLine((int) startPoint.getX(), (int) startPoint.getY(), (int) endPoint.getX(), (int) endPoint.getY());
  106. if (showWeights) {
  107. drawWeights(edge, g, startPoint, endPoint);
  108. }
  109. }
  110. }
  111. /**
  112. * draw the weights of an edge
  113. *
  114. * @param edge
  115. * edge
  116. * @param g
  117. * graphic
  118. * @param startPoint
  119. * start point edge
  120. * @param endPoint
  121. * end point edge
  122. */
  123. private void drawWeights(MyEdge edge, Graphics2D g, Point2D startPoint, Point2D endPoint) {
  124. // Set weight Position on street map
  125. String weight = edge.getAttribute("weight").toString();
  126. // get weight height and width under given font
  127. FontMetrics metrics = g.getFontMetrics();
  128. int tw = metrics.stringWidth(weight);
  129. int th = 1 + metrics.getAscent();
  130. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  131. double deltaX = (startPoint.getX() - endPoint.getX()) / 2;
  132. double deltaY = (startPoint.getY() - endPoint.getY()) / 2;
  133. double weightPosX;
  134. double weightPosY;
  135. if (deltaX < 0) {
  136. weightPosX = startPoint.getX() + Math.abs(deltaX);
  137. } else {
  138. weightPosX = startPoint.getX() - Math.abs(deltaX);
  139. }
  140. if (deltaY < 0) {
  141. weightPosY = startPoint.getY() + Math.abs(deltaY);
  142. } else {
  143. weightPosY = startPoint.getY() - Math.abs(deltaY);
  144. }
  145. // Show weight left middle of deviceType picture
  146. g.drawString(weight, (int) weightPosX - tw / 2, (int) weightPosY - th / 2);
  147. }
  148. /**
  149. * Sets the showEdges attribute
  150. *
  151. * @param showEdges
  152. */
  153. public void setShowEdges(Boolean showEdges) {
  154. this.showEdges = showEdges;
  155. }
  156. /**
  157. * sets the showWeights attribute
  158. *
  159. * @param showWeights
  160. */
  161. public void setShowWeights(Boolean showWeights) {
  162. this.showWeights = showWeights;
  163. }
  164. /**
  165. * sets the thickness of the drawn edges
  166. *
  167. * @param thickness
  168. */
  169. public static void setEdgeThickness(int thickness) {
  170. EDGE_THICKNESS = thickness;
  171. }
  172. /**
  173. * sets the color types of edges
  174. *
  175. * @param standard
  176. * standard color when symbol rep. opened
  177. * @param placement
  178. * when used in placement
  179. * @param selected
  180. * when clicked
  181. */
  182. public static void setColor(String standard, String placement, String selected) {
  183. STANDARD = stringToColor(standard);
  184. PLACEMENT = stringToColor(placement);
  185. CLICKED = stringToColor(selected);
  186. }
  187. /**
  188. *
  189. * @param string
  190. * @return color under given string
  191. */
  192. public static Color stringToColor(String color) {
  193. switch (color) {
  194. case "Red":
  195. return Color.RED;
  196. case "Black":
  197. return Color.BLACK;
  198. case "Blue":
  199. return Color.BLUE;
  200. case "Yellow":
  201. return Color.YELLOW;
  202. case "Green":
  203. return Color.GREEN;
  204. case "Orange":
  205. return Color.ORANGE;
  206. case "Gray":
  207. return Color.GRAY;
  208. default:
  209. return Color.BLACK;
  210. }
  211. }
  212. /**
  213. * @return the thickness of edges
  214. */
  215. public static int getThickness() {
  216. return EDGE_THICKNESS;
  217. }
  218. /**
  219. * @return color when clicked
  220. */
  221. public static String getClickedColor() {
  222. return getColorAsString(CLICKED);
  223. }
  224. /**
  225. * @return standard color
  226. */
  227. public static String getStandardColor() {
  228. return getColorAsString(STANDARD);
  229. }
  230. /**
  231. * @return placement color
  232. */
  233. public static String getPlacementColor() {
  234. return getColorAsString(PLACEMENT);
  235. }
  236. /**
  237. *
  238. * @param color
  239. * @return color in specific string representation
  240. */
  241. public static String getColorAsString(Color color) {
  242. if (color.equals(Color.RED))
  243. return "Red";
  244. if (color.equals(Color.BLACK))
  245. return "Black";
  246. if (color.equals(Color.BLUE))
  247. return "Blue";
  248. if (color.equals(Color.GREEN))
  249. return "Green";
  250. if (color.equals(Color.YELLOW))
  251. return "Yellow";
  252. if (color.equals(Color.ORANGE))
  253. return "Orange";
  254. if (color.equals(Color.GRAY))
  255. return "Gray";
  256. return "Unknown";
  257. }
  258. }