、両方の円は、一致(0,0)
を中心と半径5
と次のとおりである:彼らのバウンディングボックス(とGroup
の結果、バウンディングボックス)が(-5,-5)
と幅と10
の高さで左上隅を持っています。この四角形10x10
の境界ボックスは大きくすることはできません(サイズ変更不可のGroup
です)。画面中央に配置します。 BorderPane
には500
ピクセルの利用可能な幅がありますので、Group
の両側に均等に分割して中央に配置すると、右側に245
、右側には245
の幅があり、使用されていない幅のピクセルは490
です。したがって、両方の円の左端であるGroup
の左端はBorderPane
座標系のx=245
にあります。他の右に100
画素を翻訳されているので、そのバウンディングボックスが(95, -5)
から(105, 5)
に延びているアニメーションの終了時
一円は、幅10x10
と(-5,-5)
のままです。その結果、Group
のバウンディングボックスは、その子ノードの集合境界を占め、(-5, -5)
、幅が110
、高さが10
の左上があります。このボックスのサイズを変更することはできませんので、BorderPane
のレイアウトメカニズムは、このボックスを使用可能な領域の中央に配置します。BorderPane
の幅は500
ピクセルであるため、左右に均等に分割された未使用ピクセル390個があります(Group
の左側に195
、右側に195
)。この時点で、Group
の左端は翻訳されていない円の左端であり、BorderPane
座標系のx=195
にあります。その結果、アニメーションが終了すると、BorderPane
の座標系では、未翻訳の円が50
ピクセル(翻訳距離の半分)だけ左に移動しています。
もっと自然なことはGroup
の代わりにPane
を使用することです。 Pane
はサイズ変更が可能ですので、BorderPane
は利用可能なすべての領域を埋めるように拡張します。したがって、それはBorderPane
の左上にあり、BorderPane
を記入します。 Pane
の境界は(0,0)
から始まり、その幅と高さに及ぶ。したがって、Group
をPane
に変更するだけで、アニメーション中に翻訳されていない円は移動しません。
ただし、円は中心の代わりに枠の左上に開始されます。あなたがそれらを中心に開始したい場合は、円そのものの座標を変更することができますので、彼らは(250, 250)
を中心に開始します。別の方法として
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
BorderPane root = new BorderPane();
root.setStyle("-fx-background-color: Black");
Pane graph = new Pane();
root.setCenter(graph);
// graph.setLayoutX(250);
// graph.setLayoutY(250);
Circle circle = new Circle(250, 250, 5);
circle.setFill(Color.ORANGE);
graph.getChildren().add(circle);
Circle circle2 = new Circle(250, 250, 5);
circle2.setFill(Color.AQUA);
graph.getChildren().add(circle2);
TranslateTransition t = new TranslateTransition(Duration.millis(1000), circle);
t.setFromX(0);
t.setToX(100);
t.setFromY(0);
t.setToY(0);
t.setInterpolator(Interpolator.LINEAR);
t.play();
stage.setTitle("Circle Test");
stage.setScene((new Scene(root, 500, 500)));
stage.show();
}
}
、あなたは代わりに、ルートとしてPane
を使用することができますBorderPane
。プレーンPane
はレイアウトを行わないため、この場合はlayoutX
とlayoutY
の設定が有効になります。したがって、サークルの中心を(0,0)
に戻して、のレイアウト設定を使用して中心に配置できます。
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
Pane root = new Pane();
root.setStyle("-fx-background-color: Black");
Pane graph = new Pane();
root.getChildren().add(graph);
graph.setLayoutX(250);
graph.setLayoutY(250);
Circle circle = new Circle(0, 0, 5);
circle.setFill(Color.ORANGE);
graph.getChildren().add(circle);
Circle circle2 = new Circle(0, 0, 5);
circle2.setFill(Color.AQUA);
graph.getChildren().add(circle2);
TranslateTransition t = new TranslateTransition(Duration.millis(1000), circle);
t.setFromX(0);
t.setToX(100);
t.setFromY(0);
t.setToY(0);
t.setInterpolator(Interpolator.LINEAR);
t.play();
stage.setTitle("Circle Test");
stage.setScene((new Scene(root, 500, 500)));
stage.show();
}
}