30dc958315b80017145c9b6064c27648 6.4 KB

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