I am new to javaFX and want to try a simple application.
I have an application with a Canvas and I draw a rectangle, but I can't erase it's border properly.
Here is how I handle it :
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext g = canvas.getGraphicsContext2D();
g.setFill(colorBackground);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setStroke(Color.BLACK);
g.strokeRect(200, 200, 10, 10);
g.setStroke(colorBackground);
g.strokeRect(200, 200, 10, 10);
But I still have a greyish rectangle instead of just nothing. I don't understand why I have this.
Maybe I use Canvas wrong or something..
Is colorBackground
grey? Then the issue is that you're not erasing anything. To clear the canvas, use GraphicsContext#clearRect
. Then you can erase your rectangle (or the whole canvas) like so:
g.clearRect(200, 200, 10, 10);
Note that this will clear the desired rectangle with a transparent color, not to your specified colorBackground
. To emulate a colored background, you can place an opaque Node
(such as another Canvas
) behind your Canvas
, so that the transparency makes that colored "background" node visible.