JMapViewer,绘制到 OSM 并避免重复调用 MouseClick 事件
JMapViewer, draw to OSM and avoid double call of MouseClick event
假设 Map3 如下 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;}
}
避免鼠标监听器重复调用,见题
class 不是直接派生自 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
class.
的 paintComponent()
方法
无论何时重写 class 的方法,您都应该使用:
@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。希望对您有所帮助:-)
假设 Map3 如下 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;}
}
避免鼠标监听器重复调用,见题
class 不是直接派生自 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
class.
paintComponent()
方法
无论何时重写 class 的方法,您都应该使用:
@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。希望对您有所帮助:-)