java.awt.geom多边形_从java.awt.geom.Area转换为java.awt.Polygon

我需要将java.awt.geom.Area或java.awt.Shape转换为java.awt.Polygon.我所知道的是:isSingular = true,isPolygonal = true.因此,我认为多边形应该能够描述相同的区域.

解决方法:

我不确定是否值得转换,因为Polygon是一个旧的Java 1.0类,只能存储整数坐标,因此您可能会失去一些精度.

无论如何,您可以从Shape中获取PathIterator,并在对其进行迭代时将新点添加到Polygon中:

public static void main(String[] args) {

Area a = new Area(new Rectangle(1, 1, 5, 5));

PathIterator iterator = a.getPathIterator(null);

float[] floats = new float[6];

Polygon polygon = new Polygon();

while (!iterator.isDone()) {

int type = iterator.currentSegment(floats);

int x = (int) floats[0];

int y = (int) floats[1];

if(type != PathIterator.SEG_CLOSE) {

polygon.addPoint(x, y);

System.out.println("adding x = " + x + ", y = " + y);

}

iterator.next();

}

}

编辑正如比尔·林(Bill Lin)所说,如果PathIterator描述了多个子路径(例如,在带孔的区域的情况下),则此代码可能会给您一个错误的多边形.为了考虑到这一点,还需要检查PathIterator.MOVETO段,并可能创建一个多边形列表.

为了确定哪些多边形是孔,您可以计算边界框(Shape.getBounds2D()),然后检查哪个边界框包含另一个多边形.请注意,getBounds2D API表示“无法保证返回的Rectangle2D是包围Shape的最小边界框,只能保证Shape完全位于指定的Rectangle2D内”,但根据我的经验,多边形将是最小的,无论如何,要计算多边形的确切边界框很简单(只要找到最小和最大的x和y坐标)即可.

标签:java-2d,awt,java

来源: https://codeday.me/bug/20191120/2041084.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用JavaGeoJSON转换为图片的详细代码: ```java import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.util.List; import javax.imageio.ImageIO; import org.geojson.Feature; import org.geojson.FeatureCollection; import org.geojson.GeoJsonObject; import org.geojson.LngLatAlt; import org.geojson.Point; import org.geojson.Polygon; import org.geojson.jackson.JsonParser; import org.geojson.jackson.ObjectMapper; public class GeoJsonToImage { public static void main(String[] args) throws Exception { // 读取GeoJSON文件 FileInputStream fis = new FileInputStream(new File("input.geojson")); ObjectMapper mapper = new ObjectMapper(); JsonParser parser = new JsonParser(mapper); FeatureCollection featureCollection = parser.parseFeatureCollection(fis); // 计算GeoJSON数据的范围 double minX = Double.POSITIVE_INFINITY; double minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY; double maxY = Double.NEGATIVE_INFINITY; for (Feature feature : featureCollection.getFeatures()) { GeoJsonObject geometry = feature.getGeometry(); if (geometry instanceof Point) { LngLatAlt coordinates = ((Point) geometry).getCoordinates(); minX = Math.min(minX, coordinates.getLongitude()); minY = Math.min(minY, coordinates.getLatitude()); maxX = Math.max(maxX, coordinates.getLongitude()); maxY = Math.max(maxY, coordinates.getLatitude()); } else if (geometry instanceof Polygon) { List<List<LngLatAlt>> coordinates = ((Polygon) geometry).getCoordinates(); for (List<LngLatAlt> ring : coordinates) { for (LngLatAlt point : ring) { minX = Math.min(minX, point.getLongitude()); minY = Math.min(minY, point.getLatitude()); maxX = Math.max(maxX, point.getLongitude()); maxY = Math.max(maxY, point.getLatitude()); } } } } // 计算图片的大小和比例 int width = 800; int height = (int) (800 * (maxY - minY) / (maxX - minX)); double xRatio = width / (maxX - minX); double yRatio = height / (maxY - minY); // 创建图片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); g2d.setBackground(Color.WHITE); g2d.clearRect(0, 0, width, height); // 绘制GeoJSON数据 for (Feature feature : featureCollection.getFeatures()) { GeoJsonObject geometry = feature.getGeometry(); if (geometry instanceof Point) { LngLatAlt coordinates = ((Point) geometry).getCoordinates(); int x = (int) ((coordinates.getLongitude() - minX) * xRatio); int y = (int) ((maxY - coordinates.getLatitude()) * yRatio); g2d.setColor(Color.RED); g2d.fillRect(x - 5, y - 5, 10, 10); } else if (geometry instanceof Polygon) { List<List<LngLatAlt>> coordinates = ((Polygon) geometry).getCoordinates(); for (List<LngLatAlt> ring : coordinates) { int[] xPoints = new int[ring.size()]; int[] yPoints = new int[ring.size()]; for (int i = 0; i < ring.size(); i++) { LngLatAlt point = ring.get(i); int x = (int) ((point.getLongitude() - minX) * xRatio); int y = (int) ((maxY - point.getLatitude()) * yRatio); xPoints[i] = x; yPoints[i] = y; } g2d.setColor(Color.GREEN); g2d.fillPolygon(xPoints, yPoints, ring.size()); } } } // 保存图片 ImageIO.write(image, "png", new File("output.png")); // 关闭流 fis.close(); } } ``` 这个程序使用 `org.geojson` 包解析 GeoJSON 数据,绘制点和多边形,并保存为 PNG 图片。你可以替换 `org.geojson` 包为其它 GeoJSON 解析库,以适应你的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值