简体   繁体   English

如何将JTS-Geometry转换为AWT-Shape?

[英]How can I convert a JTS-Geometry into an AWT-Shape?

Is it possible to convert a com.vividsolutions.jts.geom.Geometry (or a subclass of it) into a class that implements java.awt.Shape ? 是否可以将com.vividsolutions.jts.geom.Geometry (或其子类)转换为实现java.awt.Shape的类? Which library or method can I use to achieve that goal? 我可以使用哪个库或方法来实现该目标?

Also have a look at ShapeWriter provided by the JTS library. 另请参阅 JTS库提供的ShapeWriter I used the following code snipped to convert jts geometry objects into an awt shape. 我使用以下代码剪切将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);
    }
}

Edit: The result looks like this image 编辑:结果看起来像这个图像 在awt中可视化jts几何对象

According to: 根据:

http://lists.jump-project.org/pipermail/jts-devel/2007-May/001954.html http://lists.jump-project.org/pipermail/jts-devel/2007-May/001954.html

There's a class: 有一节课:

com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter

which can do it? 哪能做到?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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