是否可以将 com.vividsolutions.jts.geom.Geometry
(或其子类)转换为实现 java.awt.Shape
的类?我可以使用哪个库或方法来实现该目标?
最佳答案
另请查看 ShapeWriter由 JTS 库提供。我使用以下代码片段将 jts 几何对象转换为 awt 形状。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.vividsolutions.jts.awt.ShapeWriter;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Polygon;
public class Paint extends JPanel{
public void paint(Graphics g) {
Coordinate[] coords = new Coordinate[] {new Coordinate(400, 0), new Coordinate(200, 200), new Coordinate(400, 400), new Coordinate(600, 200), new Coordinate(400, 0) };
Polygon polygon = new GeometryFactory().createPolygon(coords);
LineString ls = new GeometryFactory().createLineString(new Coordinate[] {new Coordinate(20, 20), new Coordinate(200, 20)});
ShapeWriter sw = new ShapeWriter();
Shape polyShape = sw.toShape(polygon);
Shape linShape = sw.toShape(ls);
((Graphics2D) g).draw(polyShape);
((Graphics2D) g).draw(linShape);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new Paint());
f.setSize(700, 700);
f.setVisible(true);
}
}
编辑:结果看起来像这张图片
关于java - 如何将 JTS-Geometry 转换为 AWT-Shape?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/129968/