gpt4 book ai didi

How do I get X and Y Coordinates of different rows and columns in a gridpane?(如何获取网格窗格中不同行和列的X和Y坐标?)

转载 作者:bug小助手 更新时间:2023-10-22 14:29:53 38 4
gpt4 key购买 nike



I'm currently making a game board and want to be able to move pieces from one tile to another. I'm trying to do this by getting the centerX and centerY of each tile as the user click on it, which would then allow me to move my pieces based off of those values.

我目前正在制作一个游戏板,希望能够将碎片从一个瓷砖移动到另一个瓷砖。我试图通过在用户点击每个磁贴时获得其中心X和中心Y来实现这一点,这将允许我根据这些值移动我的作品。


Here is what I tried:

以下是我尝试过的:


gameBoard.setOnMouseClicked(evt -> {
Node target = evt.getPickResult().getIntersectedNode();

if (target != gameBoard) {
Bounds bounds = target.getBoundsInParent();
System.out.println("bounds = " + bounds);
System.out.println("centerX = " + (bounds.getMinX() + bounds.getWidth()/2));
System.out.println("centerY = " + (bounds.getMinY() + bounds.getHeight()/2));
}
});

However centerX = and centerY = only print out 25.0, no matter the grid row/column I click on. Any advice?

然而,无论我点击网格行/列,centerX=和centerY=都只打印出25.0。有什么建议吗?


更多回答

Perhaps off-topic, but it’s usually cleaner to code this with an event handler for each of the pieces, instead of a single event handler for the board. That way your event handler already has access to information like which piece was clicked, what its location is, etc.

也许偏离了主题,但通常更简单的做法是为每一块代码都使用一个事件处理程序,而不是为板使用一个单独的事件处理程序。通过这种方式,您的事件处理程序已经可以访问诸如单击了哪一个片段、它的位置等信息。

Two approaches are examined here.

这里考察了两种方法。

@trashgod I think this helped I'm able to get the rows and columns of the circles in the Grid.

@垃圾神,我想这有助于我在网格中获得圆圈的行和列。

@RyanCruz: Excellent; you should update your question to reflect the the revised approach; going forward, you can answer your own question; if not, I can close as a duplicate..

@RyanCruz:优秀;您应该更新您的问题以反映修订后的方法;展望未来,你可以回答自己的问题;如果没有,我可以作为副本关闭。。

优秀答案推荐

The problem is probably caused by target.getBoundsInParent() this gives you the clicked element bounds in relation to its parent.

问题可能是由target.getBoundsInParent()引起的,它为您提供了单击的元素相对于其父元素的边界。


The reason why you got the

你为什么得到



centerX = 25.0



and



centerY = 25.0



for all the cells is because, if your grid cells are uniform, clicking any cells will give you same dimension and its relative position with its parent.

对于所有单元格,是因为,如果网格单元格是统一的,单击任何单元格都会得到相同的尺寸及其与父单元格的相对位置。


try to convert the local coordinates to your scene coordinates.

尝试将局部坐标转换为场景坐标。


gameBoard.setOnMouseClicked(evt -> {
Node target = evt.getPickResult().getIntersectedNode();

if (target != gameBoard) {
Bounds bounds = target.getBoundsInParent();
Point2D pointInScene = target.localToScene(bounds.getMinX() + bounds.getWidth() / 2, bounds.getMinY() + bounds.getHeight() / 2);

System.out.println("bounds = " + bounds);
System.out.println("centerX in Scene = " + pointInScene.getX());
System.out.println("centerY in Scene = " + pointInScene.getY());
}
});

更多回答

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com