简体   繁体   中英

Why doesn't localToScene give correct scene coordinates here?

I am trying to illustrate local and scene XY-axes in this app:

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TestCoordinates extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        GridPane pane1 = new GridPane();
        GridPane pane2 = new GridPane();
        pane1.setVgap(10);
        pane1.setHgap(10);
        // place holders
        pane1.add(new Label("PH"), 1, 1);
        pane1.add(new Label("PH"), 1, 2);
        pane1.add(new Label("PH"), 2, 1);
        pane1.add(pane2, 2, 2);
        Rectangle r = new Rectangle(50, 50);
        r.setFill(Color.RED);
        pane2.add(r, 1, 1);
        Line line = new Line(0, 0, 50, 50);
        line.setStroke(Color.RED);
        pane2.add(line, 2, 2);
        Point2D origin1 = new Point2D(0, 0);
        Point2D origin2 = r.localToScene(0, 0);
        Point2D origin3 = line.localToScene(0, 0);
        Line xAxis1 = new Line(origin1.getX(), origin1.getY(), 53, origin1.getY());
        Line xAxis2 = new Line(origin2.getX(), origin2.getY(), 53, origin2.getY());
        Line xAxis3 = new Line(origin3.getX(), origin3.getY(), 53, origin3.getY());
        Line yAxis1 = new Line(origin1.getX(), origin1.getY(), origin1.getX(), 53);
        Line yAxis2 = new Line(origin2.getX(), origin2.getY(), origin2.getX(), 53);
        Line yAxis3 = new Line(origin3.getX(), origin3.getY(), origin3.getX(), 53);
        root.getChildren().addAll(pane1, xAxis1, xAxis2, xAxis3, yAxis1, yAxis2, yAxis3);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

But it only draws axes at the root:

在此处输入图片说明

Did I do something wrong?

The solution is to set the scene and show the stage first, and set up a button for adding the axes:

package sample;/**
 * Created by IDEA on 30/07/15.
 */

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TestCoordinates extends Application {
    private Pane root;
    private GridPane pane1;
    private GridPane pane2;
    private Rectangle r;
    private Line line;
    private Line xAxis1;
    private Line xAxis2;
    private Line xAxis3;
    private Line yAxis1;
    private Line yAxis2;
    private Line yAxis3;
    private Button btn;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() {
        root = new Pane();
        pane1 = new GridPane();
        pane2 = new GridPane();
        pane1.setVgap(10);
        pane1.setHgap(10);
        // place holders
        pane1.add(new Label("PH"), 1, 1);
        pane1.add(new Label("PH"), 1, 2);
        pane1.add(new Label("PH"), 2, 1);
        pane1.add(pane2, 2, 2);
        r = new Rectangle(50, 50);
        r.setFill(Color.RED);
        pane2.add(r, 1, 1);
        line = new Line(0, 0, 50, 50);
        line.setStroke(Color.RED);
        pane2.add(line, 2, 2);
        btn = new Button("Draw axes");
        pane2.add(btn, 2, 3);
        xAxis1 = new Line(0, 0, 53, 0);
        yAxis1 = new Line(0, 0, 0, 53);
        root.getChildren().addAll(pane1, xAxis1, yAxis1);
    }

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        btn.setOnAction(e -> drawAxes());
        primaryStage.show();
    }

    private void drawAxes() {
        Point2D origin2 = r.localToScene(0, 0);
        Point2D origin3 = line.localToScene(0, 0);
        root.getChildren().addAll(
          drawXAxis(origin2), drawYAxis(origin2),
          drawXAxis(origin3), drawYAxis(origin3)
        );
    }

    public Line drawXAxis(Point2D origin) {
        return new Line(origin.getX(), origin.getY(), origin.getX() + 53, origin.getY());
    }

    public Line drawYAxis(Point2D origin) {
        return new Line(origin.getX(), origin.getY(), origin.getX(), origin.getY() + 53);
    }
}

Result:

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM