0030ab1defb000171877feeee34c61ac 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 lejos.pc.comm.NXTComm;
  11. import lejos.pc.comm.NXTCommException;
  12. import lejos.pc.comm.NXTCommFactory;
  13. import lejos.pc.comm.NXTInfo;
  14. public class Map extends JPanel {
  15. private final class KeyboardEvent implements KeyListener {
  16. public void keyTyped(KeyEvent e) {
  17. }
  18. public void keyReleased(KeyEvent e) {
  19. }
  20. public void keyPressed(KeyEvent e) {
  21. char input = e.getKeyChar();
  22. if (input == '1') map.setVisual(0);
  23. else if (input == '2') map.setVisual(1);
  24. else if (input == '3') map.setVisual(2);
  25. else if (input == 'l') map.showLine();
  26. else if (input == 's') map.save();
  27. System.out.println(e.getKeyCode());
  28. switch(e.getKeyCode()) {
  29. case KeyEvent.VK_UP:
  30. sendthread.send(SendThread.FORWARD);
  31. break;
  32. case KeyEvent.VK_DOWN:
  33. sendthread.send(SendThread.BACKWARD);
  34. break;
  35. case KeyEvent.VK_LEFT:
  36. sendthread.send(SendThread.LEFT);
  37. break;
  38. case KeyEvent.VK_RIGHT :
  39. sendthread.send(SendThread.RIGHT);
  40. break;
  41. }
  42. }
  43. }
  44. private ImageMap map;
  45. private SendThread sendthread;
  46. private DataOutputStream output;
  47. public Map (DataInputStream input, DataOutputStream output) throws IOException {
  48. JFrame frame = new JFrame("Mapa MAC0318");
  49. KeyboardEvent event = new KeyboardEvent();
  50. output = this.output;
  51. sendthread = new SendThread(output);
  52. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53. frame.setLayout(new BorderLayout());
  54. this.map = new ImageMap();
  55. frame.add(this.map);
  56. frame.setSize(800, 800);
  57. frame.setVisible(true);
  58. frame.setFocusable(true);
  59. frame.requestFocusInWindow();
  60. frame.addKeyListener(event);
  61. String text = "1,2,3 - Change view mode.\n";
  62. text += "s - Save image.\n";
  63. text += "l - Show trace.\n";
  64. JOptionPane.showMessageDialog(null, text);
  65. int bytes_valiable = -1;
  66. while(true) {
  67. try {
  68. bytes_valiable = input.available();
  69. } catch (IOException e1) {
  70. // TODO Auto-generated catch block
  71. e1.printStackTrace();
  72. }
  73. if (bytes_valiable >= 0) {
  74. if (input.readByte() != '@') continue;
  75. int angle = input.readByte();
  76. float x = input.readFloat();
  77. int distance = input.readByte();
  78. float y = input.readFloat();
  79. System.out.println(x+" "+y);
  80. }
  81. }
  82. }
  83. public void addPoint(Point3D p) {
  84. map.addPoint(p);
  85. }
  86. public static void main(String[] args) throws NXTCommException, IOException {
  87. NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  88. NXTInfo[] nxtInfo = nxtComm.search("NXT8"); //find brick with NXT_ID by doing a Bluetooth inquiry
  89. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  90. System.err.println("NO NXT found");
  91. System.exit(1);
  92. }
  93. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  94. System.err.println("Failed to open NXT");
  95. System.exit(1);
  96. }
  97. DataInputStream input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  98. DataOutputStream output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  99. Map map = new Map(input, output);
  100. Map map = new Map(null, null);
  101. }
  102. }