001f17bc27bb001711b8bf632416c20d 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.concurrent.Semaphore;
  8. import javax.imageio.ImageIO;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JPanel;
  11. import lejos.geom.Line;
  12. import lejos.robotics.mapping.LineMap;
  13. import lejos.robotics.navigation.Pose;
  14. public class ScannerImage extends JPanel {
  15. private Pose pose;
  16. private LineMap map;
  17. private ArrayList<SonarRead> lista_leituras;
  18. private Semaphore semaphore;
  19. class SonarRead {
  20. public double distance;
  21. public double ang;
  22. public double expected;
  23. SonarRead(double distance, double ang, double expected) {
  24. this.ang = ang;
  25. this.distance = distance;
  26. this.expected = expected;
  27. }
  28. }
  29. @Override
  30. protected void paintComponent(Graphics g) {
  31. int w = this.getWidth() / 2;
  32. int h = this.getHeight();
  33. int distance = h * 2 / 25;
  34. g.setColor(new Color(0f, 0f, 0f, 0.4f));
  35. for (int i = 1; i <= 18; i++) {
  36. int r = distance * i;
  37. g.drawArc(w - r, h - r, 2 * r, 2 * r, 0, 180);
  38. g.drawString(new Integer(i * 20).toString(), w - 7, h - r);
  39. }
  40. g.setColor(new Color(0f, 0f, 0f, 0.5f));
  41. for (int i = 1; i < 6; i++) {
  42. int lw = (int) (Math.cos(Math.PI / 6.0 * i) * distance * 18);
  43. int lh = (int) (Math.sin(Math.PI / 6.0 * i) * distance * 18);
  44. g.drawLine(w, h, lw + w, h - lh);
  45. }
  46. g.setColor(new Color(0f, 0f, 0f, 0.1f));
  47. for (int i = 1; i < 12; i++) {
  48. int lw = (int) (Math.cos(Math.PI / 12.0 * i) * distance * 18);
  49. int lh = (int) (Math.sin(Math.PI / 12.0 * i) * distance * 18);
  50. g.drawLine(w, h, lw + w, h - lh);
  51. }
  52. if (semaphore.tryAcquire()) {
  53. double d = h * 2.0 / 25.0 / 20.0;
  54. g.setColor(new Color(0f, 1f, 0f));
  55. if (map != null && pose != null) {
  56. Line[] lines = map.getLines();
  57. for (int i = 0; i < lines.length; i++) {
  58. Line l = lines[i];
  59. double sin = Math.sin(-Math.toRadians(pose.getHeading() - 180));
  60. double cos = Math.cos(-Math.toRadians(pose.getHeading() - 180));
  61. double x1 = (l.x1 - pose.getX()) * d;
  62. double y1 = (l.y1 - pose.getY()) * d;
  63. double x2 = (l.x2 - pose.getX()) * d;
  64. double y2 = (l.y2 - pose.getY()) * d;
  65. double xx1 = x1 * cos - y1 * sin;
  66. double yy1 = y1 * cos + x1 * sin;
  67. double xx2 = x2 * cos - y2 * sin;
  68. double yy2 = y2 * cos + x2 * sin;
  69. g.drawLine((int) (w + xx1), (int) (h - yy1), (int) (w + xx2), (int) (h - yy2));
  70. }
  71. }
  72. drawDots(g, w, h);
  73. semaphore.release();
  74. }
  75. }
  76. private void drawDots(Graphics g, int w, int h) {
  77. int oval_size = 16;
  78. int distance = h * 2 / 25;
  79. double d = distance / 20.0;
  80. double a = -oval_size / 4;
  81. double x, y;
  82. g.setColor(new Color(1f, 0f, 0f));
  83. for (SonarRead r : lista_leituras) {
  84. x = a + (d * r.distance) * Math.sin(Math.toRadians(r.ang));
  85. y = a + (d * r.distance) * Math.cos(Math.toRadians(r.ang));
  86. x = w - x;
  87. y = h - y;
  88. g.setColor(new Color(1f, 0f, 0f));
  89. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  90. x = a + (d * r.expected) * Math.sin(Math.toRadians(r.ang));
  91. y = a + (d * r.expected) * Math.cos(Math.toRadians(r.ang));
  92. x = w - x;
  93. y = h - y;
  94. g.setColor(new Color(0f, 0f, 1f));
  95. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  96. }
  97. if (map == null)
  98. return;
  99. }
  100. public Pose getPose() {
  101. return pose;
  102. }
  103. public void setPose (Pose p) {
  104. pose = p;
  105. repaint();
  106. }
  107. public void addRead(Pose p, double distance, double ang, double expected) {
  108. if (semaphore.tryAcquire()) {
  109. if (pose == null || lista_leituras.size() >= 180) {
  110. pose = new Pose(p.getX(), p.getY(), p.getHeading());
  111. lista_leituras.clear();
  112. }
  113. lista_leituras.add(new SonarRead(distance, ang + 90, expected));
  114. semaphore.release();
  115. }
  116. repaint();
  117. }
  118. public ScannerImage(LineMap map) {
  119. this.map = map;
  120. semaphore = new Semaphore(1);
  121. lista_leituras = new ArrayList<SonarRead>();
  122. }
  123. public void clean() {
  124. if (semaphore.tryAcquire()) {
  125. lista_leituras.clear();
  126. semaphore.release();
  127. }
  128. repaint();
  129. }
  130. public void save () {
  131. Integer name = new Integer((int) (Math.random()*1000000));
  132. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  133. Graphics g = imagebuf.createGraphics();
  134. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  135. print(g);
  136. try {
  137. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  138. JOptionPane.showMessageDialog(null, "Image saved.");
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. JOptionPane.showMessageDialog(null, "Image not saved.");
  142. }
  143. }
  144. }