d0b3319f18b80017145c9b6064c27648 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. lista_ultra.add(new Pose(x, y, 0));
  176. repaint();
  177. }
  178. public void showLine () {
  179. line = !line;
  180. repaint();
  181. }
  182. public void setVisual (int method) {
  183. visual_method = method;
  184. repaint();
  185. }
  186. public void save () {
  187. Integer name = new Integer((int) (Math.random()*1000000));
  188. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  189. Graphics g = imagebuf.createGraphics();
  190. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  191. print(g);
  192. try {
  193. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  194. JOptionPane.showMessageDialog(null, "Image saved.");
  195. } catch (IOException e) {
  196. e.printStackTrace();
  197. JOptionPane.showMessageDialog(null, "Image not saved.");
  198. }
  199. }
  200. public void clean() {
  201. lista_pontos.clear();
  202. lista_ultra.clear();
  203. repaint();
  204. }
  205. @Override
  206. public void mouseDragged(MouseEvent e) {
  207. centerx += e.getX() - mousePt.x;
  208. centery += e.getY() - mousePt.y;
  209. mousePt = e.getPoint();
  210. repaint();
  211. }
  212. @Override
  213. public void mouseMoved(MouseEvent e) {
  214. }
  215. @Override
  216. public void mouseClicked(MouseEvent e) {
  217. }
  218. @Override
  219. public void mousePressed(MouseEvent e) {
  220. mousePt = e.getPoint();
  221. repaint();
  222. }
  223. @Override
  224. public void mouseReleased(MouseEvent e) {
  225. }
  226. @Override
  227. public void mouseEntered(MouseEvent e) {
  228. }
  229. @Override
  230. public void mouseExited(MouseEvent e) {
  231. }
  232. @Override
  233. public void mouseWheelMoved(MouseWheelEvent e) {
  234. if(e.getWheelRotation()<0){
  235. if (zoom < 15.0)
  236. zoom *= 1.1;
  237. repaint();
  238. }
  239. //Zoom out
  240. if(e.getWheelRotation()>0){
  241. if (zoom > 1.0)
  242. zoom /= 1.1;
  243. repaint();
  244. }
  245. }
  246. }