0


I'm starting to toy a little with the Leap Motion Controller and made a small GUI in Swing. That means it's just a Frame with a Label on it which should show a text of what the Leap Motion sees. Unfortunately my programm simply breaks down after two seconds. I have no Exceptions or something like that. Here's my code:

public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;

public JLabel label;

public GUI(){
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.label = new JLabel("Waiting for Gestures");
    this.add(label);

    setVisible(true);
}

public class LeapListener extends Listener{
    JLabel label;

    LeapListener(JLabel label){
        this.label = label;
    }

    @Override
    public void onInit(Controller controller){

    }

    @Override
    public void onExit(Controller controller){

    }

    @Override
    public void onConnect(Controller controller){
        System.out.println("bin da");
        controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
        controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
        controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
        controller.enableGesture(Gesture.Type.TYPE_SWIPE);
    }

    @Override
    public void onDisconnect(Controller controller){

    }

    @Override
    public void onFrame(Controller controller){
        Frame frame = controller.frame();

        GestureList glist = frame.gestures();

        for (int i = 0; i < glist.count(); i++){
            Gesture g = glist.get(i);

            switch (g.type()){
            case TYPE_CIRCLE:
                CircleGesture circle = new CircleGesture(g);
                this.label.setText("Mach mal nen Kreis!");
            case TYPE_KEY_TAP:
                KeyTapGesture key = new KeyTapGesture(g);
                this.label.setText("Schreiben?");
            case TYPE_SCREEN_TAP:
                ScreenTapGesture screen = new ScreenTapGesture(g);
                this.label.setText("Klicken?");
            case TYPE_SWIPE:
                SwipeGesture swipe = new SwipeGesture(g);
                this.label.setText("Da wurde gewischt!");
            default:
                System.out.println("nothing!");
            break;
            }
        }
    }
}

public static void main(String[] args) {
    GUI gui = new GUI();
    LeapListener listener;
    listener = gui.new LeapListener(gui.label);
    Controller controller = new Controller();
    controller.addListener(listener);

}

}

I have no idea what I've done wrong. The Windows-Error-Message says that the Java Platform SE binary is down (javaw.exe). Errormodule: Leap.dll Is it a mistake that I've made or is my whole setup f@*ked up?

Raistlin
  • 997
  • 2
  • 11
  • 33
  • Where do you actually add your `LeapListener`? What do you mean by "simply breaks"? – Josh M Sep 12 '13 at 12:51
  • The LeapListener is added in the very last line (controller.addListener(listener);) By "simply breaks" I mean that I don't get any java exceptions. The program simply crashes with a Windows-Error. – Raistlin Sep 12 '13 at 12:56

1 Answers1

0

Ok, I found a solution by myself but unfortunately I can't explain it.
I have to create the Controller Object in the class declaration, not in the main-method. Then I have to add the listener to the controller in the constructor of my GUI, like that:

public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;

public JLabel label;
public Controller c = new Controller();
public LeapListener listener;

public GUI(){
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.label = new JLabel("Waiting for Gestures");
    this.add(label);
    listener = new LeapListener(this.label);
    c.addListener(listener);
    setVisible(true);
}
    ...
}

So, I'm happy that I got it to work but maybe somebody is interested in the problem and may find a reason for the error I've had.

Raistlin
  • 997
  • 2
  • 11
  • 33
  • Once your main() function exits, any local variables, including the Controller object in your first version, go out of scope and are eligible for garbage collection. – Charles Ward Sep 17 '13 at 23:03