60e7755ef9b00017155fe9ac88a695a6 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import java.awt.BorderLayout;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.KeyListener;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import javax.swing.JFrame;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.SwingUtilities;
  11. import lejos.pc.comm.NXTComm;
  12. import lejos.pc.comm.NXTCommException;
  13. import lejos.pc.comm.NXTCommFactory;
  14. import lejos.pc.comm.NXTInfo;
  15. public class Map extends JPanel implements KeyListener {
  16. private ImageMap map;
  17. private DataOutputStream output;
  18. private DataInputStream input;
  19. private int before;
  20. public static final byte FORWARD = 0;
  21. public static final byte STOP = 1;
  22. public static final byte EXIT = 2;
  23. public static final byte LEFT = 3;
  24. public static final byte RIGHT = 4;
  25. public static final byte BACKWARD = 5;
  26. private class Receiver extends Thread {
  27. @Override
  28. public void run() {
  29. int bytes_valiable = -1;
  30. while(true) {
  31. try {
  32. bytes_valiable = input.available();
  33. } catch (IOException e1) {
  34. // TODO Auto-generated catch block
  35. e1.printStackTrace();
  36. }
  37. if (bytes_valiable >= 0) {
  38. try {
  39. if (input.readByte() != '@') continue;
  40. int angle = input.readByte();
  41. float alpha = input.readFloat();
  42. float x = input.readFloat();
  43. int distance = input.readByte();
  44. float y = input.readFloat();
  45. System.out.println(x+" "+y);
  46. } catch (IOException e1) {
  47. continue;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. public Map (DataInputStream in, DataOutputStream out) {
  54. JFrame frame = new JFrame("Mapa MAC0318");
  55. Receiver r = new Receiver();
  56. output = out;
  57. input = in;
  58. before = -1;
  59. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. frame.setLayout(new BorderLayout());
  61. map = new ImageMap();
  62. frame.add(this.map);
  63. frame.setSize(800, 800);
  64. frame.setVisible(true);
  65. frame.setFocusable(true);
  66. frame.requestFocusInWindow();
  67. frame.addKeyListener(this);
  68. r.start();
  69. String text = "1,2,3 - Change view mode.\n";
  70. text += "s - Save image.\n";
  71. text += "l - Show trace.\n";
  72. JOptionPane.showMessageDialog(null, text);
  73. }
  74. public void addPoint(Point3D p) {
  75. map.addPoint(p);
  76. }
  77. @Override
  78. public void keyPressed(KeyEvent e) {
  79. char input = e.getKeyChar();
  80. if (input == '1') map.setVisual(0);
  81. else if (input == '2') map.setVisual(1);
  82. else if (input == '3') map.setVisual(2);
  83. else if (input == 'l') map.showLine();
  84. else if (input == 's') map.save();
  85. switch(e.getKeyCode()) {
  86. case KeyEvent.VK_UP:
  87. sendCmd(FORWARD);
  88. break;
  89. case KeyEvent.VK_DOWN:
  90. sendCmd(BACKWARD);
  91. break;
  92. case KeyEvent.VK_LEFT:
  93. sendCmd(LEFT);
  94. break;
  95. case KeyEvent.VK_RIGHT :
  96. sendCmd(RIGHT);
  97. break;
  98. }
  99. }
  100. @Override
  101. public void keyReleased(KeyEvent arg0) {
  102. sendCmd(STOP);
  103. }
  104. @Override
  105. public void keyTyped(KeyEvent arg0) {
  106. // TODO Auto-generated method stub
  107. }
  108. private void sendCmd (int cmd) {
  109. if (before != cmd) {
  110. try {
  111. output.write(cmd);
  112. output.flush();
  113. System.out.println("Send cmd");
  114. } catch (IOException e1) {
  115. System.out.println("Erro send cmd");
  116. before = -1;
  117. sendCmd (cmd);
  118. }
  119. before = cmd;
  120. }
  121. }
  122. public static void main(String[] args) throws NXTCommException, IOException {
  123. NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  124. NXTInfo[] nxtInfo = nxtComm.search("NXT8"); //find brick with NXT_ID by doing a Bluetooth inquiry
  125. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  126. System.err.println("NO NXT found");
  127. System.exit(1);
  128. }
  129. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  130. System.err.println("Failed to open NXT");
  131. System.exit(1);
  132. }
  133. final DataInputStream input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  134. final DataOutputStream output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  135. SwingUtilities.invokeLater(new Runnable() {
  136. public void run() {
  137. new Map(input, output);
  138. }
  139. });
  140. }
  141. }