-2

Im trying to clear simple canvas in JavaFX.

Start function

Canvas canvas = new Canvas();
        Group root = new Group(canvas);
        Scene scene = new Scene(root, 1400, 1000);

If user want to load game, then loadSave boolean variable sets to 'true'

if(loadSave){
            //Clear scene and load new with circles from file
        }

Else, it loads new game

else if(!loadSave){
            drawSquares(scene, root);
        }

I'll really appreciate your help.

Tacoo
  • 9
  • 3

1 Answers1

0

I have special group for my Scene - root.

I can delete only this group using

root.getChildren().clear();

This feature has many uses, I can now eg add items that I want to remove into this group.

To remove only e.g Circles from group(where my Canvas is) I used

void clearScene(Group root) {
        for (Circle circle : circles) {
            root.getChildren().remove(circle);
        }
        circles.clear();
    }
Tacoo
  • 9
  • 3
  • The question is about clearing a canvas. That doesn't clear the canvas, it removes it (and everything else) from the root. – James_D Jun 06 '22 at 16:13
  • 1
    The title is "Clear canvas in JavaFX" and this answer to your own question has nothing to do with that! – mipa Jun 06 '22 at 16:29
  • Again, edited answer. From code you see only my canvas is in group named 'root', and i clearing this group. I'm new here, please tell me if edited answer is fine now. – Tacoo Jun 06 '22 at 16:36
  • A `Group` is not a `Canvas` – James_D Jun 06 '22 at 16:59
  • 1
    The problem is the disconnect between your question and your answer. If you want to **clear a `Canvas`**, then check out [JavaFX: how to clear the canvas](https://stackoverflow.com/questions/27203671/javafx-how-to-clear-the-canvas). You can see how that Q&A's accepted answer is different from yours, and thus how you answer doesn't fit your question. Keep in mind that "canvas" has a specific meaning in JavaFX; it refers to the `javafx.scene.canvas.Canvas` class. What you're doing in your answer is modifying the "scene graph" (by adding and removing nodes to/from a layout, e.g., `Group`). – Slaw Jun 06 '22 at 17:53
  • 1
    And with that said, we appreciate your effort to improve Stack Overflow with a self-answered Q&A. Please continue to ask questions as needed, and also answer questions as you can. We just ask that you make sure to follow the guidelines (see [ask] and [answer]), and also that any answer actually answers the question _as written_. Accuracy is important. The Q&As on Stack Overflow are meant to help not just the questioner, but also future readers. – Slaw Jun 06 '22 at 17:55
  • Im posting this question to provide answer immadiately, in meantime im tried to figure it out and it works for me. – Tacoo Jun 06 '22 at 19:33
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '22 at 21:09