私の基本的な問題は、マップが表示されている内部フレームがあることです。そのマップにウェイポイントを描画できるように、そのマップをクリックした座標を取得したいのですが、基本的には何も得られません。getX と getY は、そのマップの外側 (内部フレーム内) をクリックした場合にのみ座標を返します。ウェイポイントを手動で追加すると、正常に機能します。私のコードは次のとおりです。
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLayer;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.CardLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.LayerUI;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import soft.MapDrawer;
import soft.MapView;
import java.awt.Component;
import javax.swing.JToggleButton;
import javax.swing.JRadioButton;
import javax.swing.JInternalFrame;
import java.awt.Canvas;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.Dimension;
import javax.swing.JLayeredPane;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.Box;
public class MainWindow {
private JFrame frame;
private JMenu MenuBasicActions;
private JInternalFrame mapFrameRef;
private MapDrawer mapD;
private MapView hlavnaMapa;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow()
{
initialize();
}
public JFrame getMainFrame()
{
return frame;
}
public JInternalFrame getMapFrame()
{
return mapFrameRef;
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setBounds(100, 100, 1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBarUpper = new JMenuBar();
frame.setJMenuBar(menuBarUpper);
MenuBasicActions = new JMenu("GPS Settings");
menuBarUpper.add(MenuBasicActions);
JMenuItem mntmNewMenuItem = new JMenuItem("New menu item");
MenuBasicActions.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("New menu item");
MenuBasicActions.add(mntmNewMenuItem_1);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("New menu item");
MenuBasicActions.add(mntmNewMenuItem_2);
JMenu DevicesMenu = new JMenu("Devices");
menuBarUpper.add(DevicesMenu);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("New menu item");
DevicesMenu.add(mntmNewMenuItem_3);
JMenuItem mntmNewMenuItem_4 = new JMenuItem("New menu item");
DevicesMenu.add(mntmNewMenuItem_4);
JMenu HelpMenu = new JMenu("Help");
menuBarUpper.add(HelpMenu);
JToolBar toolBar = new JToolBar();
toolBar.setOrientation(SwingConstants.VERTICAL);
frame.getContentPane().add(toolBar, BorderLayout.WEST);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
toolBar.add(btnNewButton_1);
JButton btnNewButton = new JButton("New button");
toolBar.add(btnNewButton);
JButton btnNewButton_2 = new JButton("New button");
toolBar.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("New button");
toolBar.add(btnNewButton_3);
toolBar.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{btnNewButton_1, btnNewButton}));
JInternalFrame mapInternalFrame = new JInternalFrame("New JInternalFrame");
mapFrameRef=mapInternalFrame;
mapInternalFrame.getGlassPane();
frame.getContentPane().add(mapInternalFrame, BorderLayout.CENTER);
mapInternalFrame.setVisible(true);
//mapFrame.getContentPane().add(drawPanel, BorderLayout.CENTER);
frame.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{frame.getContentPane(), toolBar, btnNewButton_1, btnNewButton, btnNewButton_2, btnNewButton_3, menuBarUpper, MenuBasicActions, mntmNewMenuItem, mntmNewMenuItem_1, mntmNewMenuItem_2, DevicesMenu, mntmNewMenuItem_3, mntmNewMenuItem_4, HelpMenu}));
/*Vlastny kod */
hlavnaMapa=new MapView();
hlavnaMapa.Init(mapInternalFrame);
DrawerEventHandler handler=new DrawerEventHandler();
mapInternalFrame.addMouseListener(handler);
mapInternalFrame.addMouseMotionListener(handler);
}
public class DrawerEventHandler implements MouseListener, MouseMotionListener
{
@Override
public void mouseDragged(MouseEvent event)
{
}
@Override
public void mouseMoved(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent event)
{
mapD=new MapDrawer(hlavnaMapa.getMap());
Point2D point=new Point2D.Double(event.getX(),event.getY());
mapD.addWaypoint(mapD.pointToGeo(point));
System.err.println(event.getX()+" "+event.getY());
}
@Override
public void mouseEntered(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent event) {
// TODO Auto-generated method stub
}
}
}
MapDrawer クラス:
package soft;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import org.jdesktop.swingx.JXMapKit;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.TileFactoryInfo;
import org.jdesktop.swingx.mapviewer.Waypoint;
import org.jdesktop.swingx.mapviewer.WaypointPainter;
import org.jdesktop.swingx.mapviewer.util.GeoUtil;
public class MapDrawer
{
private JXMapViewer view;
private JXMapKit mapRef;
private Set waypoints;
//private BufferedImage img;
//img = ImageIO.read(new File("D://workspace//GPS Admin Software//resources//iconPerson.jpg"));
public MapDrawer(JXMapKit mapa)
{
mapRef=mapa;
waypoints = new HashSet();
}
public Point2D geoToPoint(GeoPosition pos)
{
return view.convertGeoPositionToPoint(pos);
}
public GeoPosition pointToGeo(Point2D point)
{
return view.convertPointToGeoPosition(point);
}
public void addWaypoint(GeoPosition geo) {
//create a Set of waypoints
waypoints.add(new Waypoint(geo.getLatitude(),geo.getLongitude()));
//create a WaypointPainter to draw the points
WaypointPainter painter = new WaypointPainter();
painter.setWaypoints(waypoints);
mapRef.getMainMap().setOverlayPainter(painter);
}
}
MapViewer クラス
package soft;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import org.jdesktop.swingx.JXMapKit;
public class MapView
{
private JXMapKit map;
public MapView()
{
map=new JXMapKit();
map.setDefaultProvider(org.jdesktop.swingx.JXMapKit.DefaultProviders.OpenStreetMaps);
map.setDataProviderCreditShown(true);
}
public void Init(JInternalFrame container)
{
container.add(map);
}
public JXMapKit getMap()
{
return map;
}
}