I think were’ not too far different in our ideas, but I think the practical 
odds of getting this implemented are much higher if the existing 
GraphicsContext methods are the base for the interface I propose.  I think (but 
could be wrong) that in fact the GraphicsContext was modeled after the HTML 5 
canvas so in that sense it forms a good base for what you suggest.  One could 
write code that drew to the Canvas, to an HTML 5 canvas, to graphics files etc. 
 It’s just that now, with GraphicsContext being a final class rather than 
non-final or an interface, one can’t do any of this.

Bruce



> On Nov 2, 2020, at 9:17 AM, Michael Paus <m...@jugs.org> wrote:
> 
> Hi,
> I very much like the idea in general but I think it falls too short.
> Such an interface should not be tied to any existing graphics
> framework. Instead it should be just pure Java. This would allow
> to write complex graphics rendering code for a lot of different
> platforms and not only platforms where JavaFX already exists
> and can be used. Just think of the Android canvas or, via cross-
> compilation, even the HTML 5 canvas.
> Just my two €ent.
> Michael
> 
> Am 02.11.20 um 14:18 schrieb Bruce Johnson:
>> A variety of packages (for example,  VectorGraphics or JFreeSVG) exist that 
>> allow redirecting Java2D drawing to output other than the Java2D canvas.
>> 
>> These work by extending java.awt.Graphics2D.  By passing the extended 
>> Graphics2D object into a paint method, output can be redirected to a file 
>> (.svg, .pdf, etc.).
>> 
>> The GraphicsContext class of JavaFX serves a similar function to Graphics2D 
>> of java.awt, but because it is a final class it cannot be extended to create 
>> similar functionality as found in VectorgGraphics or JFreeSVG.
>> 
>> This is a serious limitation (at least as far as I can tell) to JavaFX 
>> applications.  It would be highly desirable to be able to redirect drawing 
>> on a Canvas to other output formats such as vector graphics files (.svg, 
>> .pdf etc.).
>> 
>> I currently work around this by using composition.  I have a Java interface 
>> that has most methods of GraphicsContext.  Then a GraphicsContextProxy class 
>> implements the interface and contains an instance of GraphicsContext.  This 
>> class is used for drawing to the Canvas.  I’ve then created a 
>> SVGGraphicsContext and PDFGraphicsContext that implement the interface and 
>> these can be used to draw to .svg or .pdf files.
>> 
>> This works, but means that all code that draws on the canvas has to be 
>> rewritten to take the GraphicsContextInterface rather than the normal 
>> GraphicsContext.
>> 
>> It would be much simpler if
>>      1) GraphicsContext was not final and could be extended.
>>      or
>>      2) A GraphicsContextInterface existed that GraphicsContext implemented. 
>>  Developers could then have alternative GraphicsContext implementations that 
>> implemented that interface.  This would require canvas drawing code to be 
>> written to use the interface, but would still be very useful.
>> 
>> Either solution could (I think) be easily implemented in JavaFX without 
>> breaking existing code and add a significant advance to the toolkit.
>> 
>> If there are alternative solutions to the problem, that would allow 
>> exporting canvas drawing to vector graphics files without requiring a change 
>> to the code that draws to the canvas, I’d appreciate hearing them.
>> 
>> — Bruce
>> 
> 

Reply via email to