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

时间:2008-09-24 21:03:33

标签: java geometry awt shape jts

是否可以将com.vividsolutions.jts.geom.Geometry(或其子类)转换为实现java.awt.Shape的类?我可以使用哪种库或方法来实现这一目标?

2 个答案:

答案 0 :(得分:5)

另请查看JTS库提供的ShapeWriter。我使用以下代码剪切将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);
    }
}

编辑:结果如下图Visualization of jts geometry objects in awt

答案 1 :(得分:2)

根据:

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

有一堂课:

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

可以做到吗?