302f98100ab9001711c7a09600ea5d68 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/20.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. System.out.println(pose.getHeading());
  53. double sin = Math.sin(Math.toRadians(pose.getHeading()));
  54. double cos = Math.cos(Math.toRadians(pose.getHeading()));
  55. g.drawLine(
  56. (int)(w+(l.x1/10.0-pose.getX())*d*sin),
  57. (int)(h-(l.y1/10.0-pose.getY())*d*cos),
  58. (int)(w+(l.x2/10.0-pose.getX())*d*sin),
  59. (int)(h-(l.y2/10.0-pose.getY())*d*cos)
  60. );
  61. }
  62. }
  63. drawDots(g, w, h);
  64. semaphore.release();
  65. }
  66. }
  67. private void drawDots(Graphics g, int w, int h) {
  68. g.setColor(new Color(1f, 0f, 0f));
  69. int oval_size = 16;
  70. int distance = h*2/25;
  71. double d = distance/20.0;
  72. double a = -oval_size/4;
  73. for (SonarRead r: lista_leituras) {
  74. double x = a+(d*r.distance)*Math.sin(Math.toRadians(r.ang));
  75. double y = a+(d*r.distance)*Math.cos(Math.toRadians(r.ang));
  76. x = w - x;
  77. y = h - y;
  78. g.fillOval((int)(x-oval_size/2.0), (int)(y-oval_size/2.0), oval_size/2, oval_size/2);
  79. }
  80. if (map == null) return;
  81. }
  82. public Pose getPose() {
  83. return pose;
  84. }
  85. public void addRead(Pose p, double distance, double ang) {
  86. if (semaphore.tryAcquire()) {
  87. if (pose == null || !(p.getX() == pose.getX() && p.getY() == pose.getY()
  88. && p.getHeading() == pose.getHeading())) {
  89. pose = new Pose(p.getX(), p.getY(), p.getHeading());
  90. lista_leituras.clear();
  91. }
  92. lista_leituras.add(new SonarRead(distance, ang+90));
  93. semaphore.release();
  94. }
  95. repaint();
  96. }
  97. public ScannerImage(LineMap map) {
  98. this.map = map;
  99. semaphore = new Semaphore(1);
  100. lista_leituras = new ArrayList<SonarRead>();
  101. }
  102. }