a032661708b9001711c7a09600ea5d68 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.util.ArrayList;
  4. import java.util.concurrent.Semaphore;
  5. import javax.swing.JPanel;
  6. import lejos.geom.Line;
  7. import lejos.robotics.mapping.LineMap;
  8. import lejos.robotics.navigation.Pose;
  9. public class ScannerImage extends JPanel {
  10. private Pose pose;
  11. private LineMap map;
  12. private ArrayList<SonarRead> lista_leituras;
  13. private Semaphore semaphore;
  14. class SonarRead {
  15. public double distance;
  16. public double ang;
  17. SonarRead (double distance, double ang) {
  18. this.ang = ang;
  19. this.distance = distance;
  20. }
  21. }
  22. @Override
  23. protected void paintComponent(Graphics g) {
  24. int w = this.getWidth()/2;
  25. int h = this.getHeight();
  26. int distance = h*2/25;
  27. g.setColor(new Color(0f, 0f, 0f, 0.4f));
  28. for (int i = 1; i <= 18; i++) {
  29. int r = distance * i;
  30. g.drawArc(w-r, h-r, 2*r, 2*r, 0, 180);
  31. g.drawString(new Integer(i*20).toString(), w-7, h-r);
  32. }
  33. g.setColor(new Color(0f, 0f, 0f, 0.5f));
  34. for (int i = 1; i < 6; i++) {
  35. int lw = (int)(Math.cos(Math.PI/6.0*i)*distance * 18);
  36. int lh = (int)(Math.sin(Math.PI/6.0*i)*distance * 18);
  37. g.drawLine(w, h, lw+w, h-lh);
  38. }
  39. g.setColor(new Color(0f, 0f, 0f, 0.1f));
  40. for (int i = 1; i < 12; i++) {
  41. int lw = (int)(Math.cos(Math.PI/12.0*i)*distance * 18);
  42. int lh = (int)(Math.sin(Math.PI/12.0*i)*distance * 18);
  43. g.drawLine(w, h, lw+w, h-lh);
  44. }
  45. if (semaphore.tryAcquire()) {
  46. double d = h*2.0/25.0/200.0;
  47. g.setColor(new Color(0f, 1f, 0f));
  48. if (map != null && pose!= null) {
  49. Line[] lines = map.getLines();
  50. for (int i = 0; i < lines.length; i++) {
  51. Line l = lines[i];
  52. g.drawLine(
  53. (int)(w+(l.x1-pose.getX())*d),
  54. (int)(h-(l.y1-pose.getY())*d),
  55. (int)(w+(l.x2-pose.getX())*d),
  56. (int)(h-(l.y2-pose.getY())*d)
  57. );
  58. }
  59. }
  60. drawDots(g, w, h);
  61. semaphore.release();
  62. }
  63. }
  64. private void drawDots(Graphics g, int w, int h) {
  65. g.setColor(new Color(1f, 0f, 0f));
  66. int oval_size = 30;
  67. int distance = h*2/25;
  68. double d = distance/20.0;
  69. double a = -oval_size/4;
  70. for (SonarRead r: lista_leituras) {
  71. double x = a+(d*r.distance)*Math.sin(Math.toRadians(r.ang));
  72. double y = a+(d*r.distance)*Math.cos(Math.toRadians(r.ang));
  73. x = w - x;
  74. y = h - y;
  75. g.fillOval((int)(x-oval_size/2.0), (int)(y-oval_size/2.0), oval_size/2, oval_size/2);
  76. }
  77. if (map == null) return;
  78. }
  79. public Pose getPose() {
  80. return pose;
  81. }
  82. public void addRead(Pose p, double distance, double ang) {
  83. if (semaphore.tryAcquire()) {
  84. if (pose == null || !(p.getX() == pose.getX() && p.getY() == pose.getY()
  85. && p.getHeading() == pose.getHeading())) {
  86. pose = new Pose(p.getX(), p.getY(), p.getHeading());
  87. lista_leituras.clear();
  88. }
  89. lista_leituras.add(new SonarRead(distance, ang+90));
  90. semaphore.release();
  91. }
  92. repaint();
  93. }
  94. public ScannerImage(LineMap map) {
  95. this.map = map;
  96. semaphore = new Semaphore(1);
  97. lista_leituras = new ArrayList<SonarRead>();
  98. }
  99. }