📄 mapview.java
字号:
/***********************************************************************
* J a v a G P S - GPS access library and Java API *
* Copyright (C) 2001 Ulrich Walther *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
* MA 02111-1307 USA *
***********************************************************************/
package org.iu.gps;
/*
* MapView.java
* @author U.Walther
* @changed June 2002
* @changed January 2002
* @changed November 2001
* @changed August 2001
* @changed 21. Juli 2000, 16:53
* Created on 2. Juni 2000, 17:16
*/
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.util.*;
import java.lang.reflect.*;
/**
* WhereAmI map panel display, with the following features: - ability to
* save/load locations - ability log/replay movements from GPS - ability to
* simulate movements
*
*@author walther
*@version 1.1 (18 Dec 2000)
*/
public class MapView extends javax.swing.JFrame
implements Runnable, GPSListener {
static double lat, lon, zoom;
static GPSDriver gpsDriver = null;
static GPSDriver.Configuration gpsConfig = null;
MapPanel mapImg;
DirectionPanel dirPanel;
GPSInfoView gpsInfoView;
boolean followGPS = false;
Thread simulateMovement = null;
// vars for movement simulation:
double simSpeed = 3;
double simX, simY, simDX, simDY;
double simT, simDT;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenu menuFile;
private javax.swing.JMenuItem menuFileLoad;
private javax.swing.JMenuItem menuFileSave;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JMenuItem menuFileExit;
private javax.swing.JMenu menuGPS;
private javax.swing.JMenuItem menuGPSSetup;
private javax.swing.JMenuItem menuLog;
private javax.swing.JMenuItem menuDisableLog;
private javax.swing.JMenuItem menuReplayLog;
private javax.swing.JMenu menuCoord;
private javax.swing.JMenu menuGPSEarthDatum;
private javax.swing.JPanel jPanelMapContainer;
private javax.swing.JPanel jPanelNavigate;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton jButtonN;
private javax.swing.JPanel jPanel3;
private javax.swing.JButton jButtonW;
private javax.swing.JPanel jPanel4;
private javax.swing.JButton jButtonE;
private javax.swing.JPanel jPanel5;
private javax.swing.JButton jButtonS;
private javax.swing.JPanel jPanelZoom;
private javax.swing.JCheckBox checkboxFollowGPS;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JPanel jPanelDirection;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanelLatLong;
private javax.swing.JLabel jLabel2;
private java.awt.TextField textLatitude;
private java.awt.TextField textLongitude;
private javax.swing.JPanel jPanelGaussKrueger;
private javax.swing.JLabel jLabel3;
private java.awt.TextField textGaussX;
private java.awt.TextField textGaussY;
/**
* Creates new form MapView
*
*@exception Exception Exception
*@throws Exception
*/
public MapView() throws Exception
{
/*
String lnfName = UIManager.getLookAndFeel().getClass().getName();
System.out.println(lnfName);
UIManager.setLookAndFeel(
//"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
"javax.swing.plaf.metal.MetalLookAndFeel");
//"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
*/
gpsConfig = new GPSDriver.Configuration();
try
{
Config.loadObject( Config.getConfigFilename( "gps.cfg" ),
gpsConfig );
}
catch ( Exception e )
{
System.out.println( "Warning! *** Cannot read GPS configuration. Detecting settings ***" );
GPSDriver.Configuration cfg;
try
{
cfg = GPSDriver.detect();
}
catch ( java.lang.Throwable e2 )
{
cfg = null;
System.out.println( "Warning! *** Exception in GPS Driver: " + e2 );
}
if ( cfg != null )
{
System.out.println( "Found GPS at " + cfg.port + " baudrate=" + cfg.baudRate );
gpsConfig = cfg;
}
else
{
System.out.println( "Could not detect GPS. Using default settings." );
gpsConfig.port = "COM1";
gpsConfig.baudRate = 4800;
}
}
Config.saveObject( Config.getConfigFilename( "gps.cfg" ),
gpsConfig );
gpsDriver = new GPSDriver();
try
{
gpsDriver.open( gpsConfig );
gpsDriver.addGPSListener( this );
}
catch ( java.lang.Throwable e )
{
System.out.println( "Warning! *** Cannot access GPS :" + e );
System.out.println( "Setup your GPS device correctly. Currently only simulation mode is available" );
}
System.out.println("Setting GPS driver to VERBOSE mode.");
gpsDriver.setVerbose( true );
mapImg = new MapPanel();
addKeyListener( mapImg );
dirPanel = new DirectionPanel();
/* dirPanel.addActionListener( new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
if (gpsDriver==null)
return;
if (gpsDriver.getGPSInfo()==null)
return;
GPSInfo gi = gpsDriver.getGPSInfo();
updateGPSInfo( gi.X, gi.Y );
}
});
*/
initComponents();
jPanelDirection.add( dirPanel );
jPanelMapContainer.add( mapImg );
mapImg.setBounds( jPanelMapContainer.getVisibleRect() );
pack();
setSize( 535 /*+170*/, 560 );
// set initial location
loadPosition( Config.getConfigFilename( "default.gpspos" ) );
// create GPSInfo window
gpsInfoView = new GPSInfoView();
//gpsInfoView.show();
//gpsInfoView.toFront();
// add possible GPS earth dates into menu
JMenu rm = menuGPSEarthDatum;
for ( int i = 0; i < COORD.gDatum.length; i++ )
{
rm.add( new JMenuItem( COORD.gDatum[i].name ) );
if ( ( i % 20 ) == 19 )
{
JMenu m;
rm.add( m = new JMenu( "more..." ) );
rm = m;
}
}
System.out.println("Version Info: MapView "+Long.toHexString(getUID())+", GPSDriver "+
Long.toHexString( ObjectStreamClass.lookup(GPSDriver.class).getSerialVersionUID() ) );
}
//GEN-LAST:event_jButton2ActionPerformed
protected static long getUID() { return ObjectStreamClass.lookup(MapView.class).getSerialVersionUID(); }
public void destroy()
{
try
{
gpsDriver.close();
}
catch (Exception ignored)
{}
gpsDriver = null;
gpsInfoView.setVisible( false );
gpsInfoView = null;
setVisible( false );
//Thread.currentThread().getThreadGroup().stop();
gpsConfig = null;
}
/**
* Stand-alone startup for MapView / GPSManager (without agent functionality).
*
*@param args Parameter
*@exception Exception Exception
*@throws Exception
*/
public static void main( String args[] ) throws Exception
{
new MapView().show();
}
static String doubleToStr( double d, int digits )
{
double f = Math.pow( 10.0, digits );
d = Math.floor( d * f ) / f;
return ( "" + d );
}
/**
* Returns the GPS driver used by MapView.
*
*@return GPSDriver instance.
*/
public GPSDriver getGPSDriver()
{
return gpsDriver;
}
/**
* Gets the gPSInfo attribute of the MapView object
*
*@return The gPSInfo value
*/
public GPSInfo getGPSInfo()
{
if ( followGPS )
{
return gpsDriver.getGPSInfo();
}
else
{
GPSInfo gi = new GPSInfo();
NMEA.infiniteAge( gi );
gi.X = Double.parseDouble( textGaussX.getText() );
gi.Y = Double.parseDouble( textGaussY.getText() );
XY gk = convertToLL( gi.X, gi.Y );
gi.latitude = gk.x;
gi.longitude = gk.y;
gi.latitudeAge = gi.longitudeAge = gi.XAge = gi.YAge = 0;
gi.courseOverGround = dirPanel.getDirection();
gi.courseOverGroundAge = 0;
gi.speedOverGround = 0;
gi.speedOverGroundAge = 0;
gi.GPSModel = "GPS Simulator V0.1";
gi.GPSModelAge = 0;
gi.EPE = gi.HPE = gi.VPE = 10.0;
gi.horDilution = gi.verDilution = 1.0;
gi.EPEAge = gi.HPEAge = gi.VPEAge = 0;
gi.horDilutionAge = gi.verDilutionAge = 0;
return gi;
}
}
/**
* Main processing method for the MapView object
*/
public void run()
{
if ( Thread.currentThread().getName().equals( "GPS" ) )
{
runGPS();
}
else
if ( Thread.currentThread().getName().equals( "SIM" ) )
{
runSimulateMovement();
}
}
/**
* GPS event listener. Retrieves latest GPSInfo from GPS driver.
*
*@param gpsInfo
*/
public void gpsEvent( GPSInfo gpsInfo )
{
System.out.println( "Got GPS Event: "+gpsInfo );
try
{
// display current values in map
if ( gpsInfo != null && gpsInfo.longitudeAge != 9999
&& gpsInfo.longitudeAge != 9999 )
{
setLocation( gpsInfo.latitude,
gpsInfo.longitude, zoom );
}
if ( gpsInfo.HPEAge < 60 )
{
mapImg.setDilution( gpsInfo.HPE, gpsInfo.VPE );
}
else
{
mapImg.setDilution( 100.0, 100.0 );
}
if ( gpsInfo.courseOverGroundAge < 60 )
{
dirPanel.setDirection( gpsInfo.courseOverGround );
}
// display current GPSInfo in InfoPanel
if ( gpsInfo != null )
{
gpsInfoView.setGPSInfo( gpsInfo );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
void setLocation( double lat, double lon, double zoom )
{
if ( Math.abs( this.lat - lat ) < 1e-5 && Math.abs( this.lon - lon ) < 1e-5 &&
Math.abs( this.zoom - zoom ) < 1 )
{
//System.out.println("setLocation: already at that position!");
return;
}
//System.out.println("setLocation "+lat+","+lon+","+zoom);
try
{
this.lat = lat;
this.lon = lon;
this.zoom = zoom;
textLatitude.setText( doubleToStr( lat, 4 ) );
textLongitude.setText( doubleToStr( lon, 4 ) );
XY gk = convertToGK( lat, lon );
textGaussX.setText( doubleToStr( gk.x, 2 ) );
textGaussY.setText( doubleToStr( gk.y, 2 ) );
mapImg.setLocation( lat, lon, zoom );
updateGPSInfo( gk.x, gk.y );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
void runSimulateMovement()
{
XY gk;
// simulate movement
while ( simT <= 1.0 )
{
gk = convertToLL( simX + simT * simDX, simY + simT * simDY );
setLocation( gk.x, gk.y, zoom );
try
{
Thread.currentThread().sleep( 1000 );
}
catch ( Exception e )
{
}
simT += simDT;
if ( simT > 1.0 )
{
simT = 1.0;
}
}
gk = convertToLL( simX + simDX, simY + simDY );
setLocation( gk.x, gk.y, zoom );
simulateMovement = null;
}
void runGPS()
{
GPSInfo gpsInfo;
for ( ; ; )
{
try
{
// get current GPSInfo
gpsInfo = gpsDriver.getGPSInfo();
// display current values in map
if ( gpsInfo != null && gpsInfo.longitudeAge != 9999
&& gpsInfo.longitudeAge != 9999 )
{
setLocation( gpsInfo.latitude,
gpsInfo.longitude, zoom );
}
if ( gpsInfo.HPEAge < 60 )
{
mapImg.setDilution( gpsInfo.HPE, gpsInfo.VPE );
}
else
{
mapImg.setDilution( 100.0, 100.0 );
}
if ( gpsInfo.courseOverGroundAge < 60 )
{
dirPanel.setDirection( gpsInfo.courseOverGround );
}
// display current GPSInfo in InfoPanel
if ( gpsInfo != null )
{
gpsInfoView.setGPSInfo( gpsInfo );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
try
{
Thread.currentThread().sleep( 500 );
}
catch ( Exception e )
{
}
}
}
XY convertToLL( double x, double y )
{
XY gk = COORD.convertToLatLong( x, y );
return COORD.translate( 100, /*28*/100, gk.x, gk.y );
}
XY convertToGK( double lat, double lon )
{
XY gk = COORD.translate( /* 28 */100, 100, lat, lon );
return COORD.convertToGaussKrueger( gk.x, gk.y );
// return COORD.convertToGK( lat, lon );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -