如何根据JavaFX 8中的特定祖先获取节点边界? [英] How to get node bounds according to its specific ancestor in JavaFX 8?

查看:160
本文介绍了如何根据JavaFX 8中的特定祖先获取节点边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AnchorPane中添加了一个图表,我想得到它的情节(图表情节,我用青色标记它)的边界,这样我就可以添加一些文字了它的顶部,但我应该根据它的祖先知道它的确切界限。如果我手动执行,当调整大小等节点的填充大小时,我可能会失败。

I have added a chart in AnchorPane, and I want to get the bounds of its plot (chart-plot, I have marked it with cyan color), so that I could add some texts on top of it, but I should know its exact bounds according to its ancestor(s). If I do it manually, I may fail when the paddings' size of the nodes will be changed when resizing etc.

import javafx.application.Application;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        NumberAxis numberAxis = new NumberAxis();
        LineChart chart = new LineChart(numberAxis, new NumberAxis());
        chart.getYAxis().setSide(Side.RIGHT);
        Node chartPlotArea = chart.lookup(".chart-plot-background");
        chartPlotArea.setStyle("-fx-background-color: cyan");
        AnchorPane anchorPane = new AnchorPane(chart);
        AnchorPane.setTopAnchor(chart, 0.0);
        AnchorPane.setRightAnchor(chart, 0.0);
        AnchorPane.setBottomAnchor(chart, 0.0);
        AnchorPane.setLeftAnchor(chart, 0.0);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setMaximized(true);
        primaryStage.show();
    }
}

所以问题是如何在我的情况下根据它的祖先获得节点或图表的界限,无论有多少?

So the question is how to get bounds of a node or chart-plot in my case according to its ancestor no matter how many are there?

推荐答案

您已找到Chart-Plot背景节点,并根据其祖先获取坐标,您只需调用

You already found Chart-Plot background node, and to get it coordinates according to its ancestor you need to simply call

chartPlotArea.getBoundsInParent();

如果它们之间有多个祖先,你可以获得char-plot bounds
在AnchorPane这样的坐标系中

and if there is more than one ancestor between them you can get char-plot bounds in AnchorPane coordinates system like this

Bounds bounds =
            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));

这里的一个小技巧是,在你展示舞台并让javaFX布局节点之前它们将是0所以你需要在 .show()方法后更新它,所以结果可能如下所示:

A little trick here, is that they will be 0s until you show stage and let javaFX layout nodes, so you need to update it afte .show() method, so result may look like this:

 NumberAxis numberAxis = new NumberAxis();
    LineChart chart = new LineChart(numberAxis, new NumberAxis());
    chart.getYAxis().setSide(Side.RIGHT);
    Node chartPlotArea = chart.lookup(".chart-plot-background");
    chartPlotArea.setStyle("-fx-background-color: cyan");

    Text text = new Text();
    text.setText("Text");


    AnchorPane anchorPane = new AnchorPane();
    AnchorPane.setTopAnchor(chart, 0.0);
    AnchorPane.setRightAnchor(chart, 0.0);
    AnchorPane.setBottomAnchor(chart, 0.0);
    AnchorPane.setLeftAnchor(chart, 0.0);


    anchorPane.getChildren().addAll(chart, text);

    Scene scene = new Scene(anchorPane);
    primaryStage.setScene(scene);
    primaryStage.setMaximized(true);


    primaryStage.show();

        Bounds bounds =
            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));

    double textRelativeX = (bounds.getMinX() + bounds.getMaxX()) / 2 - text.getLayoutBounds().getWidth() / 2;
    double textRelativeY = bounds.getMinY() - text.getLayoutBounds().getHeight() / 2;

    AnchorPane.setLeftAnchor(text, textRelativeX);
    AnchorPane.setTopAnchor(text, textRelativeY);

请记住,如果您想在调整大小时更改坐标,可以将其绑定到chart或chartPlotArea bound /宽度变化,类似这样

And remember, that if you want coordinates change when resizing you can bind this to chart or chartPlotArea bound/width changes, something like this

  chart.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
        double textRelativeXz = (newValue.getMinX() + newValue.getMaxX()) / 2 - text.getLayoutBounds().getWidth() / 2;
        double textRelativeYz = newValue.getMinY() - text.getLayoutBounds().getHeight() / 3;

        AnchorPane.setLeftAnchor(text, textRelativeXz);
        AnchorPane.setTopAnchor(text, textRelativeYz);
    });

编辑:如果您有多个祖先,您可以这样做以接收char-plot界限anchorPane坐标系

if you have more than one ancestor you can do like this to receive char-plot bounds in anchorPane coordinate system

     Bounds bounds =
            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));   

即使它们之间有多个祖先,这也会有效

this will work even if there more than one ancestor between them

这篇关于如何根据JavaFX 8中的特定祖先获取节点边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆