JMapViewer,绘制到OSM,避免重复调用MouseClick事件



假设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, mousellistener被调用2次

类不是直接从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()没有被调用…

谢谢你的帮助。

p.add(map);

您正在向面板添加"map"。因此,您需要重写JMapViewwer类的paintComponent()方法。

当你重写一个类的方法时,你应该使用:

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

如果不正确地重写方法,则会得到编译错误。

然而,在你的例子中,你甚至不应该重写paintComponent()方法。这种方法只适用于绘画。它不是用于添加新的多边形对象。也许代码应该在mousePressed()逻辑中?

已找到以下解决方案:

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。希望有帮助:-)

相关内容

  • 没有找到相关文章

最新更新