Java如何绘制和填充有孔的多边形 [英] Java how to draw and fill a Polygon which has holes
问题描述
我目前正在尝试绘制和填充在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.
我正在用Java编写代码,并将 JTS拓扑套件用于我的几何数据.
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);
}
可悲地 java.awt.Polygon 不支持带孔多边形.
Sadly java.awt.Polygon does not support Polygons with holes.
推荐答案
- 使用
Polygon
作为Area
的基础(例如称为polygonShape
). - 为孔"创建一个
Ellipse2D
,然后为其创建一个Area
(ellipseShape
). -
- Use the
Polygon
as the basis for anArea
(e.g. calledpolygonShape
). - Create an
Ellipse2D
for the 'hole', then establish anArea
for it (ellipseShape
). Use
Area.subtract(Area)
something like:Area polygonWithHole = polygonShape.subtract(ellipseShape);
这篇关于Java如何绘制和填充有孔的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Use the