Javafx形状错位

问题描述 投票:0回答:1

我在javafx中有形状错位的问题。我正在使用javafx.scene.shape.Shape对形状进行一些计算。当我像下面的第一个代码一样将所有形状放在一起时,一切都很好。但是在我的扩展程序中,其想法是向AnchorPane动态添加形状(如果可以帮助我使用其他类型的Pane)。我在下面粘贴了第二个代码,该代码显示了形状错位。

第一个代码(好结果):

public class Controller {
public AnchorPane displayPane;

public Circle circleA = new Circle();
public Circle circleB = new Circle();
public Shape resultShape;

public void initialize() {
    setupShapes();
    displayPane.getChildren().add(circleA);
    displayPane.getChildren().add(circleB);
}

public void setupShapes() {
    double d = 100;
    double x = Math.sqrt(3 * Math.pow(d, 2) / 4);

    circleA.setCenterX(300.0);
    circleA.setCenterY(300.0 - d);
    circleA.setRadius(150);
    circleA.setFill(Color.TRANSPARENT);
    circleA.setStroke(Color.BLACK);
    circleA.setStrokeWidth(2);

    circleB.setCenterX(300.0 + x);
    circleB.setCenterY(300.0 + d / 2);
    circleB.setRadius(150);
    circleB.setFill(Color.TRANSPARENT);
    circleB.setStroke(Color.BLACK);
    circleB.setStrokeWidth(2);

    resultShape = Shape.intersect(circleA, circleB);
    resultShape.setFill(Color.DEEPSKYBLUE);
    displayPane.getChildren().add(resultShape);
}
}

第二代码(错误结果):

public class Controller {
    public AnchorPane displayPane;

    public Circle circleA = new Circle();
    public Circle circleB = new Circle();
    public Shape resultShape;

    public void initialize() {
        setupShapes();
        displayPane.getChildren().add(circleA);
        displayPane.getChildren().add(circleB);

        resultShape = Shape.intersect(circleA, circleB);
        resultShape.setFill(Color.DEEPSKYBLUE);
        displayPane.getChildren().add(resultShape);
    }

    public void setupShapes() {
        double d = 100;
        double x = Math.sqrt(3 * Math.pow(d, 2) / 4);

        circleA.setCenterX(300.0);
        circleA.setCenterY(300.0 - d);
        circleA.setRadius(150);
        circleA.setFill(Color.TRANSPARENT);
        circleA.setStroke(Color.BLACK);
        circleA.setStrokeWidth(2);

        circleB.setCenterX(300.0 + x);
        circleB.setCenterY(300.0 + d / 2);
        circleB.setRadius(150);
        circleB.setFill(Color.TRANSPARENT);
        circleB.setStroke(Color.BLACK);
        circleB.setStrokeWidth(2);
    }
}

此代码仅显示了我较大代码的简化版本,但说明了我的问题复杂方式,我认为我的做法可能不是最好的。我想得到您的想法,以使其正常工作。感谢您今后的评论和回答。

left -first code, right- second code

[编辑]我的简单fxml:

<AnchorPane fx:controller="sample.Controller"
        xmlns:fx="http://javafx.com/fxml" prefHeight="720" prefWidth="1280">
<children>
    <AnchorPane fx:id="displayPane" layoutX="60.0" layoutY="60.0" prefHeight="600.0" prefWidth="600.0" />
</children>

当我将anchorPane更改为Pane时,没有任何变化。

java javafx shapes
1个回答
0
投票

如果要查看形状及其位置的详细信息,可以使用System.out.println(resultShape)在控制台中将其打印出来。

对两个版本都执行完此操作后,您会注意到错误的一个在X轴上错位了60.0,而在Y轴上错位了。仅当在圆之后添加resultShape时,它才会发生,这是由AnchorPane中的layoutX和layoutY的值引起的。

圆圈的中心设置为特定值。如果在将它们添加到窗格之前对其进行相交,则将使用这些值来计算resultShape的位置。之后,displayPane.getChildren()。add()将仅对所有三个对象进行一次移位-这是预期的结果。

当您在将圆添加到AnchorPane后尝试获取resultShape时,将使用displayPane.getChildren()。add()已易位的值来计算其位置,这将是一个很好的位置。不幸的是,将resultShape添加到该AnchorPane会再次移动它-它会“两次”移位。

有一些方法可以解决此问题:

1)将resultShape添加到主AnchorPane(displayPane的父级)-您将避免第二次移位。

2)在将圆添加到displayPane之前将它们相交(就像在代码的第一个版本中一样-所有对象将仅移位一次。

3)除去不必要的layoutX和layoutY。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.