I'm trying to draw an itinary on the map (JMapPane) based on a list of points
or coordinates that defines the succesive positions of the object I'm tracking
on the map. How can I do this ?
I've tried the following code but it dosen't work:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tools;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Point;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.List;
import org.geotools.gui.swing.JMapPane;
/**
*
* @author RAZ
*/
public class MyRouteDrawer {
private static BasicStroke stroke = new BasicStroke(4.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static boolean drawNumbers = false;
private static boolean drawPoints = true;
private static boolean drawMap = true;
private static Color strokeColor = Color.YELLOW;
private static int OVAL_SIZE = 2;
private static Graphics2D g2;
public static Shape shape = null;
private static JMapPane MAP = null;
/**
* Draw route.
*
* @param coordList list of coordinates for route
*/
public static void drawStraightRoute(List<Coordinate> coordList) {
int x = 0, y = 0, prevX = 0, prevY = 0;
int nr = 0;
Iterator itr = coordList.iterator();
Coordinate coord = (Coordinate) itr.next();
g2.setStroke(stroke);
g2.setColor(strokeColor);
//Draw first point
x = (int) calcX(coord.x);
y = (int) calcY(coord.y);
nr ++;
if (drawPoints)
g2.drawOval(x,y,OVAL_SIZE,OVAL_SIZE);
if (drawNumbers)
g2.drawString(nr + "",x,y);
//Draw rest of route
while (itr.hasNext()) {
coord = (Coordinate) itr.next();
prevX = x;
prevY = y;
nr++;
x = (int) calcX(coord.x);
y = (int) calcY(coord.y);
// System.out.println("Drawing: " + x + ", " + y + ", " + prevX
// + ", " + prevY);
g2.drawLine(x, y, prevX, prevY);// (startx, starty, prevx, prevy);
if (drawPoints)
g2.drawOval(x, y, OVAL_SIZE, OVAL_SIZE);
//g2.drawString("Pos: " + coord.getX() + ", " + coord.getY(),x,y);
if (drawNumbers)
g2.drawString(nr + "", x, y);
// + coord.getX() + ", " + coord.getY(), x, y);
// drawBezierRoute(coordList);
}
}
/** Drawing route in bezier splines */
// TODO make this work nicely
public static void drawBezierRoute(List<Coordinate> coordList) {
double x1, x2, x3, x4, y1, y2, y3, y4;
Shape curve = null;
Iterator itr = coordList.iterator();
Coordinate coord = (Coordinate) itr.next();
x2 = calcX(coord.x);
y2 = calcY(coord.y);
coord = (Coordinate) itr.next();
x3 = calcX(coord.x);
y3 = calcY(coord.y);
coord = (Coordinate) itr.next();
x4 = calcX(coord.x);
y4 = calcY(coord.y);
g2.setStroke(stroke);
g2.setColor(strokeColor);
for (Coordinate coordz : coordList) {
x1 = x2; x2 = x3; x3 = x4; x4 = calcX(coordz.x);
y1 = y2; y2 = y3; y3 = y4; y4 = calcY(coordz.y);
// curve = new QuadCurve2D.Double(x1, y1, x2, y2, x3, y3);
// double wx1 = x2 + (sqrt(pow(x1 - x2, 2))) / weight * x1 - x3;
// double wy1 = y2 + (sqrt(pow(y1 - y2, 2))) / weight * y1 - y3;
// double wx2 = x3 + (sqrt(pow(x2 - x3, 2))) / weight * x2 - x4;
// double wy2 = y3 + (sqrt(pow(y2 - y3, 2))) / weight * y2 - y4;
double wx1 = x2 + (x2-x1);
double wy1 = y2 + (y2-y1);
double wx2 = x3 + (x4-x3);
double wy2 = y3 + (y4-y3);
// double wx1 = x2 + (sqrt(pow(x2 - x1, 2)
// + (pow(y2 - y1, 2)))) / weight * x3 - x1;
// double wy1 = y2 + (sqrt(pow((x2 - x1), 2)
// + (pow(y2 - y1, 2)))) / weight * y3 - y1;
// double wx2 = x3 + (sqrt(pow((x3 - x2), 2)
// + (pow(y3 - y2, 2)))) / weight * x4 - x2;
// double wy2 = y3 + (sqrt(pow((x3 - x2), 2)
// + (pow(y3 - y2, 2)))) / weight * y4 - y2;
// curve = new CubicCurve2D.Double(x1,y1,x2,y2,x3,y3,x4,y4);
curve = new CubicCurve2D.Double(x2, y2, wx1, wy1,
wx2, wy2, x3, y3);
g2.draw(curve);
}
shape = curve;
// curve = new QuadCurve2D.Float(x, y, prevX, prevY, w * .9f, yy);
// curve = new QuadCurve2D.Double(100, 100, 150, 150, 200, 100);
//
// g2.draw(curve);
}
/**
* Draw route.
*
* @param coordList list of coordinates for route
*/
public static void drawStraightRoute1(List<Coordinate> coordList) {
int x = 0, y = 0, prevX = 0, prevY = 0;
int nr = 0;
Iterator itr = coordList.iterator();
Coordinate coord = (Coordinate) itr.next();
g2.setStroke(stroke);
g2.setColor(strokeColor);
//Draw first point
x = (int) calcX(coord.x);
y = (int) calcY(coord.y);
nr ++;
if (drawPoints)
g2.drawOval(x,y,OVAL_SIZE,OVAL_SIZE);
if (drawNumbers)
g2.drawString(nr + "",x,y);
//Draw rest of route
while (itr.hasNext()) {
coord = (Coordinate) itr.next();
prevX = x;
prevY = y;
nr++;
x = (int) calcX(coord.x);
y = (int) calcY(coord.y);
// System.out.println("Drawing: " + x + ", " + y + ", " + prevX
// + ", " + prevY);
g2.drawLine(x, y, prevX, prevY);// (startx, starty, prevx, prevy);
if (drawPoints)
g2.drawOval(x, y, OVAL_SIZE, OVAL_SIZE);
//g2.drawString("Pos: " + coord.getX() + ", " + coord.getY(),x,y);
if (drawNumbers)
g2.drawString(nr + "", x, y);
// + coord.getX() + ", " + coord.getY(), x, y);
// drawBezierRoute(coordList);
}
}
/** Drawing route in bezier splines */
// TODO make this work nicely
public static void drawBezierRoute1(List<Point> pointList) {
double x1, x2, x3, x4, y1, y2, y3, y4;
Shape curve = null;
Iterator itr = pointList.iterator();
Point point = (Point) itr.next();
x2 = calcX(point.getX());
y2 = calcY(point.getY());
point = (Point) itr.next();
x3 = calcX(point.getX());
y3 = calcY(point.getY());
point = (Point) itr.next();
x4 = calcX(point.getX());
y4 = calcY(point.getY());
g2.setStroke(stroke);
g2.setColor(strokeColor);
for (Point pointz : pointList) {
x1 = x2; x2 = x3; x3 = x4; x4 = calcX(pointz.getX());
y1 = y2; y2 = y3; y3 = y4; y4 = calcY(pointz.getY());
double wx1 = x2 + (x2-x1);
double wy1 = y2 + (y2-y1);
double wx2 = x3 + (x4-x3);
double wy2 = y3 + (y4-y3);
curve = new CubicCurve2D.Double(x2, y2, wx1, wy1,
wx2, wy2, x3, y3);
g2.draw(curve);
}
shape = curve;
}
/**
* Drawing route in bezier splines
* @param coordList List of coordinates representing the different positions
* to draw.
*/
public static void drawBezierRoute2(List<Coordinate> coordList) {
double x1, x2, x3, x4, y1, y2, y3, y4;
GeneralPath curve = new GeneralPath();
Iterator itr = coordList.iterator();
Coordinate coord = (Coordinate) itr.next();
x1 = calcX(coord.x);
y1 = calcY(coord.y);
curve.moveTo(x1, y1);
g2.setStroke(stroke);
g2.setColor(strokeColor);
while(itr.hasNext()){
coord = (Coordinate) itr.next();
x2 = calcX(coord.x);
y2 = calcY(coord.y);
curve.lineTo(x2, y2);
}
g2.draw(curve);
shape = curve;
MAP.repaint();
}
public static Graphics2D getG2() {
return g2;
}
public static void setG2(Graphics2D g2) {
MyRouteDrawer.g2 = g2;
}
public static void setMAP(JMapPane MAP) {
MyRouteDrawer.MAP = MAP;
}
public static void setDrawMap(boolean drawMap) {
MyRouteDrawer.drawMap = drawMap;
}
public static void setDrawNumbers(boolean drawNumbers) {
MyRouteDrawer.drawNumbers = drawNumbers;
}
public static void setDrawPoints(boolean drawPoints) {
MyRouteDrawer.drawPoints = drawPoints;
}
public static void setShape(Shape shape) {
MyRouteDrawer.shape = shape;
}
public static void setStroke(BasicStroke stroke) {
MyRouteDrawer.stroke = stroke;
}
public static void setStrokeColor(Color strokeColor) {
MyRouteDrawer.strokeColor = strokeColor;
}
/** Calculates x coordinate for plotting */
public static double calcX(double mapX) {
// return (ratio * (x - limits[0]));
if(MAP == null)
return mapX;
else{
Rectangle bounds = MAP.getBounds();
Envelope mapArea = MAP.getMapArea();
double width = mapArea.getWidth();
double frameX = ((mapX - mapArea.getMinX()) *
(double)bounds.width)/width;
return frameX;
}
}
/** Calculates y coordinate for plotting */
public static double calcY(double mapY) {
// return -(ratio * (y - limits[3]));
if(MAP == null)
return mapY;
else{
Rectangle bounds = MAP.getBounds();
Envelope mapArea = MAP.getMapArea();
double height = mapArea.getHeight();
double frameY = (((mapArea.getMinY() - mapY) *
(double)bounds.height)/height) + bounds.getHeight();
return frameY;
}
}
}
In this class the Graphics2D object is initialize with the getGraphics() method
of the JMapPane object.
Must I extend the JMapPane class to override the paint(Graphics g) method or
paintComponent(Graphics g) method? Won't this have any impact on the showed map?
Or must I use a graph to do this?
_________________________________________________________________
A la recherche de bons plans pour une rentrée pas chère ? Bing ! Trouvez !
http://www.bing.com/search?q=bons+plans+rentr%C3%A9e&form=MVDE6------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users