0

I'm using JavaFX to create a triangle in the window. I find even I change x and y of triangle, the position of triangle in the window doesn't change at all, always in the center of window.

public class Board extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Board");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 600,519);
        stage.setScene(scene);

        //step three
        Triangle triangle = new Triangle(300.0,254.5,200);
        triangle.setFill(Color.LIGHTGRAY);
        root.getChildren().add(triangle);

        stage.show();
    }

    class Triangle extends Polygon{
        double x;
        double y;
        double side;

        public Triangle(double x, double y, double side) {
            double mySideSqrt = Math.sqrt((side * side) - (side / 2 * side / 2)) / 2;
            double tX = x;
            double tY = y - mySideSqrt;
            double rX = x + side / 2;
            double rY = y + mySideSqrt;
            double lX = x - side / 2;
            double lY = y + mySideSqrt;

            getPoints().addAll(tX, tY, rX, rY, lX, lY);
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • sorry the code is incomplete(lacking of package and imports) because StackOverflow always cannot recognize them as code format. – CarterZhang Apr 03 '21 at 09:54
  • 2
    Try using a different container, for example [Group](https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/Group.html), rather than `StackPane`. – Abra Apr 03 '21 at 10:11
  • As @Abra stated. Try using `Group` or `Pane`. I suggest `Pane`. [This](https://stackoverflow.com/questions/30901185/what-is-the-difference-between-a-pane-and-a-group) will give you a better understanding of which node you probably need to use. – SedJ601 Apr 03 '21 at 17:33

0 Answers0