我是靠谱客的博主 安详乐曲,最近开发中收集的这篇文章主要介绍java填充多边形,Java如何绘制和填充有孔的多边形,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I am currently trying to draw and fill a Polygon which has a hole in it in Java. Normally this would not be a big problem, since I would draw the exterior ring and then draw the interior ring with the color of the background.

But the problem is, that the polygon is displayed above a image which should be "seen" through the hole.

I am writing the code in Java and am using JTS Topology Suite for my geometry data.

This is my current code, which just paints the border and fills the polygon with a color.

private void drawPolygon(com.vividsolutions.jts.geom.Polygon gpoly, Color color, Graphics2D g2d){

java.awt.Polygon poly = (java.awt.Polygon)gpoly;

for(Coordinate co : gpoly.getExteriorRing().getCoordinates() {

poly.addPoint(co.x, co.y);

}

g2d.setColor(col);

g2d.fill(poly);

g2d.setColor(Color.BLACK);

g2d.draw(poly);

}

Sadly java.awt.Polygon does not support Polygons with holes.

解决方案

Use the Polygon as the basis for an Area (e.g. called polygonShape).

Create an Ellipse2D for the 'hole', then establish an Area for it (ellipseShape).

Area polygonWithHole = polygonShape.subtract(ellipseShape);

最后

以上就是安详乐曲为你收集整理的java填充多边形,Java如何绘制和填充有孔的多边形的全部内容,希望文章能够帮你解决java填充多边形,Java如何绘制和填充有孔的多边形所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部