f0f5d5d0e2b000171eb4aea94a5e3c6b 3.6 KB

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