Suppose Map3 to be the following class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JPanel;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
public class Map3 {
private JPanel p;
private JMapViewer map;
private double lat, lon;
public Map3()
{
p = new JPanel();
map = new JMapViewer();
p.setLayout(new BorderLayout());
new DefaultMapController(map) {
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
lat = map.getPosition(p).getLat();
lon = map.getPosition(p).getLon();
}
//Where to locate the method ????
protected void paintComponent(Graphics g){
Coordinate c1= new Coordinate(lat,lon),c2= new Coordinate(lat+10,lon+10); //Draw the line
List<Coordinate> route = new ArrayList<Coordinate>(Arrays.asList(c1, c2, c1));
map.addMapPolygon(new MapPolygonImpl(route));
}
};
p.add(map);
p.setVisible(true);
}
public JPanel getJPanel() {return p;}
}
To avoid the double call of the mouse listener, see the question
JMapViewer, MouseListener called 2 times
the class is not directly derived from JMapViewer. Using the mouse click I got two coordinates [lat, lon] that will be used to draw a line given by P1, P2, where P1=[lat, lon], P2=[lat+10, lon+10].
I am not sure where the method paintComponent() should be placed to be able to add some drawings to the OSM map.
public class TEST
{
public static void main (String [] args)
{
JFrame jf = new JFrame();
jf.setSize(800, 600);
Map3 p = new Map3();
jf.add(p.getJPanel());
jf.setVisible(true);
}
}
The current version does not work well, the paintComponent() is not called...
Thanks for your help.