¿Cómo puedo convertir una geometría JTS en una forma AWT?
Pregunta
¿Es posible convertir un com.vividsolutions.jts.geom.Geometry
(o una subclase de él) en una clase que implementa java.awt.Shape
? ¿Qué biblioteca o método puedo usar para lograr ese objetivo?
Solución
Según:
http: //lists.jump-project .org / pipermail / jts-devel / 2007-May / 001954.html
Hay una clase:
com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter
¿Qué puede hacerlo?
Otros consejos
También eche un vistazo a ShapeWriter proporcionado por la biblioteca JTS. Utilicé el siguiente código cortado para convertir objetos de geometría jts en una forma 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);
}
}
Editar: El resultado se parece a esta imagen
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow