Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX矩形鼠标单击网格返回_Java_Oop_Javafx_Return - Fatal编程技术网

JavaFX矩形鼠标单击网格返回

JavaFX矩形鼠标单击网格返回,java,oop,javafx,return,Java,Oop,Javafx,Return,我正在写一个20x20网格的棋盘游戏 这是我的董事会课程: private final Position[][] grid = new Position[GRID_SIZE][GRID_SIZE]; 每个职位都有: public class Position { private final Coordinates pos; private Player player; private final static double RECTANGLE_SIZE = 40.0

我正在写一个20x20网格的棋盘游戏

这是我的董事会课程:

private final Position[][] grid = new Position[GRID_SIZE][GRID_SIZE];
每个职位都有:

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);
}
基本上我有20x20个位置,每个位置有一个矩形

这就是我显示网格的方法

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);
    }
}
for(int cols=0;cols
无论如何,网格已初始化并正常工作。我想做的是使矩形对象可单击,并能够在单击时返回其坐标

这就是我处理鼠标点击的方式

private void setUpRectangle() {
    rect.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            rect.setFill(Color.BLACK);
        }
    });
}
private void setUpRectangle(){
setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
矩形设置填充(颜色为黑色);
}
});
}
这段代码的作用是将矩形的颜色更改为黑色,但是如何返回坐标呢。
基本上,我可以编辑onclick函数来返回这个位置的坐标,但是以后如何获取它们呢

这不是一个JavaFX问题,而是一个设计问题。您有一个包含两个对象(
坐标
矩形
)的容器(
位置
),您希望其中一个对象了解另一个对象。也就是说,矩形应该知道其位置的坐标

这里有几种方法,根据大局,一种可能比另一种更好。詹姆斯在一篇文章中提到了一对夫妇

  • 在矩形对象中保留位置对象的引用。如果矩形需要从不同的位置访问容器中的各种数据,这将非常有用。您可以执行类似于
    rectangle.getPosition().getCoordinates()
    .getPlayer()
    的操作
  • 在矩形对象中保留坐标对象的引用。如果您只需要该对象,这是一种更为具体的方法1。您可以执行类似于
    rectangle.getCoordinates()
    的操作
  • 将坐标传递给
    setUpRectangle
    方法。如果您不需要从不同的地方访问这些数据,这将非常有用,因为这是一个本地解决方案。然后在
    handle
    方法中,您将返回传递给
    setUpRectangle
    的坐标,尽管我们看不到该方法在哪个类中
  • 使用外部帮助。您可以保留类似于
    Map
    的内容,然后调用
    Map.get(矩形)
    。您可以在方法
    coordinations getCoordinationsforRectangle(矩形矩形)
    中隐藏此映射,而不是直接调用它
    您可以将此数据存储为
    userData
    (或者在程序中为其他内容保留
    userData
    时使用
    properties
    ):


    您还可以使用了解该位置的侦听器:

    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
        }
    
    }
    
    类CoordinateWareListener实现EventHandler{
    私人最终国际协调人;
    私人最终国际协调;
    公共协调人Warelistener(内部协调人,内部协调人){
    this.coordinateX=coordinateX;
    this.coordinateY=coordinateY;
    }
    @凌驾
    公共无效句柄(MouseeEvent事件){
    //用坐标做点什么
    }
    }
    
    坐标是什么意思?如果需要,可以尝试使用
    rect.getX()
    rect.getY()
    。@Shashwat每个位置都有坐标(x,y)和rectange对象。我想知道单击哪个矩形来获取其坐标。即使我返回矩形的坐标对象,我也不知道如何处理它们(等待它们),坐标在哪里?如果他们在
    位置
    类中,您可以为他们创建一个getter。另外,要从何处访问坐标,
    Main
    类或
    位置
    类?板类具有位置[20][20]。每个位置都有坐标和矩形。我想让程序等待用户点击矩形,然后将矩形的坐标返回到黑板上。你需要把这个问题弄清楚。我们无法确定哪些方法是在哪些类中定义的,哪些属性属于哪些对象。这里的底线是,您要么需要为
    setUpRectangle
    提供一个
    Position
    类型的参数,并将相应矩形的位置传递给它,要么需要为每个
    rectangle
    提供一个
    Position
    属性。(当你回答问题时,请修正问题。)
    rect.setOnMouseClicked((MouseEvent event) -> {
        Position pos = (Position) ((Node) event.getSource()).getUserData();
        ...
    });
    
    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
        }
    
    }