JMapViewer, draw to OSM and avoid double call of MouseClick event

250 Views Asked by At

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.

2

There are 2 best solutions below

2
justik On BEST ANSWER

The following solution has been found:

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
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 extends JMapViewer{
private double lat, lon;

public Map3() 
{
   new DefaultMapController(this) {
        public void mouseClicked(MouseEvent e) {
            System.out.println(e.getPoint());
            Point  p = e.getPoint();
            lat = map.getPosition(p).getLat();
            lon = map.getPosition(p).getLon();
        }};
  }

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Coordinate c1= new Coordinate(lat,lon),c2= new Coordinate(lat+10,lon+10);
    List<Coordinate> route = new ArrayList<Coordinate>(Arrays.asList(c1, c2, c1));
    this.addMapPolygon(new MapPolygonImpl(route));
}   

public static void main (String [] args){
            JFrame jf = new JFrame();
            jf.setSize(800, 600);
            Map3 m= new Map3();
            jf.add(m);
            jf.setVisible(true);
    }
}

The main idea is to replace

 addMouseListener(new DefaultMapController(this) {
        public void mouseClicked(MouseEvent e){
            System.out.println(e.getPoint());
            Point  p = e.getPoint();
            lat = map.getPosition(p).getLat();
            lon = map.getPosition(p).getLon();
        });

with the following construction

new DefaultMapController(this) {
        public void mouseClicked(MouseEvent e) {
            System.out.println(e.getPoint());
            Point  p = e.getPoint();
            lat = map.getPosition(p).getLat();
            lon = map.getPosition(p).getLon();
        }};

In this case, Map3 may be derived from JMapViewer. Hope it helps :-)

2
camickr On
p.add(map);

You are adding the "map" to the panel. Therefore you need to override the paintComponent() method of the JMapViewwer class.

Whenever you override a method of a class you should use:

@Override
protected void paintComponent(Graphics g)
...

Then you will get a compile error if you override a method incorrectly.

However, in your case you should not even be overriding the paintComponent() method. That method is for painting only. It is NOT for adding new polygon objects. Maybe the code should be in the mousePressed() logic?