[英]JavaFX rectangle mouseonclick grid return
I am writing a board game which has a 20x20 grid. 我正在写一个20x20的棋盘游戏。
This is in my board class: 这是在我的董事会课程中:
private final Position[][] grid = new Position[GRID_SIZE][GRID_SIZE];
each position has : 每个职位都有:
public class Position {
private final Coordinates pos;
private Player player;
private final static double RECTANGLE_SIZE = 40.0;
private final Rectangle rect = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
}
so basically I have 20x20 Positions and each positions has a Rectangle 所以基本上我有20x20个位置,每个位置都有一个矩形
This is what I do to display the grid 这是我要做的显示网格
for (int cols = 0; cols < GRID_SIZE; ++cols) {
for (int rows = 0; rows < GRID_SIZE; ++rows) {
grid.add(gameEngine.getBoard().getGrid()[cols][rows].getRect(), cols, rows);
}
}
Anyway, the grid is initialized and works properly. 无论如何,网格已初始化并且可以正常工作。 What I want to do is to make the rectangle objects clickable and to be able to return their Coordinates when they are clicked. 我要做的是使矩形对象可单击,并在单击时能够返回其坐标。
This is how I handle the mouse click 这就是我处理鼠标点击的方式
private void setUpRectangle() {
rect.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
rect.setFill(Color.BLACK);
}
});
}
What this code does is to change the color of the rectangle to black, but how could I return the Coordinates. 该代码的作用是将矩形的颜色更改为黑色,但是如何返回坐标。 Basically, I can edit the onclick function to return the coordinates of this position, but how can I acquire them later? 基本上,我可以编辑onclick函数以返回该位置的坐标,但是以后如何获取它们呢?
This is not a JavaFX question as much as it is a design question. 这不是JavaFX问题,而是设计问题。 You have a container ( Position
) of 2 objects ( Coordinates
and Rectangle
) and you want one of them to know about the other. 您有一个包含2个对象( Coordinates
和Rectangle
)的容器( Position
),并且您希望它们中的一个了解另一个。 That is, the rectangle should know the coordinates of its position. 也就是说,矩形应该知道其位置的坐标。
There are a few approaches here, and depending on the bigger picture, one might be better than the others. 这里有几种方法,根据整体情况,一种方法可能比其他方法更好。 James_D mentioned a couple in a comment . James_D在评论中提到了一对夫妇。
rectangle.getPosition().getCoordinates()
or .getPlayer()
. 您将执行诸如rectangle.getPosition().getCoordinates()
或.getPlayer()
。 rectangle.getCoordinates()
. 您可能会做类似rectangle.getCoordinates()
。 setUpRectangle
method. 将坐标传递给您的setUpRectangle
方法。 This is useful if you rectangle doesn't need access to this data from various places, it's a local solution. 如果您的矩形不需要从各个地方访问此数据,这将非常有用,这是一种本地解决方案。 Then in the handle
method you would return the coordinates you passed to setUpRectangle
, though we can't see what class this method is in. 然后,在handle
方法中,您将返回传递给setUpRectangle
的坐标,尽管我们看不到此方法所在的类。 Map<Rectangle, Coordinates>
and then call map.get(rectangle)
. 您可以保留类似Map<Rectangle, Coordinates>
,然后调用map.get(rectangle)
。 You can hide this map in a method Coordinates getCoordinatesForRectangle(Rectangle rectangle)
instead of calling it directly. 您可以使用Coordinates getCoordinatesForRectangle(Rectangle rectangle)
方法隐藏此地图,而不是直接调用它。 You could store this data as userData
(or use properties
in case userData
is preserved for something else in your program): 您可以将这些数据存储为userData
(或在程序中为其他内容保留userData
情况下使用properties
):
private final Rectangle rect;
public Position() {
rect = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
rect.setUserData(this);
}
rect.setOnMouseClicked((MouseEvent event) -> {
Position pos = (Position) ((Node) event.getSource()).getUserData();
...
});
You could also use a listener that knows about the position: 您还可以使用知道位置的侦听器:
class CoordinateAwareListener implements EventHandler<MouseEvent> {
private final int coordinateX;
private final int coordinateY;
public CoordinateAwareListener(int coordinateX, int coordinateY) {
this.coordinateX = coordinateX;
this.coordinateY = coordinateY;
}
@Override
public void handle(MouseEvent event) {
// do something with the coordinates
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.