ScannerImage.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. public class ScannerImage extends JPanel {
  12. private ArrayList<SonarRead> lista_leituras;
  13. private Semaphore semaphore;
  14. class SonarRead {
  15. public double distance;
  16. public double ang;
  17. public double expected;
  18. SonarRead(double distance, double ang) {
  19. this.ang = ang;
  20. this.distance = distance;
  21. }
  22. }
  23. @Override
  24. protected void paintComponent(Graphics g) {
  25. int w = this.getWidth() / 2;
  26. int h = this.getHeight();
  27. super.paintComponent(g);
  28. int distance = h * 2 / 25;
  29. g.setColor(new Color(0f, 0f, 0f, 0.4f));
  30. for (int i = 1; i <= 18; i++) {
  31. int r = distance * i;
  32. g.drawArc(w - r, h - r, 2 * r, 2 * r, 0, 180);
  33. g.drawString(new Integer(i * 20).toString(), w - 7, h - r);
  34. }
  35. g.setColor(new Color(0f, 0f, 0f, 0.5f));
  36. for (int i = 1; i < 6; i++) {
  37. int lw = (int) (Math.cos(Math.PI / 6.0 * i) * distance * 18);
  38. int lh = (int) (Math.sin(Math.PI / 6.0 * i) * distance * 18);
  39. g.drawLine(w, h, lw + w, h - lh);
  40. }
  41. g.setColor(new Color(0f, 0f, 0f, 0.1f));
  42. for (int i = 1; i < 12; i++) {
  43. int lw = (int) (Math.cos(Math.PI / 12.0 * i) * distance * 18);
  44. int lh = (int) (Math.sin(Math.PI / 12.0 * i) * distance * 18);
  45. g.drawLine(w, h, lw + w, h - lh);
  46. }
  47. if (semaphore.tryAcquire()) {
  48. drawDots(g, w, h);
  49. semaphore.release();
  50. }
  51. }
  52. private void drawDots(Graphics g, int w, int h) {
  53. int oval_size = 16;
  54. int distance = h * 2 / 25;
  55. double d = distance / 20.0;
  56. double a = -oval_size / 4;
  57. double x, y;
  58. g.setColor(new Color(1f, 0f, 0f));
  59. for (SonarRead r : lista_leituras) {
  60. x = a + (d * r.distance) * Math.sin(Math.toRadians(r.ang));
  61. y = a + (d * r.distance) * Math.cos(Math.toRadians(r.ang));
  62. x = w - x;
  63. y = h - y;
  64. g.setColor(new Color(1f, 0f, 0f));
  65. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  66. x = a + (d * r.expected) * Math.sin(Math.toRadians(r.ang));
  67. y = a + (d * r.expected) * Math.cos(Math.toRadians(r.ang));
  68. x = w - x;
  69. y = h - y;
  70. g.setColor(new Color(0f, 0f, 1f));
  71. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  72. }
  73. }
  74. public void addRead(double distance, double ang) {
  75. if (semaphore.tryAcquire()) {
  76. lista_leituras.add(new SonarRead(distance, ang + 90));
  77. semaphore.release();
  78. }
  79. repaint();
  80. }
  81. public ScannerImage() {
  82. semaphore = new Semaphore(1);
  83. lista_leituras = new ArrayList<SonarRead>();
  84. }
  85. public void clean() {
  86. if (semaphore.tryAcquire()) {
  87. lista_leituras.clear();
  88. semaphore.release();
  89. }
  90. repaint();
  91. }
  92. public void save () {
  93. Integer name = new Integer((int) (Math.random()*1000000));
  94. BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  95. Graphics g = imagebuf.createGraphics();
  96. g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
  97. print(g);
  98. try {
  99. ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
  100. JOptionPane.showMessageDialog(null, "Image saved.");
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. JOptionPane.showMessageDialog(null, "Image not saved.");
  104. }
  105. }
  106. }