Advertisement
jcdj1996

NXJ Remote Control Desktop App

Dec 13th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import lejos.nxt.Motor;
  2.  
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5.  
  6. import javax.swing.JFrame;
  7. public class desktop1 implements KeyListener{
  8.     JFrame frame;
  9.     static int dir = 0;
  10. public desktop1(){
  11.     frame = new JFrame();
  12.     frame.setSize(500,300);
  13.     frame.setLocation(300,300);
  14.     frame.setVisible(true);
  15.     frame.addKeyListener(this);
  16. }
  17.     public static void main(String[] args) {
  18.         // TODO Auto-generated method stub
  19.         desktop1 g1 = new desktop1();
  20.     }
  21.  
  22.    
  23.     public void keyPressed(KeyEvent e) {
  24.         // TODO Auto-generated method stub
  25.         char c = e.getKeyChar();
  26.         System.out.println(c);
  27.         if(c == 'a')
  28.         {
  29.             turnLeft();
  30.         }
  31.         if(c=='w')
  32.         {
  33.             dir=1;  //forward
  34.             move(dir);
  35.         }
  36.         if(c=='s')
  37.         {
  38.             dir=-1; //backward
  39.             move(dir);
  40.         }
  41.     }
  42.  
  43.    
  44.     public void keyReleased(KeyEvent e) {
  45.         // TODO Auto-generated method stub
  46.         char c = e.getKeyChar();
  47.         if(c=='w'||c=='s')  //if move key released, stop
  48.         {
  49.             dir=0;
  50.             move(dir);
  51.         }
  52.         if(c=='a')  //reset steering to forward
  53.         {
  54.             Motor.A.rotate(45);
  55.         }
  56.         if(c=='d')
  57.         {
  58.             Motor.A.rotate(-45);
  59.         }
  60.     }
  61.  
  62.    
  63.     public void keyTyped(KeyEvent e) {}
  64.     public void turnLeft()
  65.     {
  66.         Motor.A.rotate(-45);
  67.     }
  68.     public void turnRight()
  69.     {
  70.         Motor.A.rotate(45);
  71.     }
  72.     public void move(int direction)
  73.     {
  74.         Motor.B.setSpeed(direction*100);
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement