123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.concurrent.Semaphore;
- import javax.imageio.ImageIO;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- public class ScannerImage extends JPanel {
- private ArrayList<SonarRead> lista_leituras;
- private Semaphore semaphore;
- class SonarRead {
- public double distance;
- public double ang;
- public double expected;
- SonarRead(double distance, double ang) {
- this.ang = ang;
- this.distance = distance;
- }
- }
- @Override
- protected void paintComponent(Graphics g) {
- int w = this.getWidth() / 2;
- int h = this.getHeight();
- super.paintComponent(g);
- int distance = h * 2 / 25;
- g.setColor(new Color(0f, 0f, 0f, 0.4f));
- for (int i = 1; i <= 18; i++) {
- int r = distance * i;
- g.drawArc(w - r, h - r, 2 * r, 2 * r, 0, 180);
- g.drawString(new Integer(i * 20).toString(), w - 7, h - r);
- }
- g.setColor(new Color(0f, 0f, 0f, 0.5f));
- for (int i = 1; i < 6; i++) {
- int lw = (int) (Math.cos(Math.PI / 6.0 * i) * distance * 18);
- int lh = (int) (Math.sin(Math.PI / 6.0 * i) * distance * 18);
- g.drawLine(w, h, lw + w, h - lh);
- }
- g.setColor(new Color(0f, 0f, 0f, 0.1f));
- for (int i = 1; i < 12; i++) {
- int lw = (int) (Math.cos(Math.PI / 12.0 * i) * distance * 18);
- int lh = (int) (Math.sin(Math.PI / 12.0 * i) * distance * 18);
- g.drawLine(w, h, lw + w, h - lh);
- }
- if (semaphore.tryAcquire()) {
- drawDots(g, w, h);
- semaphore.release();
- }
- }
- private void drawDots(Graphics g, int w, int h) {
- int oval_size = 16;
- int distance = h * 2 / 25;
- double d = distance / 20.0;
- double a = -oval_size / 4;
- double x, y;
- g.setColor(new Color(1f, 0f, 0f));
- for (SonarRead r : lista_leituras) {
- x = a + (d * r.distance) * Math.sin(Math.toRadians(r.ang));
- y = a + (d * r.distance) * Math.cos(Math.toRadians(r.ang));
- x = w - x;
- y = h - y;
- g.setColor(new Color(1f, 0f, 0f));
- g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
- x = a + (d * r.expected) * Math.sin(Math.toRadians(r.ang));
- y = a + (d * r.expected) * Math.cos(Math.toRadians(r.ang));
- x = w - x;
- y = h - y;
- g.setColor(new Color(0f, 0f, 1f));
- g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
- }
- }
-
- public void addRead(double distance, double ang) {
- if (semaphore.tryAcquire()) {
- lista_leituras.add(new SonarRead(distance, ang + 90));
- semaphore.release();
- }
- repaint();
- }
- public ScannerImage() {
- semaphore = new Semaphore(1);
- lista_leituras = new ArrayList<SonarRead>();
- }
- public void clean() {
- if (semaphore.tryAcquire()) {
- lista_leituras.clear();
- semaphore.release();
- }
- repaint();
- }
- public void save () {
- Integer name = new Integer((int) (Math.random()*1000000));
- BufferedImage imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
- Graphics g = imagebuf.createGraphics();
- g.fillRect(0, 0, imagebuf.getWidth(), imagebuf.getHeight());
- print(g);
- try {
- ImageIO.write(imagebuf, "png", new File(name.toString()+".png"));
- JOptionPane.showMessageDialog(null, "Image saved.");
- } catch (IOException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Image not saved.");
- }
- }
- }
|