简体   繁体   English

JMapViewer,使用OSM并避免两次调用MouseClick事件

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

Suppose Map3 to be the following class: 假设Map3为以下类:

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 JMapViewer,MouseListener调用了2次

the class is not directly derived from JMapViewer. 该类不是直接从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]. 使用鼠标单击,我得到两个坐标[lat,lon],它们将用于绘制由P1,P2给定的线,其中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. 我不确定应将方法paintComponent()放置在何处,以便能够向OSM映射中添加一些图形。

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... 当前版本无法正常工作,paintComponent()未被调用...

Thanks for your help. 谢谢你的帮助。

p.add(map);

You are adding the "map" to the panel. 您正在将“地图”添加到面板。 Therefore you need to override the paintComponent() method of the JMapViewwer class. 因此,您需要重写JMapViewwer类的paintComponent()方法。

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. 但是,在您的情况下,您甚至不应覆盖paintComponent()方法。 That method is for painting only. 该方法仅用于绘画。 It is NOT for adding new polygon objects. 它不是用于添加新的多边形对象。 Maybe the code should be in the mousePressed() logic? 也许代码应该在mousePressed()逻辑中?

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. 在这种情况下,Map3可以从JMapViewer派生。 Hope it helps :-) 希望能帮助到你 :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM