简体   繁体   English

Java如何填充多边形

[英]How does java fill a polygon

I've wondered for a while now how java fills his polygons, I've been trying it myself with scanline algorithms etc. etc. which have ended quite distorted uptill now, so I got myself wondering how does Java do it? 我一直想知道Java如何填充他的多边形,我一直在自己尝试使用scanline算法等。这些方法现在已经完全扭曲了,所以我想知道Java是如何做到的? Because that seems quite clear, does it also use scanlines or is it a whole different kind of algorithm? 因为这似乎很清楚,所以它是否也使用扫描线,还是一种完全不同的算法? (I've tried searching it in the source but hell, that's a maze.) (我尝试过在源代码中搜索它,但是,这真是一个迷宫。)

Regards 问候

The Open JDK sources are available online . Open JDK源可在线获得 So you can have a look at the code yourself, although you should keep in mind that by looking at that code, an using what you see as “inspiration” for your own code, you're likely subject to the OpenJDK license conditions, so don't have too close a look unless you are willing to use a compatible license for your code. 因此,您可以自己查看代码,尽管您应该记住,通过查看该代码(使用您认为对自己的代码的“灵感”),您很可能要遵守OpenJDK许可条件,因此除非您愿意为代码使用兼容的许可证,否则请不要过于仔细地看。

  1. Start at share/classes/java/awt/Graphics.java , which is the interface containing the fillPolygon code you'll likely use. share/classes/java/awt/Graphics.java ,这是包含您可能会使用的fillPolygon代码的接口。 It's an abstract method there, so you'll have to look at classes implementing that interface. 这是一个抽象方法,因此您必须查看实现该接口的类。
  2. share/classes/sun/java2d/SunGraphics2D.java is one such. share/classes/sun/java2d/SunGraphics2D.java就是这样的一种。 It simply delegates the fillPolygon call to a matching call on a sun.java2d.pipe.PixelFillPipe object. 它只是将fillPolygon调用委派给sun.java2d.pipe.PixelFillPipe对象上的匹配调用。 But since that is only an interface, you'll again have to look for implementations. 但是,由于那只是一个接口,因此您将不得不再次寻找实现。
  3. share/classes/sun/java2d/pipe/BufferedRenderPipe.java is one possible implementation. share/classes/sun/java2d/pipe/BufferedRenderPipe.java是一种可能的实现。 Its fill implementation makes use of a fillSpans method, the core of which is implemented in native code. 它的fill实现使用fillSpans方法,该方法的核心以本机代码实现。
  4. share/native/sun/java2d/pipe/BufferedRenderPipe.c has the corresponding implementation. share/native/sun/java2d/pipe/BufferedRenderPipe.c具有相应的实现。 So once you have, from looking at the code, understood what a span is and how a polygon gets translated into spans, then this might tell you how a span gets rendered. 因此,一旦您通过查看代码了解了跨度是什么以及多边形如何转换为跨度,那么这可能会告诉您跨度是如何呈现的。

All of the above are just one possible code path. 以上所有只是一种可能的代码路径。 It very much depends on what platform you are using, and what you are drawing to. 这在很大程度上取决于您所使用的平台以及所要使用的平台。 There are different primitives for printing, and different primitives for most platforms. 打印有不同的原语,而大多数平台有不同的原语。 For example, on a (non-accelerated) X11 connection, solaris/native/sun/java2d/x11/X11Renderer.c will simply delegate to XFillPolygon . 例如,在(非加速)X11连接上, solaris/native/sun/java2d/x11/X11Renderer.c将仅委托给XFillPolygon There is also custom code for OpenGL-based graphics , but I can't see a fill optimization in that just now. 对于基于OpenGL的图形 ,也有自定义代码 ,但是我现在看不到填充优化。 But it might be good to have a closer look. 但是仔细看看可能会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM