I have a java library which heavily uses java.awt.Graphics2d.
I want to port my library to html5 canvas by using gwt.
So I'm planning to write an interface (or just a class), say common.Graphics2d, an adapter class, say com.test.awt.Graphics2d, implements common.Graphics2d and uses java.awt.Graphics2d
and another adapter class, say com.test.gwt.Graphics2d, implements common.Graphics2d and uses com.google.gwt.canvas.dom.client.Context2d.
Then I will replace all java.awt.Graphics2d with common.Graphics2d. So after that, my library will work on both gwt and java.
The problem here is to implement graphics2d methods, and configuration by canvas context 2d. Is it feasible to implement same functionality with canv开发者_如何学Cas?
I have done a similar thing. I have an interface which represents aview and two implementations of said interface. One for Android using its android.graphics
classes and a second implementations in GWT using com.google.gwt.canvas.client.Canvas
.
The GWT canvas stuff seems pretty full-featured to me. You can draw shapes, display text and images, move, rotate, scale...
It probably depends on the functions you use (for instance color gradient may not be easy). for basic drawing functions, the number of methods you really need to implement is very small.
You can have a look (and reuse) classes from my jvect-clipboard package for instance (on sourceforge). Basically, all geometric methods can use the general path drawing commmand, and you are left with storing colors and the like.
Have a look for instance at the implementation for SVG or WMF output, you will see that the code is pretty simple, especially for SVG (although it doesn't cover all possibilities, in particular gradients).
精彩评论