2014-02-06 33 views
8

我有一個傳統的搖擺應用,我需要添加觸摸手勢,特別是捏縮放和觸摸和拖動。向傳統的搖擺應用添加觸摸手勢

我試過了JDK 8的SwingNode,我可以在那裏運行swing應用程序,但顯示性能降低了50%以上,這是行不通的。 MT4J中的SwingTextureRenderer具有相同的問題,甚至沒有嘗試將觸摸事件重新分配爲鼠標事件。

我想到了一種在頂部使用JavaFX圖層並捕獲觸摸事件並試圖將它們作爲鼠標事件分配給Swing應用程序的玻璃窗格方法。

有沒有人有另一種方法?目標平臺是Windows 8.

賞金來了,只要Stackoverflow打開它。我非常需要這個。

編輯:

這是我與SwingNode試過(鼠標再發運沒有工作)。該SwingNode的東西可能是從最好的解決辦法分心,從而忽略這一點,如果你有獲得觸摸到擺一個更好的主意:

@Override 
public void start(Stage stage) { 
    final SwingNode swingNode = new SwingNode(); 
    createAndSetSwingContent(swingNode); 

    StackPane pane = new StackPane(); 
    pane.getChildren().add(swingNode); 

    stage.setScene(new Scene(pane, 640, 480)); 
    stage.show(); 
} 

private void createAndSetSwingContent(final SwingNode swingNode) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      UILib.setPlatformLookAndFeel(); 

      // create GraphViewTouch 
      String datafile = null; 
      String label = "label"; 

      final JPanel frame = GraphView.demoFrameless(datafile, label); 
      swingNode.setContent(frame); 
      swingNode.setOnZoom(new EventHandler<ZoomEvent>() { 
       @Override public void handle(ZoomEvent event) { 
        MouseWheelEvent me = new MouseWheelEvent(frame, 1, System.currentTimeMillis(), 0, (int)Math.round(event.getSceneX()), (int)Math.round(event.getSceneY()), (int)Math.round(event.getScreenX()), (int)Math.round(event.getScreenY()), (int)Math.round(event.getZoomFactor()), false, MouseWheelEvent.WHEEL_UNIT_SCROLL, (int)Math.round(event.getZoomFactor()), (int)Math.round(event.getZoomFactor()), event.getZoomFactor()); 
        frame.dispatchEvent(me); 
        System.out.println("GraphView: Zoom event" + 
         ", inertia: " + event.isInertia() + 
         ", direct: " + event.isDirect()); 

        event.consume(); 
       } 
      }); 
     } 
    }); 
} 
+2

請提供代碼[最小示例](http://stackoverflow.com/help/mcve),它演示了使用SwingNode降低性能。 – jewelsea

+1

我使用SwingNode嘗試更新了問題 – davidethell

+1

@jewelsea建議提供[MCTaRE](http://stackoverflow.com/help/mcve)(最小完整測試和可讀示例)。請點擊鏈接並閱讀相關內容。它不是'不可編譯的代碼片段',它可以準備一個非常有用。 –

回答

0

嗯...這是一個艱難的一個,但有一個機會。

我會做的是使多個MouseListener類(相對於你想要拾取的鼠標事件的數量),並創建某種類型的系統來檢測某些調整,例如。 (變焦)

1聽衆:

public void mousePressed(MouseEvent e){ 
    //Set click to true 
    clk = true; 
    //set first mouse position 
    firstPos = window.getMousePosition(); 
    } 

    public void mouseReleased(MouseEvent e){ 

    //set second mouse position 
    secondPos = window.getMousePosition(); 
    } 

第二個偵聽

public void mousePressed(MouseEvent e){ 
    //set clicked to true 
    clk = true; 
    //set first mouse position (listener 2) 
    firstPos = window.getMousePosition(); 
    } 

    public void mouseReleased(MouseEvent e){ 

    //set second mouse position 
    secondPos = window.getMousePosition(); 
    } 

主處理器

if(Listener1.get1stMousePos() < Listener1.get2ndMousePos() && Listener2.get1stMousePos() < Listener2.get2ndMousePos() && Listener1.clk && Listener2.clk){ 
    zoomMethod((Listener1.get1stMousePos - Listener1.get2ndMousePos()) +   (Listener1.get1stListener2.get2ndMousePos()); 

    } 

一nd比只是把它添加到窗口中:

window.addMouseListener(Listener1); 
    window.addMouseListener(Listener2); 

希望你找到一個方法。

0

對於雙指縮放:

嘗試通過guigarage.com的GestureMagnificationListener。它提供了一種方法:

public void magnify(GestureMagnificationEvent me){...}