四叉树中的 Javafx 矩形不会 dissappear/be 创建 - 使用鼠标事件

Javafx rectangles in quadtree won't dissappear/be created - using mouseevents

我制作了一个创建简单视觉四叉树的程序 - 基本上从 4 个矩形的网格开始,每个矩形都可以分成包含 4 个较小矩形的网格的较小版本。 问题:虽然有时,最初的 4 个方块中有一个拒绝划分,而其他方块继续工作。只有当网格已经组合到一个无法进一步组合的基本方块时,我尝试右键单击几次后才会出现此问题。

我在每个方块上附加了一个 .setOnMouseClicked() 处理程序,它在左键单击时调用我的 divide() 方法,在右键单击时调用 combine() 方法。此外,处理程序方法设置为在尝试调用 combine 方法之前检查被单击的正方形是否属于正方形网格,这就是我难以理解哪里出了问题的地方。

到目前为止我的理论:

class 层次结构由一个抽象 Cell class 组成,可以是 GridSquare。请原谅我在这里删除了很多代码,但是 javafx 调试有时让我很困惑,所以老实说我不知道​​是哪部分代码负责,所以我粘贴了整个代码。

Cell.java:

public abstract class Cell {
    int x, y, w, h, id; //id is a reference the index in a grid this cell belongs to - it's used when a square replaces itself with a grid
    Pane pane;
    Cell parent;

    public Cell(Pane pane, Cell parent, int x, int y, int w, int h, int id) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.id = id;
        this.pane = pane;
        this.parent = parent;
    }

    public void divide(){}
    public abstract void remove();
}

Grid.java

public class Grid extends Cell {
    Cell[] grid = new Cell[4];

    public Grid(Pane pane, Cell parent, int x, int y, int w, int h, int id) {
        //The constructor works as it should, just some unnecessary maths to avoid confusion over rounding
        super(pane, parent, x, y, w, h, id);
        int ver = x + w/2;
        int hor = y + h/2;
        grid[0] = new Square(pane, this, x, y, w/2, h/2, 0);
        grid[1] = new Square(pane, this, ver+1, y, x+w-1-ver, h/2, 1);
        grid[2] = new Square(pane, this, x, hor+1, w/2, y+h-1-hor, 2);
        grid[3] = new Square(pane, this, ver+1, hor+1, x+w-1-ver, y+h-1-hor, 3);
    }

    public void combine(){
        remove();
        parent = new Square(pane, parent, x, y, w, h, id);
    }

    public void remove(){
        for(Cell c : grid){
            c.remove();
        }
    }
}

Square.java:

public class Square extends Cell {
    Rectangle rect;

    public Square(Pane pane, Cell parent, int x, int y, int w, int h, int id) {
        super(pane, parent, x, y, w, h, id);
        rect = new Rectangle(x, y, w, h);
        rect.setFill(Color.RED);
        pane.getChildren().add(rect);
        rect.setOnMouseClicked(e ->{
            if(e.getButton().equals(MouseButton.PRIMARY))
                divide();
            else if(e.getButton().equals(MouseButton.SECONDARY) && parent != null && parent.getClass().equals(Grid.class))
                ((Grid)parent).combine();
        });
    }

    @Override
    public void divide(){
        if(w > 3 && h > 3){
            remove();
            if(parent != null && parent.getClass().equals(Grid.class))
                ((Grid)parent).grid[id] = new Grid(pane, parent, x, y, w, h, id);
            else
                parent = new Grid(pane, parent, x, y, w, h, id);
        }
    }

    public void remove(){
        pane.getChildren().remove(rect);
    }
}

在主文件中,parent 被初始化为 null 为基础 Cell 表示它不能从鼠标处理程序中分离出来:

@Override
public void start(Stage primaryStage) throws Exception {
    Pane pane = new Pane();
    pane.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
    cell = new Square(pane, null, 50, 50, 700, 700, 0);
    cell.divide(); //cell is a Cell type instance variable
    primaryStage.setTitle("SquareSception");
    primaryStage.setScene(new Scene(pane, 800, 800));
    primaryStage.show();
}

尽管点击了很多次左键,轮廓方块仍拒绝分割 右上角的方块拒绝划分,其他的会

首先你应该把parent的类型改成Grid。这样可以避免不必要的类型检查。 (毕竟每个 parent 都是 Grid。)

然而,真正的问题是,您从未正确更新 Grid.combine 中的 parent 网格。您需要执行此更新,以便 remove 方法正确找到所有 child 个方块:

public void combine(){
    remove();
    Square newParent = new Square(pane, parent, x, y, w, h, id);
    if (parent != null) {
        parent.setChild(newParent);
    }
}

public void setChild(Cell newChild) {
    grid[newChild.id] = newChild;
}