JavaFX: how to clear the canvas
Asked Answered
I

1

18

Let's say I have drawn a rectangle on my canvas and I want to clean it in order to draw some other figure / polygon / arc ....

How can I do it? I have tried it in many ways but none has worked.

I think this may work but I'm not sure:

GraphicsContext gc = myCanvas.getGraphicsContext2D();
gc.setFill(Color.ALICEBLUE);
gc.fillRect(0, 0, 300, 200);

Could you tell me if this will work consistently and whether it is the standard way to achieve this goal?

Iand answered 29/11, 2014 at 15:5 Comment(0)
R
31

The method clearRect seems to be dedicated for this:

gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
Rotter answered 29/11, 2014 at 17:29 Comment(2)
@Rotter How do I clear a rectangle inclined at an angle?Ludendorff
maybe this is better: gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());Henrik

© 2022 - 2024 — McMap. All rights reserved.