c038912b1ebb001711b8bf632416c20d 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. public MapImage() {
  33. super();
  34. semaphore = new Semaphore(1);
  35. lista_pontos = new ArrayList<Pose>();
  36. lista_ultra = new ArrayList<Pose>();
  37. setBackground(Color.BLACK);
  38. addMouseWheelListener(this);
  39. addMouseListener(this);
  40. addMouseMotionListener(this);
  41. }
  42. public MapImage(LineMap map) {
  43. this();
  44. this.map = map;
  45. }
  46. private void drawModel (Graphics g) {
  47. int width = (int) (getWidth()+2*centerx);
  48. int height = (int) (getHeight()+2*centery);
  49. int count = 0;
  50. int x_tmp = 0, y_tmp = 0;
  51. for (Pose p : lista_pontos) {
  52. double hading = Math.toRadians(p.getHeading());
  53. int x = width/2+(int)(p.getX()*zoom);
  54. int y = height/2+(int)(p.getY()*zoom)*-1;
  55. if (visual_method == 0) {
  56. g.setColor(Color.getHSBColor((float) (hading/(2.0*Math.PI)), 1, 1));
  57. g.fillOval(
  58. x-(int)(zoom/2.0*1.5),
  59. y-(int)(zoom/2.0*1.5),
  60. (int)(zoom*1.5),
  61. (int)(zoom*1.5)
  62. );
  63. } else if (visual_method == 1) {
  64. g.setColor(Color.RED);
  65. g.drawLine(
  66. width/2+(int)(p.getX()*zoom),
  67. height/2-(int)(p.getY()*zoom),
  68. width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom),
  69. height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom)
  70. );
  71. g.drawLine(
  72. width/2+(int)(p.getX()*zoom+zoom*Math.sin(hading)),
  73. height/2-(int)(p.getY()*zoom-zoom*Math.cos(hading)),
  74. width/2+(int)(p.getX()*zoom+0.6*zoom*Math.sin(Math.PI/8+hading)),
  75. height/2-(int)(p.getY()*zoom-0.6*zoom*Math.cos(Math.PI/8+hading))
  76. );
  77. } else if (visual_method == 2) {
  78. g.setColor(Color.RED);
  79. g.fillOval(
  80. x-(int)(zoom/2.0*1.5),
  81. y-(int)(zoom/2.0*1.5),
  82. (int)(zoom*1.5),
  83. (int)(zoom*1.5)
  84. );
  85. g.setColor(Color.BLACK);
  86. g.drawLine(
  87. width/2+(int)(p.getX()*zoom),
  88. height/2-(int)(p.getY()*zoom),
  89. width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom),
  90. height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom)
  91. );
  92. }
  93. if (line && count != 0) {
  94. g.setColor(Color.LIGHT_GRAY);
  95. g.drawLine(x_tmp, y_tmp, x, y);
  96. }
  97. x_tmp = x;
  98. y_tmp = y;
  99. count++;
  100. }
  101. g.setColor(Color.RED);
  102. for (Pose p : lista_ultra) {
  103. int x = width/2+(int)(p.getX()*zoom);
  104. int y = height/2+(int)(p.getY()*zoom)*-1;
  105. g.fillRect(
  106. x-(int)(zoom/2.0*1.0),
  107. y-(int)(zoom/2.0*1.0),
  108. (int)(zoom*1.0),
  109. (int)(zoom*1.0)
  110. );
  111. }
  112. if (map != null) {
  113. Line[] lines = map.getLines();
  114. for (int i = 0; i < lines.length; i++) {
  115. Line l = lines[i];
  116. g.drawLine(
  117. width/2+(int)(l.x1*zoom),
  118. height/2-(int)(l.y1*zoom),
  119. width/2+(int)(l.x2*zoom),
  120. height/2-(int)(l.y2*zoom)
  121. );
  122. }
  123. }
  124. }
  125. @Override
  126. protected void paintComponent(Graphics g) {
  127. int width = (int) (getWidth());
  128. int height = (int) (getHeight());
  129. int width2 = (int) (getWidth()+2*centerx);
  130. int height2 = (int) (getHeight()+2*centery);
  131. super.paintComponent(g);
  132. g.setColor(new Color(20, 20, 20));
  133. int initial_x = height2/2;
  134. while (initial_x < width) {
  135. initial_x += grid*zoom;
  136. g.drawLine(0, initial_x, width, initial_x);
  137. }
  138. initial_x = height2/2;
  139. while (initial_x > 0) {
  140. initial_x -= grid*zoom;
  141. g.drawLine(0, initial_x, width, initial_x);
  142. }
  143. int initial_y = width2/2;
  144. while (initial_y < width) {
  145. initial_y += grid*zoom;
  146. g.drawLine(initial_y, 0, initial_y, height);
  147. }
  148. initial_y = width2/2;
  149. while (initial_y > 0) {
  150. initial_y -= grid*zoom;
  151. g.drawLine(initial_y, 0, initial_y, height);
  152. }
  153. g.setColor(Color.ORANGE);
  154. g.drawLine(width2/2, 0, width2/2, height);
  155. g.drawLine(0, height2/2, width, height2/2);
  156. if (semaphore.tryAcquire()) {
  157. drawModel(g);
  158. semaphore.release();
  159. }
  160. }
  161. /**
  162. * Adiciona um ponto ao mapa
  163. * @param p ponto
  164. */
  165. public void addPoint(Pose p) {
  166. if (semaphore.tryAcquire()) {
  167. lista_pontos.clear();
  168. lista_pontos.add(p);
  169. semaphore.release();
  170. }
  171. repaint();
  172. }
  173. public void addPoint(float x, float y, float z) {
  174. if (semaphore.tryAcquire()) {
  175. lista_pontos.add(new Pose(x, y, z));
  176. semaphore.release();
  177. }
  178. repaint();
  179. }
  180. public void addRead(float x, float y) {
  181. if (semaphore.tryAcquire()) {
  182. lista_ultra.add(new Pose(x, y, 0));
  183. semaphore.release();
  184. }
  185. repaint();
  186. }
  187. public void addPoint(double x, double y, double z) {
  188. addPoint((float)x, (float)y, (float)z);
  189. }
  190. public void addRead(double x, double y) {
  191. addRead((float)x, (float)y);
  192. }
  193. public void showLine () {
  194. line = !line;
  195. repaint();
  196. }
  197. public void setVisual (int method) {
  198. visual_method = method;
  199. repaint();
  200. }
  201. public void save () {
  202. Integer name = new Integer((int) (Math.random()*1000000));
  203. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  204. Graphics g = imagebuf.createGraphics();
  205. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  206. print(g);
  207. try {
  208. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  209. JOptionPane.showMessageDialog(null, "Image saved.");
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. JOptionPane.showMessageDialog(null, "Image not saved.");
  213. }
  214. }
  215. public void clean() {
  216. if (semaphore.tryAcquire()) {
  217. lista_pontos.clear();
  218. lista_ultra.clear();
  219. semaphore.release();
  220. }
  221. repaint();
  222. }
  223. @Override
  224. public void mouseDragged(MouseEvent e) {
  225. centerx += e.getX() - mousePt.x;
  226. centery += e.getY() - mousePt.y;
  227. mousePt = e.getPoint();
  228. repaint();
  229. }
  230. @Override
  231. public void mouseMoved(MouseEvent e) {
  232. }
  233. @Override
  234. public void mouseClicked(MouseEvent e) {
  235. }
  236. @Override
  237. public void mousePressed(MouseEvent e) {
  238. mousePt = e.getPoint();
  239. repaint();
  240. }
  241. @Override
  242. public void mouseReleased(MouseEvent e) {
  243. }
  244. @Override
  245. public void mouseEntered(MouseEvent e) {
  246. }
  247. @Override
  248. public void mouseExited(MouseEvent e) {
  249. }
  250. @Override
  251. public void mouseWheelMoved(MouseWheelEvent e) {
  252. if(e.getWheelRotation()<0){
  253. if (zoom < 15.0)
  254. zoom *= 1.1;
  255. repaint();
  256. }
  257. //Zoom out
  258. if(e.getWheelRotation()>0){
  259. if (zoom > 1.0)
  260. zoom /= 1.1;
  261. repaint();
  262. }
  263. }
  264. }