Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: how to clear the canvas

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?

like image 906
bog Avatar asked Nov 29 '14 15:11

bog


People also ask

What is JavaFX canvas?

Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext . A Canvas node is constructed with a width and height that specifies the size of the image into which the canvas drawing commands are rendered. All drawing operations are clipped to the bounds of that image.


1 Answers

The method clearRect seems to be dedicated for this:

gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
like image 65
Roland Avatar answered Oct 11 '22 05:10

Roland