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) です。この座標をグループのローカル座標内でさらに変換できますLayoutXLayoutY

  • Group 内の Line Layout Bounds を知りたい場合は、 を使用できますline.getLayoutBounds().getMinX()
  • シーン内のライン位置を知りたい場合は、line.localToScene(10, 10)メソッドを使用することもできます。
于 2013-01-31T15:55:53.643 に答える