306eeab11eb80017145c9b6064c27648 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import java.awt.BorderLayout;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.KeyListener;
  4. import java.awt.event.WindowEvent;
  5. import java.awt.event.WindowListener;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import javax.bluetooth.BluetoothConnectionException;
  10. import javax.swing.JFrame;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JSplitPane;
  14. import javax.swing.SwingUtilities;
  15. import javax.swing.event.AncestorEvent;
  16. import javax.swing.event.AncestorListener;
  17. import config.Map;
  18. import lejos.pc.comm.NXTComm;
  19. import lejos.pc.comm.NXTCommException;
  20. import lejos.pc.comm.NXTCommFactory;
  21. import lejos.pc.comm.NXTInfo;
  22. import lejos.robotics.mapping.LineMap;
  23. import lejos.robotics.navigation.Pose;
  24. import robots.BluetoothRobot;
  25. import robots.DataPose;
  26. import robots.Robot;
  27. import robots.RobotReturn;
  28. import robots.VirtualRobot;
  29. public class MainProgram extends JPanel implements KeyListener, WindowListener, RobotReturn {
  30. private MapImage imap;
  31. private Robot robot;
  32. public static final byte FORWARD = 0;
  33. public static final byte STOP = 1;
  34. public static final byte EXIT = 2;
  35. public static final byte LEFT = 3;
  36. public static final byte RIGHT = 4;
  37. public static final byte BACKWARD = 5;
  38. public MainProgram(LineMap map, Robot robot) {
  39. this.robot = robot;
  40. JFrame frame = new JFrame("Mapa MAC0318");
  41. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. frame.setLayout(new BorderLayout());
  43. imap = new MapImage(map);
  44. ScannerImage scanner = new ScannerImage();
  45. // frame.add(this.map);
  46. frame.setSize(800, 800);
  47. frame.setVisible(true);
  48. frame.setFocusable(true);
  49. frame.requestFocusInWindow();
  50. frame.addKeyListener(this);
  51. frame.addWindowListener(this);
  52. JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scanner, imap);
  53. splitPane.setOneTouchExpandable(true);
  54. splitPane.setDividerLocation((int) (frame.getHeight() / 2));
  55. frame.add(splitPane);
  56. if (robot != null)
  57. robot.connect(this);
  58. //
  59. // String text = "1,2,3 - Change view mode.\n";
  60. // text += "s - Save image.\n";
  61. // text += "l - Show trace.\n";
  62. // text += "c - Clean map.\n";
  63. // JOptionPane.showMessageDialog(null, text);
  64. }
  65. public void addPoint(Pose p) {
  66. imap.addPoint(p);
  67. }
  68. @Override
  69. public void keyPressed(KeyEvent e) {
  70. char input = e.getKeyChar();
  71. if (input == '1')
  72. imap.setVisual(0);
  73. else if (input == '2')
  74. imap.setVisual(1);
  75. else if (input == '3')
  76. imap.setVisual(2);
  77. else if (input == 'l')
  78. imap.showLine();
  79. else if (input == 's')
  80. imap.save();
  81. else if (input == 'c')
  82. imap.clean();
  83. if (robot == null)
  84. return;
  85. switch (e.getKeyCode()) {
  86. case KeyEvent.VK_UP:
  87. robot.moveForward();
  88. break;
  89. case KeyEvent.VK_DOWN:
  90. robot.moveBackward();
  91. break;
  92. case KeyEvent.VK_LEFT:
  93. robot.moveLeft();
  94. break;
  95. case KeyEvent.VK_RIGHT:
  96. robot.moveRight();
  97. break;
  98. }
  99. }
  100. @Override
  101. public void keyReleased(KeyEvent arg0) {
  102. if (robot == null)
  103. return;
  104. robot.stop();
  105. }
  106. @Override
  107. public void keyTyped(KeyEvent arg0) {
  108. }
  109. public static void main(String[] args) throws NXTCommException, IOException {
  110. LineMap map = Map.makeMap();
  111. Robot robot = new VirtualRobot(map);
  112. //Robot robot = new BluetoothRobot("NXJ8");
  113. SwingUtilities.invokeLater(new Runnable() {
  114. public void run() {
  115. new MainProgram(map, robot);
  116. }
  117. });
  118. }
  119. @Override
  120. public void windowOpened(WindowEvent e) {
  121. }
  122. @Override
  123. public void windowClosing(WindowEvent e) {
  124. System.err.println("Fechando...");
  125. if (robot == null)
  126. return;
  127. robot.exit();
  128. }
  129. @Override
  130. public void windowClosed(WindowEvent e) {
  131. }
  132. @Override
  133. public void windowIconified(WindowEvent e) {
  134. }
  135. @Override
  136. public void windowDeiconified(WindowEvent e) {
  137. }
  138. @Override
  139. public void windowActivated(WindowEvent e) {
  140. }
  141. @Override
  142. public void windowDeactivated(WindowEvent e) {
  143. }
  144. @Override
  145. public void robotData(DataPose data) {
  146. // TODO Auto-generated method stub
  147. }
  148. }