Canvas : 2 pixel thick line width

Damien Dudouit ddudouit at
Wed Apr 1 10:14:15 UTC 2015


I have found the following answer :

*Imagine each pixel as a (small) rectangle (instead of a point). The
integer coordinates are the boundaries between pixels; so a (horizontal or
vertical) line with integer coordinates falls "between pixels". This is
rendered via antialising, approximating half of the line on one pixel and
half on the other. Moving the line 0.5 pixels left or right moves it to the
center of the pixel, getting around the issue.*

I have done quite a bit of custom controls drawing (canvas) with swing and
swt and it's quite a shift of approach that plain coordinates to not match
pixels on display.

In swing/swt, if I want to draw a rectangle from pixel (0,0) to pixel
(2,2), ie. a square of 3x3 pixels I do :
gc.drawRectangle(0, 0, 3, 3); // x, y, width, height

In JavaFX I must do :
gc.strokeRect(0.5, 0.5, 2, 2)

Have a good day,


2015-04-01 11:42 GMT+02:00 Damien Dudouit <ddudouit at>:

> Hello,
> I'm using a Canvas to display some content (mostly text with lines also
> for underline).
> I've just noticed something that is a big problem for me : with
> line-width=1 and no scaling, actual line-width on display takes 2 pixels.
>     Canvas canvas = new Canvas(300, 300);
>     GraphicsContext gc = canvas.getGraphicsContext2D();
>     gc.setStroke(Color.BLACK);
>     gc.setLineWidth(1);
>     gc.strokeLine(10, 10, 110, 10);
> I'm running Win7 64bits and I have made the test with Oracle jdk8_40 and
> jdk7_71 and the result is the same.
> That 2 pixel thick line is not perfectly black but dark gray.
> If I do 'gc.setLineWidth(2)', then I get a 2 pixel thick line perfectly
> black.
> If I do 'gc.setLineWidth(0.5)', then I get a 2 pixel thick line with a
> lighter gray.
> I want to display underline text in the canvas and a 2 pixel thick
> underline looks bad.
> Any help would be greatly appreciated.
> Damien

More information about the openjfx-dev mailing list