📄 simplerulermaptool.java
字号:
/**
* Copyright 2000 MapInfo Corporation
* You are free to use this code in your applications, but you may not
* redistribute the source code without this notice.
*/
import com.mapinfo.beans.vmapj.*;
import com.mapinfo.beans.tools.*;
import com.mapinfo.coordsys.LinearUnit;
import com.mapinfo.mapj.*;
import com.mapinfo.util.DoublePoint;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.beans.*;
import java.io.Serializable;
import java.net.URL;
import java.text.DecimalFormat;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
* The SimpleRulerMapTool interprets mouse actions within VisualMapJ as requests
* to calculate the distance between two points.
*/
public class SimpleRulerMapTool extends AbstractConstrainedAction implements
MapTool, MapMouseListener, KeyListener, MapPainter, ToolTipTextSetter,
Serializable {
protected boolean m_bSelected = false;
protected boolean m_bInitialized = false;
protected Cursor m_cursor = new Cursor(java.awt.Cursor.DEFAULT_CURSOR);
protected Icon m_originalIcon = null;
protected Icon m_selectedIcon = null;
protected Rubberband m_rubberband= new Rubberband();
protected double m_distance = 0.0;
protected DecimalFormat m_numberFormat= new DecimalFormat();
/**
* Creates a SimpleRulerMapTool with a default cursor, icon, and short description.
* Distances are computed in miles using the Cartesian distance formula.
*/
public SimpleRulerMapTool()
{
super();
URL gifURL = null;
/**
* establish the selected/unselected icons that will be displayed
* for the tool button. The gif files used as icon images must
* be found on the classpath
*/
gifURL = getClass().getResource("images/distance.gif");
m_originalIcon = new ImageIcon(gifURL);
gifURL = getClass().getResource("images/distances.gif");
m_selectedIcon = new ImageIcon(gifURL);
putValue(SMALL_ICON, m_originalIcon);
putValue(SHORT_DESCRIPTION, "Ruler");
m_numberFormat.setMaximumFractionDigits(4);
m_numberFormat.setMinimumFractionDigits(1);
}
public void setSelected(boolean bSelected) throws PropertyVetoException
{
// We want to allow a selection to be vetoed by VisualMapJ (or any
// other listener)
fireVetoableChange("Selected", m_bSelected, bSelected);
// if we got here, no veto listeners disagreed with the change
m_bSelected= bSelected;
// Change the icon to visually reflect whether we are selected
if (bSelected)
{
putValue(SMALL_ICON, m_selectedIcon);
}
else
{
putValue(SMALL_ICON, m_originalIcon);
}
firePropertyChange("Selected", new Boolean(!bSelected),
new Boolean(bSelected) );
}
public boolean isSelected()
{
return m_bSelected;
}
public void setCursor(Cursor cursor)
{
m_cursor = cursor;
}
public Cursor getCursor()
{
return m_cursor;
}
/**
* Toggles the selected property of the SimpleRulerMapTool. When the selected
* property is true and the SimpleRulerMapTool is registered with VisualMapJ this
* tool will receive events from VisualMapJ, and can control its behavior.
*/
public void actionPerformed(ActionEvent evt)
{
// toggle the selected state
try
{
setSelected(!isSelected());
}
catch(Exception e)
{
}
}
public void mouseEntered(MapMouseEvent e){}
public void mouseExited(MapMouseEvent e){}
public void mousePressed(MapMouseEvent e) {}
public void mouseReleased(MapMouseEvent e) {}
// This begins a ruler operation
public void mouseClicked(MapMouseEvent e)
{
m_bInitialized = true;
m_rubberband.initializePoints(e.getDoublePoint());
}
// This updates the stretch point of the ruler operation
public void mouseDragged(MapMouseEvent e)
{
handleStretch(e);
}
// This updates the stretch point of the ruler operation
public void mouseMoved(MapMouseEvent e)
{
handleStretch(e);
}
public void paintOnMap(Graphics g)
{
// if we have a valid rubberband when VisualMapJ is repainting,
// draw this on top of the vector data
if (m_rubberband != null && m_bInitialized)
{
m_rubberband.draw(g);
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
// Allow an escape key to abort the ruler operation
// Note that a previously drawn line will be cleared
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
VisualMapJ vj= (VisualMapJ) e.getSource();
cleanup(vj);
vj.repaint(false);
}
}
// The tooltip text should reflect the current distance. This value
// is formatted and then the mile abbreviation is appended. ShowToolTips
// property for VisualMapJ must be set to true for the tooltip to display
public String getToolTipText(MapMouseEvent e)
{
if (m_bInitialized)
{
String distanceString= m_numberFormat.format(m_distance) + " mi";
return distanceString;
}
else
{
return null;
}
}
protected void handleStretch(MapMouseEvent e)
{
VisualMapJ vj= (VisualMapJ) e.getSource();
vj.interruptRenderer();
m_rubberband.setStretchedPt(e.getDoublePoint() );
MapJ mapj= vj.getMapJ();
try
{
// convert the rubberband points to numeric coordinates and then
// calculate the Cartesian distance
DoublePoint anchorPt=
mapj.transformScreenToNumeric(m_rubberband.getAnchorPt());
DoublePoint stretchedPt=
mapj.transformScreenToNumeric(m_rubberband.getStretchedPt());
double distance = mapj.cartesianDistance(anchorPt, stretchedPt);
// convert the distance to miles
m_distance = LinearUnit.mile.convert(distance, mapj.getDistanceUnits());
}
catch(Exception ex)
{
ex.printStackTrace();
}
// notify any listeners that the distance has changes
firePropertyChange("Distance", null, null);
// prompt a repaint on VisualMapJ to update the rubberband line
vj.repaint(false);
}
/**
* Returns the distance of the line segment formed by the
* previous mouse click location and the current mouse position.
*
* @return The distance of the current line segment.
*/
public double getDistance()
{
return m_distance;
}
private void cleanup(VisualMapJ vj)
{
m_distance = 0.0;
m_bInitialized = false;
firePropertyChange("Distance", null, null);
}
//Helper class to handle tracking the start and end points of a ruler
//operation. Also handles drawing the line between the two points.
class Rubberband {
protected DoublePoint m_anchorPt= new DoublePoint();
protected DoublePoint m_stretchedPt= new DoublePoint();
public DoublePoint getAnchorPt()
{
return m_anchorPt;
}
public DoublePoint getStretchedPt()
{
return m_stretchedPt;
}
public void setAnchorPt(DoublePoint pt)
{
m_anchorPt.set(pt);
}
public void setStretchedPt(DoublePoint pt)
{
m_stretchedPt.set(pt);
}
public void initializePoints(DoublePoint pt)
{
m_anchorPt.set(pt);
m_stretchedPt.set(pt);
}
public void draw(Graphics g)
{
Color c= g.getColor();
g.setColor(Color.black);
g.setXORMode(Color.white);
g.drawLine((int)m_anchorPt.x, (int)m_anchorPt.y,
(int)m_stretchedPt.x, (int)m_stretchedPt.y);
// re-set original color and restore to paint mode
g.setColor(c);
g.setPaintMode();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -