0
我正在研究創建一個應用程序的代碼,該應用程序在我點擊的任何點繪製點。我是JavaFX的新手,我理解如何將對象添加到屏幕的一般原則,除了我無法弄清楚如何刪除項目外,其他所有工作都可以使用。我目前正在通過一個LinkedList進行查看,如果具有相同座標的點存在,我使用pane.getChildren()。remove(index),這是假定窗格中唯一的索引是我的點對象,它們與我的鏈接列表索引。也許創建一個團隊可以幫助這個項目?我不太瞭解一個組的用途,當我試圖用一個組對象而不是一個窗格對象替換代碼時,點不再出現。JavaFX分組和刪除節點
這裏是我試圖找出的代碼,我會添加註釋,以幫助
public void start(Stage stage) {
stage.setTitle("Dots!");
// TODO: Your code starts here
currentColor = Color.RED;
dotList = new SinglyLinkedList<Dot>();
Pane pane = new Pane();
pane.setPrefSize(SIZE, SIZE);
Dot dot = new Dot(50 ,50);
dot.setFill(currentColor);
//dotList.add(dot);
//pane.getChildren().add(dot);
Scene scene = new Scene(pane);
stage.setScene(scene); // Place the scene in the stage
pane.setOnKeyPressed(e -> {
switch (e.getCode()) {
case DIGIT1: currentColor = Color.RED; break;
case DIGIT2: currentColor = Color.BLUE; break;
case DIGIT3: currentColor = Color.GREEN; break;
case NUMPAD1: currentColor = Color.RED; break;
case NUMPAD2: currentColor = Color.BLUE; break;
case NUMPAD3: currentColor = Color.GREEN; break;
default:
break;
}
});
pane.requestFocus();
pane.setOnMouseClicked(e -> {
double x = e.getX();
double y = e.getY();
int index = 0;
int size = dotList.size();
if (size !=0 && index < size) {
Dot check = dotList.get(index);
if (check.contains(x, y)) {
dotList.remove(index);
pane.getChildren().remove(index); //This is may be wrong
size = dotList.size();
}
index++;
}
Dot newDot = new Dot(x ,y);
dotList.add(newDot);
pane.getChildren().add(newDot); ///This adds a new dot, this works
});
pane.requestFocus();
// Your code ends here
stage.show(); // makes the window visible to the user
}
我可以製作一組點並將場景設置爲組或其他?我明白,該組可以容納我所有的點對象,但我不明白如何設置場景以查看組中的對象 –