Событие джойстика в Java
У меня есть Java-приложение, содержащее одно текстовое поле, и я использую джойстик.
Как я могу что-то сделать в текстовом поле, когда нажата кнопка на джойстике?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package op;
import net.java.games.input.Controller;
/**
*
* @author Ahmed
*/
public class Test extends javax.swing.JFrame {
/**
* Creates new form Test
*/
public Test() {
initComponents();
con();
}
private void con(){
JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
if( !joystick.isControllerConnected() ){
txt.setText("Not Connected");
}
else
txt.setText(joystick.getControllerType()+" "+joystick.getControllerName()+" Controller Cound!");
if( !joystick.pollController() ) {
txt.setText("Controller disconnected!");
}
// Number of buttons.
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txt = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
.addGap(88, 88, 88))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(145, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JTextField txt;
// End of variables declaration
}
1 ответ
Основываясь на учебном проекте здесь: https://theuzo007.wordpress.com/2013/10/26/joystick-in-java-with-jinput-v2/ похоже, что вы должны зацикливаться и просто проверять вещи, поэтому
private void con(){
JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
while (joystick.isControllerConnected()) {
// Go trough all components of the controller.
Component[] components = joystick.getComponents();
for(int i=0; i < components.length; i++) {
Component component = components[i];
Identifier componentIdentifier = component.getIdentifier();
// Buttons
if(componentIdentifier.getName().matches("^[0-9]*$")){ // If the component identifier name contains only numbers, then this is a button.
// Is button pressed?
if(component.getPollData() != 0.0f) {
txt.setText("Button got pressed!")
}
}
}
// We have to give processor some rest.
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
Logger.getLogger(JoystickTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
txt.setText("Controller not connected");
}