Global Positioning System Reference
In-Depth Information
In order to retrieve these events from Swing, the scroll pane could
be tagged as MouseListener and MouseMotionListener and implement the
methods needed to move the map:
1. mousePressed > 2. mouseDragged > 3. mouseReleased
The actual implementation uses the MouseInputAdapter to avoid empty
method bodies. The dragging functionality is achieved with the help of
event triggers, completely hidden inside the scroll pane. The user should
not be bothered with the conversion of pixel to decimal values. Instead
of returning the cursor position in pixels, it should return the decimal
position.
The idea is to implement a mouse-click listener MousePosition to catch
the mouse-click event mouseClicked(MouseEvente) from Swing. At this
point, the MouseEvent can be extended to a MapEvent to add the decimal
coordinates. To restrict the number of classes, we create a MapMouseListener
with only one method:
MapMouseListener extends EventListener
{
void mapClickedAt( MouseEvent e, Point2D decPoint );
}
From the developer's perspective, the arguments of a listener method are
actually the return values from the event. The implementing client can use
the information about the mouse event provided by Swing and the decimal
point clicked on the map. The pixel-to-decimal conversion is hidden in the
MapPanel in order not to bother the map user with pixel coordinates.
A client, like the GPXviewer , using a MapPanel , only has to execute these
steps:
// 1. declare class as listener:
public class GPXviewer implements MapMouseListener
{
mapDisplay = new MapPanel( gridArea );
...
// 2. direct mouse events to this listener class:
mapDisplay.addMapMouseListener( this );
...
// 3. catch decimal coordinates of mouse click:
public void mapClickedAt(Point2D decPoint)
{
String lat = decPoint.getY() + "";
String lon = decPoint.getX() + "";
...
//
propagate coordinates to listener
}
}
 
Search WWH ::




Custom Search