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

发布于 2024-07-05 21:37:34 字数 126 浏览 9 评论 0原文

是否可以将 com.vividsolutions.jts.geom.Geometry (或其子类)转换为实现 java.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? Which library or method can I use to achieve that goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-07-12 21:37:34

另请参阅提供的 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);
    }
}

编辑:结果看起来像这个图像 Visualization of jts Geometry Objects in awt

Also have a look at ShapeWriter provided by the JTS library. I used the following code snipped to convert jts geometry objects into an awt shape.

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 Visualization of jts geometry objects in awt

如若梦似彩虹 2024-07-12 21:37:34

根据:

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

有一个类:

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

哪个可以做到这一点?

According to:

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文