1

我很难找出形状的正确 layoutx / layouty 值。请看一下这个例子:

package test;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Line;

public class TestLinePosition
    extends Application
{
  private Line line;

  private Scene getScene()
  {
    Group group = new Group();

    line = new Line(10, 10, 60, 10);

    group.getChildren().add(line);

    Scene scene = new Scene(group, 640, 480);

    return scene;
  }

  @Override
  public void start(Stage stage) throws Exception
  {
    stage.setScene(getScene());
    stage.show();
    System.out.println("x: " + line.getLayoutX() + ", y: " + line.getLayoutY());
  }

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

如果我运行这个程序,该行似乎从 10、10 开始按预期定位。但是 layoutx 和 layouty 值为 0、0。

谁能给我解释这种行为和/或告诉我如何找出实际位置?

谢谢你,罗杰

4

2 回答 2

0

不要将绘画的起始位置与布局位置混为一谈。将节点添加到组时,您可以通过调用设置它在 x 轴上的布局位置setLayoutX(xValue)。所以调用getLayoutX()返回期望值。

你的例子(重做):

package test;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class TestLinePosition extends Application {

    private Line line;        

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


    private Scene getScene() {
        Group group = new Group();

        //layout position is x:0 and y:0
        //painting starts at x:10 and y:10
        line = new Line(10, 10, 60, 10); 
        //x position for layout
        line.setLayoutX(100);
        //y position for layout
        line.setLayoutY(100);

        group.getChildren().add(line);

        Scene scene = new Scene(group, 640, 480);

        return scene;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(getScene());
        stage.show();
        System.out.println("x: " + line.getLayoutX() + ", y: " + line.getLayoutY());
        System.out.println("start x: " + line.getStartX() + ", start y: " + line.getStartY());
    }
}
于 2011-11-07T21:53:12.210 回答
0

您的线在其本地坐标中有坐标 (10, 10) - (60, 10)。LayoutX并且LayoutY可以在 Group 的本地坐标中进一步转换该坐标。

  • 如果您想知道 Group 内的 Line Layout Bounds 可以使用line.getLayoutBounds().getMinX().
  • 如果你想知道场景内的线位置,你也可以使用line.localToScene(10, 10)方法。
于 2013-01-31T15:55:53.643 回答