Java Swing应用程序和Leap Motion Listener之间的通信

时间:2015-01-17 00:10:30

标签: java swing leap-motion

我想知道如何在Java Swing Application和我的leap motion listener之间进行通信。

因为我想在我的应用程序中单击一个按钮时,我可以通过跳跃动作看到手指数来改变数字。

我有一个Java Swing应用程序:

public class KidCountingFrame extends JFrame

一个Leap Motion Runnable:

public class LeapMouse implements Runnable
{
    @Override
    public void run() 
    {
        CustomListener l = new CustomListener();
        Controller c = new Controller();
        c.addListener(l);
    }
}

启动Leap Motion Listener ......:

public class CustomListener extends Listener 

也许我必须使用设计模式?

*更新:*

我尝试在项目中应用ProgressBarDemo并遵循说明。

但是当我将侦听器放在SwingWorker构造函数中时会发生一个错误:

Exception in thread "Thread-1443" java.lang.NullPointerException: null upcall object

这是我更新的代码:

public class PanelLeapFingers  extends JPanel implements ActionListener,
PropertyChangeListener 
{
      private JButton digitDisplayButton;
      private Task task;

      class Task extends SwingWorker<Void, Void>
      {

            public Task()
            {
                try
                {
                    NbFingersListener l = new NbFingersListener();
                    Controller c = new Controller();
                    c.addListener(l);
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }
            }

            @Override
            public Void doInBackground() 
            {
                // In background :
                // Find how digit fingers are shown by the user
                int progress = 0;
                //setProgress(????); //I don't really know how call the listener here
                setProgress(5); //Here it's just to make a test

                return null;
            }

            @Override
            public void done() 
            {
              Toolkit.getDefaultToolkit().beep();
              digitDisplayButton.setEnabled(true);
              setCursor(null); // turn off the wait cursor
            }
    }

      public PanelLeapFingers()
      {
        super(new BorderLayout());
        digitDisplayButton = new JButton("?");
        digitDisplayButton.setFont(new Font("Arial", Font.PLAIN, 40));
        digitDisplayButton.setActionCommand("start");
        digitDisplayButton.addActionListener(this);

        JPanel panel = new JPanel();
        panel.add(digitDisplayButton);
        add(panel, BorderLayout.PAGE_START);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

      }

      public void actionPerformed(ActionEvent evt) 
      {
        digitDisplayButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
      }

      public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
          int progress = (Integer) evt.getNewValue();
          digitDisplayButton.setText(progress+"");
        }
      }

}

我不确定是否处于好的状态,我不明白如何在setProgress()函数中从听众那里接收信息。

编辑:    解决方案:最后我决定使用Singleton Model在侦听器和Java Swing APP之间进行通信。当Listener在Singleton模型中工作时,我保存所有信息,并且我在Java Swing APP中恢复了我需要的信息。

1 个答案:

答案 0 :(得分:3)

正如所讨论的hereListener将异步调用,通常来自另一个线程。为避免阻止event dispatch thread,请在SwingWorker的构造函数中创建Listener,并安排doInBackground()实施至publish()个感兴趣的帧;然后process()可以在事件派发线程上处理这些帧。

或者,在javax.swing.TimerController中以合适的价格轮询ActionListener