我正在開發一個視頻註釋程序。在視頻中看到的對象可能被標記爲感興趣的對象,並且如果它們正在交互,則用戶可以在兩個註釋之間畫一條線。在可見級別上,對象註釋基本上是透明的,它們之間的關係是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();
}
});
}
}
不幸的是,我正在通過'translateX'移動'Rectangle',所以這個解決方案(儘管是正確的)不是讓綁定工作的解決方案。當我的翻譯完成後,你知道該怎麼辦? – deHaar
@deHaar這就是人們發佈不包含[MCVE]的問題時如此令人沮喪的原因:不能保證他們描述的問題是由他們發佈的代碼造成的,而不是由他們未發佈的其他代碼造成的這是這裏的情況)。請更新您的問題。 (或者,顯然,改變你的方法,以便通過改變'x'來移動矩形。) –
對於令人沮喪的抱歉抱歉,我更新了帶'Rectangle'移動代碼的問題。我的解決方案有相應的屬性,所以我會將這個方法改爲與翻譯值綁定,因爲它們會被渲染。如果你知道更好的方法,請告訴我。 – deHaar