2016-11-03 16 views
0

Я строю робот-симулятор для имитации того, как робот будет двигаться и реагировать на трение и другие внешние события. У меня есть работа с помощью клавиш со стрелками для ввода, но я пытаюсь заставить его работать с джойстиком. Я использую slick2D за рекомендацию. Я никогда не использовал slick2D и очень смущен о том, как заставить мою программу работать. Какие классы в slick я должен использовать?Управление джойстиком Java

ответ

0

Согласно Slick2D's forums, JInput является предпочтительным методом работы с джойстиком.

В соответствии с java-gaming.org JInput требует, чтобы родные библиотеки работали (.dll, .so или .dylib, в зависимости от вашей платформы).

Вот пример скопирована из Java-игр:

/* Gets a list of the controllers JInput knows about and can interact with */ 
    Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers(); 

    for(int i =0;i<ca.length;i++){ 

     /* Get the name of the controller */ 
     System.out.println(ca[i].getName()); 

     /* Gets the type of the controller, e.g. GAMEPAD, MOUSE, KEYBOARD */ 
     System.out.println("Type: "+ca[i].getType().toString()); 

     /* Get this controllers components (buttons and axis) */ 
     Component[] components = ca[i].getComponents(); 
     System.out.println("Component Count: "+components.length); 
     for(int j=0;j<components.length;j++){ 

      /* Get the components name */ 
      System.out.println("Component "+j+": "+components[j].getName()); 

      /* Gets its identifier, E.g. BUTTON.PINKIE, AXIS.POV and KEY.Z */ 

      System.out.println(" Identifier: "+ components[j].getIdentifier().getName()); 

      /* Display if component is relative, absolute, analog, digital */ 
      System.out.print(" ComponentType: "); 
      if (components[j].isRelative()) { 
       System.out.print("Relative"); 
      } else { 
       System.out.print("Absolute"); 
      } 
      if (components[j].isAnalog()) { 
       System.out.print(" Analog"); 
      } else { 
       System.out.print(" Digital"); 
      } 
     } 
    } 

Получение значения требует опроса устройства или с помощью событий очередями.

Предупреждение: этот следующий блок написан в бесконечном цикле, который может блокировать выполнение другого кода, хороший кандидат для выделенного потока.

while(true) { 
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers(); 
    if(controllers.length==0) { 
     System.out.println("Found no controllers."); 
     break; 
    } 

    for(int i=0;i<controllers.length;i++) { 
     controllers[i].poll(); 

    EventQueue queue = controllers[i].getEventQueue(); 
    Event event = new Event(); 

    while(queue.getNextEvent(event)) { 
      StringBuffer buffer = new StringBuffer(controllers[i].getName()); 
      buffer.append(" at "); 
      buffer.append(event.getNanos()).append(", "); 
      Component comp = event.getComponent(); 
      buffer.append(comp.getName()).append(" changed to "); 
      float value = event.getValue(); 
      if(comp.isAnalog()) { 
       buffer.append(value); 
      } else { 
       if(value==1.0f) { 
       buffer.append("On"); 
       } else { 
       buffer.append("Off"); 
       } 
      } 
      System.out.println(buffer.toString()); 
     } 
    } 

    try { 
     Thread.sleep(20); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

 Смежные вопросы

  • Нет связанных вопросов^_^