JMapViewer,绘制到OSM并避免鼠标单击事件的双重调用

时间:2016-09-17 15:55:18

标签: java swing paintcomponent jmapviewer

假设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;}
}

要避免鼠标监听器的双重调用,请参阅问题

JMapViewer, MouseListener called 2 times

该类不是直接从JMapViewer派生的。使用鼠标单击我得到两个坐标[lat,lon],用于绘制由P1,P2给出的线,其中P1 = [lat,lon],P2 = [lat + 10,lon + 10]。

我不确定应该放置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);
    }
}

当前版本不能正常工作,不会调用paintComponent()...

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

p.add(map);

您正在向面板添加“地图”。因此,您需要覆盖paintComponent()类的JMapViewwer方法。

每当你覆盖一个类的方法时,你应该使用:

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

如果您错误地覆盖方法,则会出现编译错误。

但是,在您的情况下,您甚至不应该重写paintComponent()方法。该方法仅用于绘画。它不适用于添加新的多边形对象。也许代码应该在mousePressed()逻辑中?

答案 1 :(得分:0)

已找到以下解决方案:

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);
    }
}

主要想法是替换

 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();
        });

具有以下结构

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();
        }};

在这种情况下,Map3可以从JMapViewer派生。希望它有所帮助: - )