gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:39:37 26 4
gpt4 key购买 nike

我编写了一个程序,可以创建一个简单的视觉四叉树 - 基本上从 4 个矩形的网格开始,每个矩形都可以分为包含 4 个较小矩形的网格的较小版本。 问题:但有时,最初的 4 个方格之一拒绝分割,而其他方格继续工作。仅当我尝试右键单击几次后,当网格已合并为一个无法进一步合并的基本正方形时,才会出现此问题。

我已附上.setOnMouseClicked()每个方 block 上调用我的 divide() 的处理程序左键单击方法和 combine()右键单击。此外,处理程序方法设置为在尝试调用组合方法之前检查被单击的方 block 是否属于方 block 网格,这就是我很难理解出了什么问题的地方。

到目前为止我的理论:

  • 也许问题与 javafx 如何处理视觉对象有关,但我希望使用 .getChildren().remove(Object o);是从场景中移除某些东西的正确方法。不过,它似乎在大多数情况下都有效。
  • 也许我改变显示内容和不显示内容的方式并不是真正的线程安全,并且使用 eventHandler 会中断对象删除 - 但当我缓慢和快速单击时就会出现问题。

类层次结构由一个抽象组成 Cell类可以是 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();
}

尽管多次左键单击,轮廓方 block 仍拒绝分割 The outlined squares refuses to divide despite many left-clicks右上角的方 block 拒绝分割,其他方 block 会 The top right square refuses to divide, the others would

最佳答案

首先,您应该将 parent 的类型更改为 Grid。这样可以避免不必要的类型检查。 (毕竟每个 parent 都是一个Grid。)

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

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;
}

关于四叉树中的 Javafx 矩形不会消失/创建 - 使用鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38564337/

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