我制作了一个创建简单视觉四叉树的程序 - 基本上以4个矩形的网格开始,每个矩形可以分成包含4个较小矩形的较小版本的网格。 问题:有时候,原来的4个方格中的一个拒绝分裂,而其他方块继续工作。这个问题只发生在我已经多次尝试右键单击网格已经组合到一个无法进一步组合的基本方块之后。
我已在每个广场上附加.setOnMouseClicked()
处理程序,在左键单击时调用divide()
方法,右键单击combine()
。此外,处理程序方法设置为在尝试调用combine方法之前检查被点击的方块是否属于方格网格,这是我很难理解出错的地方。
到目前为止我的理论:
.getChildren().remove(Object o);
是从场景中删除某些内容的正确方法。它似乎大部分时间都在工作。类层次结构由一个抽象Cell
类组成,可以是Grid
或Square
。请原谅我在这里丢弃了很多代码,但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();
}
答案 0 :(得分:1)
首先,您应该将parent
的类型更改为Grid
。这样可以避免不必要的类型检查。 (毕竟,每个父母都是Grid
。)
但真正的问题是,您从未正确更新Grid.combine
中的父网格。您需要执行此更新,以便remove
方法正确查找所有子方块:
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;
}