Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JTS几何体转换为AWT形状?_Java_Geometry_Awt_Shape_Jts - Fatal编程技术网

Java 如何将JTS几何体转换为AWT形状?

Java 如何将JTS几何体转换为AWT形状?,java,geometry,awt,shape,jts,Java,Geometry,Awt,Shape,Jts,是否可以将com.lividsolutions.jts.geom.Geometry(或其子类)转换为实现java.awt.Shape的类?我可以使用哪个库或方法来实现该目标?根据: 有一门课: com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter 哪个可以做到这一点?还可以看看JTS库提供的。我使用以下截取的代码将jts几何体对象转换为awt形状 import java.awt.Graphics; impor

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

根据:

有一门课:

com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter
哪个可以做到这一点?

还可以看看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);
    }
}

编辑:结果看起来像这张图片

可能是个愚蠢的问题。为什么正方形有五个坐标?;-)别担心。我认为多边形必须以相同的坐标开始和结束。因此,第一个和最后一个是相同的