Javafx 在HBox和VBox中布置的形状之间绘制线

Javafx 在HBox和VBox中布置的形状之间绘制线,javafx,Javafx,我在各种容器(滚动窗格、组、HBox和VBox)中有一个圆圈 我想画一条从0,0到圆心的线 我认为下面的代码显示了这个问题。有一些我不知道的功能,应该去 在//中,这里有一些代码来计算圆在哪里工作 我编写了一个示例应用程序来简化我的问题 import java.util.Random; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Group; import j

我在各种容器(滚动窗格、组、HBox和VBox)中有一个圆圈

我想画一条从0,0到圆心的线

我认为下面的代码显示了这个问题。有一些我不知道的功能,应该去 在//中,这里有一些代码来计算圆在哪里工作

我编写了一个示例应用程序来简化我的问题

import java.util.Random;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class TestAppForCoords extends Application {

    //Scene Graph:
    //ScrollPane - scroll
    // - Group - root
    //   - HBox - hbox - with random padding
    //     - VBox
    //     - VBox - vbox - with random padding
    //       - Circle - circle
    //     - VBox
    //   - Group - lines
    //     - Line - line


    @Override
    public void start(final Stage primaryStage) throws Exception {
        Random rand = new Random();
        int randomNum1 = rand.nextInt((100 - 0) + 1) + 0;
        int randomNum2 = rand.nextInt((100 - 0) + 1) + 0;
        System.out.println(randomNum1);
        System.out.println(randomNum2);


        ScrollPane scroll = new ScrollPane();
        scroll.setPrefSize(500, 300);
        Scene scene = new Scene(scroll);

        primaryStage.setScene(scene);


        Group root = new Group();

        HBox hbox = new HBox();

        hbox.setPadding(new Insets(randomNum1));

        VBox vbox = new VBox();
        hbox.getChildren().add(new VBox());

        vbox.setPadding(new Insets(randomNum2));
        hbox.getChildren().add(vbox);
        hbox.getChildren().add(new VBox());

        Circle circle = new Circle();
        circle.setRadius(10);
        vbox.getChildren().add(circle);


        Group lines = new Group();
        root.getChildren().add(hbox);
        root.getChildren().add(lines);

        root.autosize();


        Line line = new Line();
        line.setStartX(0);
        line.setStartY(0);

        //SOME CODE HERE TO WORK OUT WHERE THE CIRCLE IS
        line.setEndX(123);
        line.setEndY(123);

        lines.getChildren().add(line);


        scroll.setContent(root);

        primaryStage.show();
    }



    public static void main(String[] args) {
        System.out.println("Start JavaFXTestApp");
        launch(args);
        System.out.println("End JavaFXTestApp");
    }   
}

我终于让它工作了。我可以添加一个setOnShown处理程序,该处理程序被调用以更正该行

所以我发现localToScene和sceneToLocal只在显示窗口之后才起作用

    primaryStage.setOnShown(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent arg0) {
            Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY());
            p = line.sceneToLocal(p);
            line.setEndX(p.getX());
            line.setEndY(p.getY());
        }
    });
primaryStage.setOnShown(新的EventHandler(){
@凌驾
公共无效句柄(WindowEvent arg0){
point2dp=circle.localToScene(circle.getCenterX(),circle.getCenterY());
p=线路现场局部(p);
line.setEndX(p.getX());
line.setEndY(p.getY());
}
});
因此,最终的工作计划是:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class TestAppForCoords extends Application {

    //Scene Graph:
    //ScrollPane - scroll
    // - Group - root
    //   - VBox - vboxEXTRA - with random padding
    //     - HBox - hbox - with random padding
    //       - VBox
    //       - VBox - vbox - with random padding
    //         - Circle - circle
    //       - VBox
    //     - Group - lines
    //       - Line - line

    Line line = new Line();
    Circle circle = new Circle();

    @Override
    public void start(final Stage primaryStage) throws Exception {
        Random rand = new Random();
        int randomNum1 = rand.nextInt((100 - 0) + 1) + 0;
        int randomNum2 = rand.nextInt((100 - 0) + 1) + 0;
        int randomNum3 = rand.nextInt((100 - 0) + 1) + 0;
        System.out.println(randomNum1);
        System.out.println(randomNum2);
        System.out.println(randomNum3);


        ScrollPane scroll = new ScrollPane();
        scroll.setPrefSize(500, 300);
        Scene scene = new Scene(scroll);

        primaryStage.setScene(scene);

        Group root = new Group();

        HBox hbox = new HBox();

        hbox.setPadding(new Insets(randomNum1));

        VBox vbox = new VBox();
        hbox.getChildren().add(new VBox());

        vbox.setPadding(new Insets(randomNum2));
        hbox.getChildren().add(vbox);
        hbox.getChildren().add(new VBox());

        circle.setRadius(10);
        vbox.getChildren().add(circle);

        Group lines = new Group();
        root.getChildren().add(hbox);
        root.getChildren().add(lines);

        root.autosize();
        root.requestLayout();



        lines.getChildren().add(line);


        VBox vboxEXTRA = new VBox();
        vboxEXTRA.setPadding(new Insets(randomNum3));
        vboxEXTRA.getChildren().add(root);


        scroll.setContent(vboxEXTRA);

        line.setStartX(0);
        line.setStartY(0);

        //This dosen't work prob because we aren't drawing yet
        Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY());
        p = line.sceneToLocal(p);
        line.setEndX(p.getX());
        line.setEndY(p.getY());


        primaryStage.setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent arg0) {
                Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY());
                p = line.sceneToLocal(p);
                line.setEndX(p.getX());
                line.setEndY(p.getY());
            }
        });

        primaryStage.show();
    }



    public static void main(String[] args) {
        System.out.println("Start JavaFXTestApp");
        launch(args);
        System.out.println("End JavaFXTestApp");
    }   
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Random;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.geometry.Point2D;
导入javafx.scene.Group;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ScrollPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.scene.shape.Circle;
导入javafx.scene.shape.Line;
导入javafx.stage.stage;
导入javafx.stage.WindowEvent;
公共类TestAppForCoords扩展了应用程序{
//场景图:
//滚动窗格-滚动
//-组-根
//-VBox-vboxxtra-带有随机填充
//-HBox-HBox-随机填充
//-VBox
//-VBox-VBox-带有随机填充
//-圈-圈
//-VBox
//-组-行
//-行-行
行=新行();
圆圈=新圆圈();
@凌驾
public void start(final Stage primaryStage)引发异常{
Random rand=新的Random();
int randomNum1=rand.nextInt((100-0)+1)+0;
int randomNum2=rand.nextInt((100-0)+1)+0;
int randomNum3=rand.nextInt((100-0)+1)+0;
系统输出打印项数(随机数m1);
系统输出打印项次(随机数m2);
系统输出打印项次(随机数m3);
ScrollPane scroll=新建ScrollPane();
scroll.setPrefSize(500300);
场景=新场景(滚动);
初级阶段。场景(场景);
组根=新组();
HBox HBox=新的HBox();
hbox.setPadding(新插图(randomNum1));
VBox VBox=新的VBox();
hbox.getChildren().add(新的VBox());
vbox.setPadding(新插入(randomNum2));
hbox.getChildren().add(vbox);
hbox.getChildren().add(新的VBox());
圆半径(10);
vbox.getChildren().add(圆);
组行=新组();
root.getChildren().add(hbox);
root.getChildren().add(行);
root.autosize();
root.requestLayout();
line.getChildren().add(line);
VBox vboxxtra=新的VBox();
设置填充(新插入(randomNum3));
vboxEXTRA.getChildren().add(根目录);
scroll.setContent(vboxEXTRA);
行。设置开始x(0);
行设置起始值(0);
//这个不行,因为我们还没画呢
point2dp=circle.localToScene(circle.getCenterX(),circle.getCenterY());
p=线路现场局部(p);
line.setEndX(p.getX());
line.setEndY(p.getY());
setOnShown(新的EventHandler()){
@凌驾
公共无效句柄(WindowEvent arg0){
point2dp=circle.localToScene(circle.getCenterX(),circle.getCenterY());
p=线路现场局部(p);
line.setEndX(p.getX());
line.setEndY(p.getY());
}
});
primaryStage.show();
}
公共静态void main(字符串[]args){
System.out.println(“启动JavaFXTestApp”);
发射(args);
System.out.println(“EndJavaFXTestApp”);
}   
}

请通过编辑您的第一个答案来合并您的答案。快速浏览StackOverflow站点的工作原理。