简体   繁体   中英

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? 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 . 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.

  1. Start at share/classes/java/awt/Graphics.java , which is the interface containing the fillPolygon code you'll likely use. 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. It simply delegates the fillPolygon call to a matching call on a sun.java2d.pipe.PixelFillPipe object. 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. Its fill implementation makes use of a fillSpans method, the core of which is implemented in native code.
  4. share/native/sun/java2d/pipe/BufferedRenderPipe.c has the corresponding implementation. 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 . There is also custom code for OpenGL-based graphics , but I can't see a fill optimization in that just now. But it might be good to have a closer look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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