2015-11-02 46 views
0

我一直在試圖複製斯卡拉的一些Java Swing工作。在下面的代碼this延伸斯卡拉鞦韆ScrollPaneScala Swing:如何使用鼠標平移圖像?

myPanel.reactions +={ 
    case MouseDragged(_,x,_) => { 
     //val viewport = this.viewportView.get.asInstanceOf[JViewport] 
     val viewport = this.contents.head.self.asInstanceOf[JComponent].getParent.asInstanceOf[JViewport] 
     println(viewport.getViewPosition) 
     val vp = viewport.getViewPosition 
     var cp = x.getLocation 
     vp.translate(point.x-cp.x,point.y-cp.y) 
     //newLoc = new Point(vp.getX) 
     val jComp = contents.head.asInstanceOf[JComponent] 
     jComp.scrollRectToVisible(new Rectangle(vp,viewport.getSize: Dimension)) 
    } 
    case MousePressed(_,x,_,_,_) => point = x.getLocation 
    case MouseClicked(_,_,_,_,_) => println("click") 
} 

我看不出如何獲得訪問Viewport對象中的相關問題Java代碼。 Scala Swing應該如何實現?

回答

0
myPanel.reactions +={ 
    case MouseDragged(myPanel,x,_) => { 
     val cp = x.getLocation 
     this.horizontalScrollBar.value += point.x-cp.x 
     this.verticalScrollBar.value += point.y-cp.y 
     point = cp 
    } 
    case MousePressed(_,x,_,_,_) => point = x.getLocation 
    }