MapImage.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. double n = Math.pow(2, Math.ceil(Math.log(matriz.max())));
  170. for (DiscretePoint p: matriz) {
  171. // g.setColor(new Color(0.31f, 0.58f, 0.80f, (float)(p.get()/max)));
  172. // >> g.setColor(new Color(0.31f, 0.58f, 0.80f, (float)(Math.pow(p.get(), 0.25))));
  173. // g.setColor(Color.getHSBColor((float) (p.get()*0.9), 1, 1));
  174. g.setColor(new Color(0.31f, 0.58f, 0.80f, (float) (p.get()/n)));
  175. g.fillArc(
  176. (int) (p.bounds().getMinX()*zoom+w),
  177. (int) (-p.bounds().getMinY()*zoom+h-matriz.ssize*zoom),
  178. (int) (matriz.ssize*zoom),
  179. (int) (matriz.ssize*zoom),
  180. (int) ((p.minAng()+270)%360), // onde comeca
  181. (int) ((p.maxAng()-p.minAng())%360) // tamanho
  182. );
  183. }
  184. }
  185. /**
  186. * Adiciona um ponto ao mapa
  187. * @param p ponto
  188. */
  189. public void addPoint(Pose p) {
  190. if (semaphore.tryAcquire()) {
  191. lista_pontos.clear();
  192. lista_pontos.add(p);
  193. semaphore.release();
  194. }
  195. repaint();
  196. }
  197. public void addPoint(float x, float y, float z) {
  198. if (semaphore.tryAcquire()) {
  199. lista_pontos.add(new Pose(x, y, z));
  200. semaphore.release();
  201. }
  202. repaint();
  203. }
  204. public void addRead(float x, float y) {
  205. if (semaphore.tryAcquire()) {
  206. lista_ultra.add(new Pose(x, y, 0));
  207. semaphore.release();
  208. }
  209. repaint();
  210. }
  211. public void addPoint(double x, double y, double z) {
  212. addPoint((float)x, (float)y, (float)z);
  213. }
  214. public void addRead(double x, double y) {
  215. addRead((float)x, (float)y);
  216. }
  217. public void showLine () {
  218. line = !line;
  219. repaint();
  220. }
  221. public void setVisual (int method) {
  222. visual_method = method;
  223. repaint();
  224. }
  225. public void save () {
  226. Integer name = new Integer((int) (Math.random()*1000000));
  227. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  228. Graphics g = imagebuf.createGraphics();
  229. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  230. print(g);
  231. try {
  232. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  233. JOptionPane.showMessageDialog(null, "Image saved.");
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. JOptionPane.showMessageDialog(null, "Image not saved.");
  237. }
  238. }
  239. public void clean() {
  240. if (semaphore.tryAcquire()) {
  241. lista_pontos.clear();
  242. lista_ultra.clear();
  243. semaphore.release();
  244. }
  245. repaint();
  246. }
  247. @Override
  248. public void mouseDragged(MouseEvent e) {
  249. centerx += e.getX() - mousePt.x;
  250. centery += e.getY() - mousePt.y;
  251. mousePt = e.getPoint();
  252. repaint();
  253. }
  254. @Override
  255. public void mouseMoved(MouseEvent e) {
  256. }
  257. @Override
  258. public void mouseClicked(MouseEvent e) {
  259. }
  260. @Override
  261. public void mousePressed(MouseEvent e) {
  262. mousePt = e.getPoint();
  263. repaint();
  264. }
  265. @Override
  266. public void mouseReleased(MouseEvent e) {
  267. }
  268. @Override
  269. public void mouseEntered(MouseEvent e) {
  270. }
  271. @Override
  272. public void mouseExited(MouseEvent e) {
  273. }
  274. @Override
  275. public void mouseWheelMoved(MouseWheelEvent e) {
  276. if(e.getWheelRotation()<0){
  277. if (zoom < 15.0)
  278. zoom *= 1.1;
  279. repaint();
  280. }
  281. //Zoom out
  282. if(e.getWheelRotation()>0){
  283. if (zoom > 1.0)
  284. zoom /= 1.1;
  285. repaint();
  286. }
  287. }
  288. public void showMatriz(ProbabilityMatriz matriz) {
  289. setMatriz(matriz);
  290. repaint();
  291. }
  292. public void setMatriz(ProbabilityMatriz matriz) {
  293. this.matriz = matriz;
  294. }
  295. }