Feedback requested: Proof of concept PR for extending WritableImage with its own "graphics context" to draw higher level primitives directly :)
John Hendrikx
john.hendrikx at gmail.com
Mon Nov 10 14:29:01 UTC 2025
I've extended WritableImage with a new method `getDrawingContext`.
This method gives you a similar interface as Canvas's
`getGraphicsContext`, minus some methods that I didn't implement yet
(save/restore, font rendering, path rendering, line dashes).
The method signatures are compatible, and we could have Canvas's
GraphicsContext extend the DrawingContext interface.
Using it is trivial:
WritableImage img = new WritableImage(400, 400);
DrawingContext ctx = img.getDrawingContext();
ctx.setFill(Color.RED);
ctx.fillRect(50, 50, 100, 100);
Internally, this leverages the software renderer Pisces which is part of
JavaFX. This means that FX can still be fully GPU accelerated, while
for drawing into a heap based WritableImage can be done with the CPU. A
big advantage here is that this makes it possible to draw complex images
without having to snapshot them later if you need them to be images or
having to resort to drawing all primitives yourself with just a PixelWriter.
See this PR https://github.com/openjdk/jfx/pull/1969
A screenshot and demo program is included.
Feedback on the implementation is much appreciated.
--John
More information about the openjfx-dev
mailing list