JavaFX" y"在画布上的位置

时间:2017-12-14 01:35:07

标签: java javafx javafx-8

我创建了一个随机绘制圆形,正方形,矩形的程序。我的问题是我通过event.getX()和event.getY()得到了点击位置;

问题在于:

绿点是我点击画布的地方。

Problem on Y axis

↑→轴必须是这样的。但是在我的程序中,它就像这样↓→

以下是详细信息:

使用(扩展Canvas)获取鼠标位置:

super.setOnMouseClicked(event -> selectFigureContaining(event.getX(),event.getY()));

初始化部分:

@Override
    public void start(Stage primaryStage) {
        this.color = Color.BLACK;
        toggleGroup = new ToggleGroup();
        hBox = new HBox();
        vBox = new VBox();
        label = new Label();

        vBox.setPadding(new Insets(5,15,0,15));
        vBox.setSpacing(10);
        hBox.setPadding(new Insets(5,0,0,0));




        //Radio Buttons
        red = new RadioButton("Red");
        red.setSelected(true);
        green = new RadioButton("Green");
        blue = new RadioButton("Blue");

        red.setToggleGroup(toggleGroup);
        green.setToggleGroup(toggleGroup);
        blue.setToggleGroup(toggleGroup);



        //Buttons
        buttonCircle = new Button("Circle");
        buttonCircle.setMinSize(70,20);
        buttonCircle.setOnMouseClicked(event -> circleButtonClicked());

        buttonSquare = new Button("Square");
        buttonSquare.setMinSize(70,20);
        buttonSquare.setOnMouseClicked(event -> squareButtonClicked());

        buttonRectangle = new Button("Rectangle");
        buttonRectangle.setMinSize(70,20);
        buttonRectangle.setOnMouseClicked(event -> rectangleButtonClicked());

        cleanAll = new Button("Clean All");
        cleanAll.setMinSize(70,20);
        cleanAll.setOnMouseClicked(event -> cleanAllEvent());


        figureCanvas = new FigureCanvas(10,400,500, this);
        vBox.getChildren().addAll(red,green,blue,buttonCircle,buttonSquare,buttonRectangle,cleanAll,label);
        hBox.getChildren().addAll(vBox,figureCanvas);


        Scene scene = new Scene(hBox,500,500);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Draw Figure");
        primaryStage.show();

    }

我是JavaFX的新手。我无法理解为什么y轴在左上方变小。感谢。

1 个答案:

答案 0 :(得分:2)

通常情况下,坐标系的原点位于屏幕的左上方,正y位于下方:
    。 ---> + X
    |
    v + y
您可以像这样转换y位置以获得预期的移动(左下角的原点,+ y上升):

y = canvas.getHeight() - y;