MapImage.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseMotionListener;
  7. import java.awt.event.MouseWheelEvent;
  8. import java.awt.event.MouseWheelListener;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.concurrent.Semaphore;
  14. import javax.imageio.ImageIO;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import lejos.geom.Line;
  18. import lejos.robotics.mapping.LineMap;
  19. import lejos.robotics.navigation.Pose;
  20. public class MapImage extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener {
  21. private double zoom = 2.0; // pixel per cm
  22. private double grid = 10.0; // cm
  23. private double centerx = 0.0;
  24. private double centery = 0.0; // cm
  25. private Point mousePt;
  26. private ArrayList<Pose> lista_pontos;
  27. private ArrayList<Pose> lista_ultra;
  28. private int visual_method = 0;
  29. private boolean line = false;
  30. private Semaphore semaphore;
  31. private LineMap map;
  32. private ProbabilityMatriz matriz;
  33. public MapImage() {
  34. super();
  35. semaphore = new Semaphore(1);
  36. lista_pontos = new ArrayList<Pose>();
  37. lista_ultra = new ArrayList<Pose>();
  38. setBackground(Color.BLACK);
  39. addMouseWheelListener(this);
  40. addMouseListener(this);
  41. addMouseMotionListener(this);
  42. }
  43. public MapImage(LineMap map) {
  44. this();
  45. this.map = map;
  46. }
  47. private void drawModel (Graphics g) {
  48. int width = (int) (getWidth()+2*centerx);
  49. int height = (int) (getHeight()+2*centery);
  50. int count = 0;
  51. int x_tmp = 0, y_tmp = 0;
  52. for (Pose p : lista_pontos) {
  53. double hading = Math.toRadians(p.getHeading());
  54. int x = width/2+(int)(p.getX()*zoom);
  55. int y = height/2+(int)(p.getY()*zoom)*-1;
  56. if (visual_method == 0) {
  57. g.setColor(Color.getHSBColor((float) (hading/(2.0*Math.PI)), 1, 1));
  58. g.fillOval(
  59. x-(int)(zoom/2.0*1.5),
  60. y-(int)(zoom/2.0*1.5),
  61. (int)(zoom*1.5),
  62. (int)(zoom*1.5)
  63. );
  64. } else if (visual_method == 1) {
  65. g.setColor(Color.WHITE);
  66. g.drawLine(
  67. width/2+(int)(p.getX()*zoom),
  68. height/2-(int)(p.getY()*zoom),
  69. width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom),
  70. height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom)
  71. );
  72. g.drawLine(
  73. width/2+(int)(p.getX()*zoom+zoom*Math.sin(hading)),
  74. height/2-(int)(p.getY()*zoom-zoom*Math.cos(hading)),
  75. width/2+(int)(p.getX()*zoom+0.6*zoom*Math.sin(Math.PI/8+hading)),
  76. height/2-(int)(p.getY()*zoom-0.6*zoom*Math.cos(Math.PI/8+hading))
  77. );
  78. } else if (visual_method == 2) {
  79. g.setColor(Color.WHITE);
  80. g.fillOval(
  81. x-(int)(zoom/2.0*5),
  82. y-(int)(zoom/2.0*5),
  83. (int)(zoom*5),
  84. (int)(zoom*5)
  85. );
  86. g.setColor(Color.BLACK);
  87. g.drawLine(
  88. width/2+(int)(p.getX()*zoom),
  89. height/2-(int)(p.getY()*zoom),
  90. width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom*2.5),
  91. height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom*2.5)
  92. );
  93. }
  94. if (line && count != 0) {
  95. g.setColor(Color.LIGHT_GRAY);
  96. g.drawLine(x_tmp, y_tmp, x, y);
  97. }
  98. x_tmp = x;
  99. y_tmp = y;
  100. count++;
  101. }
  102. g.setColor(Color.RED);
  103. for (Pose p : lista_ultra) {
  104. int x = width/2+(int)(p.getX()*zoom);
  105. int y = height/2+(int)(p.getY()*zoom)*-1;
  106. g.fillRect(
  107. x-(int)(zoom/2.0*1.0),
  108. y-(int)(zoom/2.0*1.0),
  109. (int)(zoom*1.0),
  110. (int)(zoom*1.0)
  111. );
  112. }
  113. if (map != null) {
  114. Line[] lines = map.getLines();
  115. for (int i = 0; i < lines.length; i++) {
  116. Line l = lines[i];
  117. g.drawLine(
  118. width/2+(int)(l.x1*zoom),
  119. height/2-(int)(l.y1*zoom),
  120. width/2+(int)(l.x2*zoom),
  121. height/2-(int)(l.y2*zoom)
  122. );
  123. }
  124. }
  125. }
  126. @Override
  127. protected void paintComponent(Graphics g) {
  128. int width = (int) (getWidth());
  129. int height = (int) (getHeight());
  130. int width2 = (int) (getWidth()+2*centerx);
  131. int height2 = (int) (getHeight()+2*centery);
  132. super.paintComponent(g);
  133. g.setColor(new Color(20, 20, 20));
  134. int initial_x = height2/2;
  135. while (initial_x < width) {
  136. initial_x += grid*zoom;
  137. g.drawLine(0, initial_x, width, initial_x);
  138. }
  139. initial_x = height2/2;
  140. while (initial_x > 0) {
  141. initial_x -= grid*zoom;
  142. g.drawLine(0, initial_x, width, initial_x);
  143. }
  144. int initial_y = width2/2;
  145. while (initial_y < width) {
  146. initial_y += grid*zoom;
  147. g.drawLine(initial_y, 0, initial_y, height);
  148. }
  149. initial_y = width2/2;
  150. while (initial_y > 0) {
  151. initial_y -= grid*zoom;
  152. g.drawLine(initial_y, 0, initial_y, height);
  153. }
  154. g.setColor(Color.ORANGE);
  155. g.drawLine(width2/2, 0, width2/2, height);
  156. g.drawLine(0, height2/2, width, height2/2);
  157. drawMatriz(g);
  158. if (semaphore.tryAcquire()) {
  159. drawMatriz(g);
  160. drawModel(g);
  161. semaphore.release();
  162. }
  163. }
  164. private void drawMatriz(Graphics g) {
  165. if (matriz == null) return;
  166. double w = getWidth()/2.0+centerx;
  167. double h = getHeight()/2.0+centery;
  168. double max = matriz.max();
  169. for (DiscretePoint p: matriz) {
  170. g.setColor(new Color(0.31f, 0.58f, 0.80f, (float)(p.get()/max)));
  171. g.fillArc(
  172. (int) (p.bounds().getMinX()*zoom+w),
  173. (int) (-p.bounds().getMinY()*zoom+h-matriz.ssize*zoom),
  174. (int) (matriz.ssize*zoom),
  175. (int) (matriz.ssize*zoom),
  176. (int) ((p.minAng())%360), // onde comeca
  177. (int) ((p.maxAng()-p.minAng())%360) // tamanho
  178. );
  179. }
  180. }
  181. /**
  182. * Adiciona um ponto ao mapa
  183. * @param p ponto
  184. */
  185. public void addPoint(Pose p) {
  186. if (semaphore.tryAcquire()) {
  187. lista_pontos.clear();
  188. lista_pontos.add(p);
  189. semaphore.release();
  190. }
  191. repaint();
  192. }
  193. public void addPoint(float x, float y, float z) {
  194. if (semaphore.tryAcquire()) {
  195. lista_pontos.add(new Pose(x, y, z));
  196. semaphore.release();
  197. }
  198. repaint();
  199. }
  200. public void addRead(float x, float y) {
  201. if (semaphore.tryAcquire()) {
  202. lista_ultra.add(new Pose(x, y, 0));
  203. semaphore.release();
  204. }
  205. repaint();
  206. }
  207. public void addPoint(double x, double y, double z) {
  208. addPoint((float)x, (float)y, (float)z);
  209. }
  210. public void addRead(double x, double y) {
  211. addRead((float)x, (float)y);
  212. }
  213. public void showLine () {
  214. line = !line;
  215. repaint();
  216. }
  217. public void setVisual (int method) {
  218. visual_method = method;
  219. repaint();
  220. }
  221. public void save () {
  222. Integer name = new Integer((int) (Math.random()*1000000));
  223. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  224. Graphics g = imagebuf.createGraphics();
  225. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  226. print(g);
  227. try {
  228. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  229. JOptionPane.showMessageDialog(null, "Image saved.");
  230. } catch (IOException e) {
  231. e.printStackTrace();
  232. JOptionPane.showMessageDialog(null, "Image not saved.");
  233. }
  234. }
  235. public void clean() {
  236. if (semaphore.tryAcquire()) {
  237. lista_pontos.clear();
  238. lista_ultra.clear();
  239. semaphore.release();
  240. }
  241. repaint();
  242. }
  243. @Override
  244. public void mouseDragged(MouseEvent e) {
  245. centerx += e.getX() - mousePt.x;
  246. centery += e.getY() - mousePt.y;
  247. mousePt = e.getPoint();
  248. repaint();
  249. }
  250. @Override
  251. public void mouseMoved(MouseEvent e) {
  252. }
  253. @Override
  254. public void mouseClicked(MouseEvent e) {
  255. }
  256. @Override
  257. public void mousePressed(MouseEvent e) {
  258. mousePt = e.getPoint();
  259. repaint();
  260. }
  261. @Override
  262. public void mouseReleased(MouseEvent e) {
  263. }
  264. @Override
  265. public void mouseEntered(MouseEvent e) {
  266. }
  267. @Override
  268. public void mouseExited(MouseEvent e) {
  269. }
  270. @Override
  271. public void mouseWheelMoved(MouseWheelEvent e) {
  272. if(e.getWheelRotation()<0){
  273. if (zoom < 15.0)
  274. zoom *= 1.1;
  275. repaint();
  276. }
  277. //Zoom out
  278. if(e.getWheelRotation()>0){
  279. if (zoom > 1.0)
  280. zoom /= 1.1;
  281. repaint();
  282. }
  283. }
  284. public void showMatriz(ProbabilityMatriz matriz) {
  285. setMatriz(matriz);
  286. repaint();
  287. }
  288. public void setMatriz(ProbabilityMatriz matriz) {
  289. this.matriz = matriz;
  290. }
  291. }