Search code examples
javafxborderjava-canvas

Draw a border around shapes in the canvas with JavaFx


I have drawn a straight line in a canvas and filled with a solid color. I want to border this straight line with a black colored border.


Solution

  • You can use two fillRect with different size and color

    example:

    final Canvas canvas = new Canvas(250,250);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    
    gc.setFill(Color.BLUE);
    gc.fillRect(0,0,100,20);
    gc.setFill(Color.RED);
    gc.fillRect(1,1,98,18);