2017-08-16 24 views
0

我正在開發一個視頻註釋程序。在視頻中看到的對象可能被標記爲感興趣的對象,並且如果它們正在交互,則用戶可以在兩個註釋之間畫一條線。在可見級別上,對象註釋基本上是透明的,它們之間的關係是Line s。此時,計算矩形的中心很容易,但我無法將Line的開始和結束綁定到相應的Rectangle的中心。我曾嘗試以下方法:如何在Rectangle中創建一個centerXProperty來綁定?

創建一個計算的中心x和y的矩形類內的兩個DoubleBinding S:

private DoubleBinding centerXBinding = new DoubleBinding() { 
    @Override 
    protected double computeValue() { 
     return getX() + getWidth()/2; 
    } 
}; 

,然後將其綁定到新創建的Line

`currentRelation.startXProperty().bind(startShape.centerXBinding());` 

在控制器...

結果是好的,起初,行開始和結束點正是我想避開t下襬,但是當Rectangle被拖到另一個位置時,線條末端不會移動到任何地方! 有沒有人看到這個問題?

UPDATE:

一個Rectangle的運動是通過計算偏移量和更新翻譯值像translateX完成:

public class MyRectangle extends Rectangle { 

    private double orgSceneX; 
    private double orgSceneY; 
    private double orgTranslateX; 
    private double orgTranslateY; 

    private void initEventHandling() { 
     this.setOnMousePressed(mousePress -> { 
      if (mousePress.getButton() == MouseButton.PRIMARY) { 
       orgSceneX = mousePress.getSceneX(); 
       orgSceneY = mousePress.getSceneY(); 
       orgTranslateX = ((MyRectangle) mousePress.getSource()).getTranslateX(); 
       orgTranslateY = ((MyRectangle) mousePress.getSource()).getTranslateY(); 
       mousePress.consume(); 
      } else if (mousePress.getButton() == MouseButton.SECONDARY) { 
        System.out.println(LOG_TAG + ": right mouse button PRESS on " + this.getId() + ", event not consumed"); 
      } 
     }); 

     this.setOnMouseDragged(mouseDrag -> { 
      if (mouseDrag.getButton() == MouseButton.PRIMARY) { 

       double offsetX = mouseDrag.getSceneX() - orgSceneX; 
       double offsetY = mouseDrag.getSceneY() - orgSceneY; 
       double updateTranslateX = orgTranslateX + offsetX; 
       double updateTranslateY = orgTranslateY + offsetY; 
       this.setTranslateX(updateTranslateX); 
       this.setTranslateY(updateTranslateY); 
       mouseDrag.consume(); 
      } 
     }); 
    } 
} 

回答

2

你的綁定需要無效時,無論是xPropertywidthProperty是無效(這樣綁定它的任何東西都知道要重新計算)。您可以通過調用自定義綁定的構造函數中bind method做到這一點:

private DoubleBinding centerXBinding = new DoubleBinding() { 

    { 
     bind(xProperty(), widthProperty()); 
    } 

    @Override 
    protected double computeValue() { 
     return getX() + getWidth()/2; 
    } 
}; 

注意,你也可以做

private DoubleBinding centerXBinding = xProperty().add(widthProperty().divide(2)); 

兩者之間的選擇真的只是一個你更喜歡哪一種風格的問題。

綁定到xwidth屬性顯然假定您正在通過更改其中一個或兩個屬性來移動矩形。如果您正在通過一些其他方式的矩形(例如,通過改變其translateXtranslateY屬性中的一個,或通過改變其轉換的列表),那麼你就需要觀察boundsInParentProperty代替:

private DoubleBinding centerXBinding = new DoubleBinding() { 

    { 
     bind(boundsInParentProperty()); 
    } 

    @Override 
    protected double computeValue() { 
     Bounds bounds = getBoundsInParent(); 
     return (bounds.getMinX() + bounds.getMaxX())/2 ; 
    } 
} 

這種綁定將在父座標系中給出矩形中心的x座標(通常是你想要的座標系)。

+0

不幸的是,我正在通過'translateX'移動'Rectangle',所以這個解決方案(儘管是正確的)不是讓綁定工作的解決方案。當我的翻譯完成後,你知道該怎麼辦? – deHaar

+1

@deHaar這就是人們發佈不包含[MCVE]的問題時如此令人沮喪的原因:不能保證他們描述的問題是由他們發佈的代碼造成的,而不是由他們未發佈的其他代碼造成的這是這裏的情況)。請更新您的問題。 (或者,顯然,改變你的方法,以便通過改變'x'來移動矩形。) –

+0

對於令人沮喪的抱歉抱歉,我更新了帶'Rectangle'移動代碼的問題。我的解決方案有相應的屬性,所以我會將這個方法改爲與翻譯值綁定,因爲它們會被渲染。如果你知道更好的方法,請告訴我。 – deHaar

相關問題