Hi Marin,

> how can I draw for example a JTS Geometry Line object in a JFrame?
> JPanel ...
>

The example below draws JTS LineStrings on a JPanel.

Hope this helps.

Michael


package org.geotools.demo;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.WKTReader;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LineDrawingPanel extends JPanel {
    private static final int MARGIN = 5;

    private List<LineString> lines = new ArrayList<LineString>();
    private AffineTransform geomToScreen;
    private boolean recalculateTransform = true;

    public void addLine(LineString line) {
        lines.add(line);
        recalculateTransform = true;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (!lines.isEmpty()) {
            if (recalculateTransform) {
                setTransform();
            }

            for (LineString line : lines) {
                Point[] screenCoords = getScreenCoords(line);
                for (int i = 1; i < screenCoords.length; i++) {
                    g.drawLine(screenCoords[i-1].x, screenCoords[i-1].y,
                               screenCoords[i].x, screenCoords[i].y);
                }
            }
        }
    }

    private void setTransform() {
        Envelope env = getGeometryBounds();
        Rectangle visRect = getVisibleRect();
        Rectangle drawingRect = new Rectangle(
                visRect.x + MARGIN, visRect.y + MARGIN, visRect.width
- 2*MARGIN, visRect.height - 2*MARGIN);

        double scale = Math.min(drawingRect.getWidth() /
env.getWidth(), drawingRect.getHeight() / env.getHeight());
        double xoff = MARGIN - scale * env.getMinX();
        double yoff = MARGIN + env.getMaxY() * scale;
        geomToScreen = new AffineTransform(scale, 0, 0, -scale, xoff, yoff);
        recalculateTransform = false;
    }

    private Envelope getGeometryBounds() {
        Envelope env = new Envelope();
        for (LineString line : lines) {
            Envelope lineEnv = line.getEnvelopeInternal();
            env.expandToInclude(lineEnv);
        }

        return env;
    }

    private Point[] getScreenCoords(LineString line) {
        Coordinate[] coords = line.getCoordinates();
        Point[] screenCoords = new Point[coords.length];
        Point2D p = new Point2D.Double();

        for (int i = 0; i < coords.length; i++) {
            p.setLocation(coords[i].x, coords[i].y);
            geomToScreen.transform(p, p);
            screenCoords[i] = new Point((int)p.getX(), (int)p.getY());
            System.out.println(screenCoords[i]);
        }

        return screenCoords;
    }

    public static void main(String[] args) throws Exception {
        LineDrawingPanel panel = new LineDrawingPanel();
        JFrame frame = new JFrame("Draw lines");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(500, 500);

        WKTReader reader = new WKTReader();

        LineString line = (LineString) reader.read(
                "LINESTRING(20 20, 20 25, 25 25, " +
                "25 15, 15 15, 15 30, 30 30, 30 10, " +
                "10 10, 10 35, 35 35, 35 5)");
        panel.addLine(line);

        line = (LineString) reader.read("LINESTRING(-10 40, 5 50, 20
40, 35 50, 50 40)");
        panel.addLine(line);

        frame.setVisible(true);
    }
}

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to