123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.event.MouseWheelEvent;
- import java.awt.event.MouseWheelListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import lejos.geom.Line;
- import lejos.robotics.mapping.LineMap;
- import lejos.robotics.navigation.Pose;
- public class MapImage extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener {
- private double zoom = 2.0; // pixel per cm
- private double grid = 10.0; // cm
- private double centerx = 0.0;
- private double centery = 0.0; // cm
- private Point mousePt;
- private ArrayList<Pose> lista_pontos;
- private ArrayList<Pose> lista_ultra;
- private int visual_method = 0;
- private boolean line = false;
-
- private LineMap map;
-
- public MapImage() {
- super();
- lista_pontos = new ArrayList<Pose>();
- lista_ultra = new ArrayList<Pose>();
- setBackground(Color.BLACK);
- addMouseWheelListener(this);
- addMouseListener(this);
- addMouseMotionListener(this);
- }
-
- public MapImage(LineMap map) {
- this();
- this.map = map;
- }
-
- private void drawModel (Graphics g) {
- int width = (int) (getWidth()+2*centerx);
- int height = (int) (getHeight()+2*centery);
- int count = 0;
- int x_tmp = 0, y_tmp = 0;
-
- for (Pose p : lista_pontos) {
- double hading = Math.toRadians(p.getHeading());
-
- int x = width/2+(int)(p.getX()*zoom);
- int y = height/2+(int)(p.getY()*zoom)*-1;
-
- if (visual_method == 0) {
- g.setColor(Color.getHSBColor((float) (hading/(2.0*Math.PI)), 1, 1));
- g.fillOval(
- x-(int)(zoom/2.0*1.5),
- y-(int)(zoom/2.0*1.5),
- (int)(zoom*1.5),
- (int)(zoom*1.5)
- );
- } else if (visual_method == 1) {
- g.setColor(Color.RED);
- g.drawLine(
- width/2+(int)(p.getX()*zoom),
- height/2-(int)(p.getY()*zoom),
- width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom),
- height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom)
- );
-
- g.drawLine(
- width/2+(int)(p.getX()*zoom+zoom*Math.sin(hading)),
- height/2-(int)(p.getY()*zoom-zoom*Math.cos(hading)),
- width/2+(int)(p.getX()*zoom+0.6*zoom*Math.sin(Math.PI/8+hading)),
- height/2-(int)(p.getY()*zoom-0.6*zoom*Math.cos(Math.PI/8+hading))
- );
- } else if (visual_method == 2) {
- g.setColor(Color.RED);
- g.fillOval(
- x-(int)(zoom/2.0*1.5),
- y-(int)(zoom/2.0*1.5),
- (int)(zoom*1.5),
- (int)(zoom*1.5)
- );
- g.setColor(Color.BLACK);
- g.drawLine(
- width/2+(int)(p.getX()*zoom),
- height/2-(int)(p.getY()*zoom),
- width/2+(int)(p.getX()*zoom+Math.sin(hading)*zoom),
- height/2-(int)(p.getY()*zoom-Math.cos(hading)*zoom)
- );
- }
-
- if (line && count != 0) {
- g.setColor(Color.LIGHT_GRAY);
- g.drawLine(x_tmp, y_tmp, x, y);
- }
-
- x_tmp = x;
- y_tmp = y;
- count++;
- }
-
- g.setColor(Color.RED);
- for (Pose p : lista_ultra) {
- int x = width/2+(int)(p.getX()*zoom);
- int y = height/2+(int)(p.getY()*zoom)*-1;
- g.fillRect(
- x-(int)(zoom/2.0*1.0),
- y-(int)(zoom/2.0*1.0),
- (int)(zoom*1.0),
- (int)(zoom*1.0)
- );
- }
-
- if (map != null) {
- Line[] lines = map.getLines();
- for (int i = 0; i < lines.length; i++) {
- Line l = lines[i];
- g.drawLine(
- width/2+(int)(l.x1*zoom),
- height/2-(int)(l.y1*zoom),
- width/2+(int)(l.x2*zoom),
- height/2-(int)(l.y2*zoom)
- );
- }
- }
- }
-
- @Override
- protected void paintComponent(Graphics g) {
- int width = (int) (getWidth());
- int height = (int) (getHeight());
- int width2 = (int) (getWidth()+2*centerx);
- int height2 = (int) (getHeight()+2*centery);
- super.paintComponent(g);
-
- g.setColor(new Color(20, 20, 20));
-
- int initial_x = height2/2;
- while (initial_x < width) {
- initial_x += grid*zoom;
- g.drawLine(0, initial_x, width, initial_x);
- }
- initial_x = height2/2;
- while (initial_x > 0) {
- initial_x -= grid*zoom;
- g.drawLine(0, initial_x, width, initial_x);
- }
- int initial_y = width2/2;
- while (initial_y < width) {
- initial_y += grid*zoom;
- g.drawLine(initial_y, 0, initial_y, height);
- }
- initial_y = width2/2;
- while (initial_y > 0) {
- initial_y -= grid*zoom;
- g.drawLine(initial_y, 0, initial_y, height);
- }
- g.setColor(Color.ORANGE);
- g.drawLine(width2/2, 0, width2/2, height);
- g.drawLine(0, height2/2, width, height2/2);
- drawModel(g);
- }
-
- /**
- * Adiciona um ponto ao mapa
- * @param p ponto
- */
- public void addPoint(Pose p) {
- lista_pontos.add(p);
- repaint();
- }
- public void addPoint(float x, float y, float z) {
- lista_pontos.add(new Pose(x, y, z));
- repaint();
- }
-
- public void addRead(float x, float y) {
- lista_ultra.add(new Pose(x, y, 0));
- repaint();
- }
-
- public void addPoint(double x, double y, double z) {
- addPoint((float)x, (float)y, (float)z);
- }
-
- public void addRead(double x, double y) {
- lista_ultra.add(new Pose(x, y, 0));
- repaint();
- }
-
-
- public void showLine () {
- line = !line;
- repaint();
- }
- public void setVisual (int method) {
- visual_method = method;
- 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.");
- }
- }
-
- public void clean() {
- lista_pontos.clear();
- lista_ultra.clear();
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- centerx += e.getX() - mousePt.x;
- centery += e.getY() - mousePt.y;
- mousePt = e.getPoint();
- repaint();
-
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
-
- @Override
- public void mouseClicked(MouseEvent e) {
- }
-
- @Override
- public void mousePressed(MouseEvent e) {
- mousePt = e.getPoint();
- repaint();
-
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
-
- @Override
- public void mouseEntered(MouseEvent e) {
- }
-
- @Override
- public void mouseExited(MouseEvent e) {
- }
-
- @Override
- public void mouseWheelMoved(MouseWheelEvent e) {
- if(e.getWheelRotation()<0){
- if (zoom < 15.0)
- zoom *= 1.1;
- repaint();
- }
- //Zoom out
- if(e.getWheelRotation()>0){
- if (zoom > 1.0)
- zoom /= 1.1;
- repaint();
- }
- }
- }
|