/* * Created on July 23, 2003 * * Copyright© NGI Systems 2003 */ package roim.geoviewer.mousemode; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import com.bbn.openmap.LatLonPoint; import com.bbn.openmap.MapBean; import com.bbn.openmap.event.AbstractMouseMode; import com.bbn.openmap.event.CenterEvent; import com.bbn.openmap.event.PaintListener; /** * @author Brian Hudson * * PanMouseMode */ public class PanMouseMode extends AbstractMouseMode implements PaintListener { public final static transient String modeID = "Pan"; protected Point _initialMousePoint; // Initial Mouse Position protected Point _mousePoint; // Current Mouse Position protected Point _startPoint; // Top Left Hand Corner of Map Image protected Image _mapImage; // Image of the Map protected Image _completeImage; // Image of Map and Background protected ImageIcon _iiCursor; // Cursor Image protected int _lastButton; // The last mouse button that was pressed public PanMouseMode() { this(true); } public PanMouseMode(boolean shouldConsumeEvents) { super(modeID, shouldConsumeEvents); try { java.net.URL url = this.getClass().getResource("cursors/pan.gif"); setModeCursor(ui.CursorFactory.createCursor(url, "Pan", new Point(8, 10))); } catch (Exception x) { x.printStackTrace(); } } public void mousePressed(MouseEvent e) { MapBean map = (MapBean)e.getSource(); _lastButton = e.getButton(); if (_lastButton == MouseEvent.BUTTON1) { _startPoint = new Point(0, 0); _mousePoint = e.getPoint(); _initialMousePoint = e.getPoint(); _mapImage = map.createImage(map.getWidth(), map.getHeight()); _completeImage = map.createImage(map.getWidth(), map.getHeight()); map.paint(_mapImage.getGraphics()); } else if (_lastButton == MouseEvent.BUTTON3) { LatLonPoint point = map.getCoordinates(e); map.center(new CenterEvent(this, point.getLatitude(), point.getLongitude())); } } public void mouseDragged(MouseEvent e) { if (_lastButton == MouseEvent.BUTTON1) { MapBean map = (MapBean)e.getSource(); Graphics mapG = map.getGraphics(); // Calc Delta Point deltaPoint = new Point((int) (_mousePoint.getX() - e.getX()), (int) (_mousePoint.getY() - e.getY())); _startPoint = new Point((int) (_startPoint.getX() - deltaPoint.getX()), (int) (_startPoint.getY() - deltaPoint.getY())); Graphics g = _completeImage.getGraphics(); g.setColor((Color)map.getBckgrnd()); g.fillRect(0, 0, map.getWidth(), map.getHeight()); g.drawImage(_mapImage, (int)_startPoint.getX(), (int)_startPoint.getY(), map.getWidth(), map.getHeight(), map); mapG.drawImage(_completeImage, 0, 0, map.getWidth(), map.getHeight(), map); _mousePoint = e.getPoint(); } } public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { MapBean map = (MapBean)e.getSource(); LatLonPoint initialLLP = map.getProjection().inverse((int)_initialMousePoint.getX(), (int)_initialMousePoint.getY()); LatLonPoint finalLLP = map.getCoordinates(e); float deltaLat = initialLLP.getLatitude() - finalLLP.getLatitude(); float deltaLon = initialLLP.getLongitude() - finalLLP.getLongitude(); LatLonPoint center = map.getCenter(); center.setLatitude(center.getLatitude() + deltaLat); center.setLongitude(center.getLongitude() + deltaLon); map.setCenter(center); _mapImage = null; _completeImage = null; } } }