ScannerImage.java 4.6 KB

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