Hi all.
I’m developing a program to control a robot with a gamepad as a graduation task. It works well under a controlled enviroment, but I want to make the program be aware whether the controller is connected or not. The code is not running so well, and I’m not too good at debugging with NetBeans, so I want to know if you can help me with this. The only conclusion I could gather is that he keeps doing what he was doing before after I connected/disconnected the controller, but he will know if the controller is connected or not at the beginning of execution.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Tela extends javax.swing.JFrame {
public Tela() {
// initialization
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e){
pollTimer.stop();
System.exit(0);
}
});
startPolling();
}
private void startPolling(){
ActionListener pollPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e){
if(controle.checa() == 1){ // returns the number of Controller.Type.STICK or Controller.Type.GAMEPAD controllers connected
// interface update
}else if(controle.checa() == 0){
// prints that there are no controllers
}else{
// prints that there are too much controllers
}
}
};
pollTimer = new Timer(DELAY, pollPerformer);
pollTimer.start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code"> // NetBeans generated code for interface elements
// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Tela().setVisible(true);
}
});
}
// Variables declaration - do not modify // NetBeans generated code for interface elements
// End of variables declaration
private Controle controle = new Controle(); // class wrapped around JInput
private Timer pollTimer;
private static final int DELAY = 20;
}