0

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.

Community
  • 1
  • 1
justik
  • 4,145
  • 6
  • 32
  • 53
  • One of your classes should extend the type of drawable object you want to use. – Bálint Sep 17 '16 at 15:57
  • Possible duplicate of [*Draw Line between two Geo Points in JMapViewer*](http://stackoverflow.com/q/10744798/230513). – trashgod Sep 17 '16 at 16:21
  • @ trashgod This question relates to previous one (JMapViewer, MouseListener called 2 times) you answered. To avoid the problem, you recommended not to derive the class from JMapViewer. However, I also need to draw some objects, the coordinates of which are obtained from the mouse click in the OSM map... – justik Sep 17 '16 at 16:29
  • Please edit your question o clarify the goal. – trashgod Sep 17 '16 at 16:33
  • @ trashgod: question has been updated... – justik Sep 17 '16 at 16:54

2 Answers2

3
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?

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @ camickr: I am having a problem. When Map3 is derived from JMapViewer, I am able to draw to the Canvas. Unfortunately, the mouseclick event is called two time for the each click. The recommendation from S-O forum was not to derive Map from JMapViewer. – justik Sep 17 '16 at 16:14
  • @justik, the suggestion was to not add the MouseListener to the class twice. There is no reason to extend a class just to add a MouseListener to the class. As I already suggested you only override the paintComponent() method of a class to do custom painting. You do NOT override the paintComponent() method to add Objects to your JMapViewer class. – camickr Sep 17 '16 at 18:08
0

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 :-)

justik
  • 4,145
  • 6
  • 32
  • 53
  • 1
    Instead of overriding `paintComponent()`, add the polygon in the controller and `repaint()`; the map will update itself in response. – trashgod Sep 18 '16 at 01:58
  • @ trashgod: May I ask you for a clarification and a short example? – justik Sep 18 '16 at 07:01