How to draw absolutely custom shape in Java?

只愿长相守 提交于 2019-12-07 18:23:50

问题


The most complex shape, supported by Java2D API is a Bezier segment. Suppose I want to draw rational segment (each control point has a weight and the entire rendering formula is slightly different).

How to accomplish that?

Is it possible to extend rendering engine to be able to draw more complex shapes?

UPDATE

Usual way to implement custom shape is implementing Shape interface. This interface has key methods to return PathIterator while PathIterator iterates over segment types. There are only 5 segment types. The most curved of them is SEG_CUBICTO which is standard Bezier curve with 4 control points (including 2 for beginning and end).

If I apply linear fractional transform to bezier curve, each control point get a weight, as an addition to it's coordinates, and Bezier curve turns to NURBS (not sure about that, have failed to learn exact terminology). Anyway, the formula for curve differs from Bezier.


回答1:


You cannot really extend the rendering engine: you can create Graphics/Graphics2D subclasses, but you have no control over the instantiation, so you cannot force the drawing framework to pass your subclass to the painting methods.

What you can do is to create a RationalShape implementation of Shape that has methods that draw anything you like, and returns a PathIterator which approximates it using Bézier splines. As a user of graphical programs like Photoshop I found that every curve can be approximated very well with Bézier splines, I don't know how complicated the math behind this approximation would be.



来源:https://stackoverflow.com/questions/20050086/how-to-draw-absolutely-custom-shape-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!