5006dba518b80017145c9b6064c27648 7.3 KB

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