⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappanel.java

📁 JavaGPS enables access to GPS devices from any Java application. Provides Java API, NMEA0183 parser,
💻 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;

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.awt.event.*;

/**
 *  Wrapper for map display within class MapView. Can currently display bitmaps
 *  and retrieve shellgeostar.com maps.<br>
 *  It displays the user's position, and the current dilution of precision as an
 *  ellipse. TODO: use map agent for map retrieval.
 *
 *@author    walther
 */
public class MapPanel extends ImagePanel implements KeyListener {
	double lat, lon;
	// user's position
	double horDil, verDil;
	// user's vertical/horizontal dilution of precision
	double mapLat, mapLon, zoom;
	// map's position + zoom
	double loadLat, loadLon, loadZoom;
	// currently loaded map's pos + zoom
	int x, y;
	// current location display in pixels
	Image bitmap;
	Rectangle bitmapRect;
	double transp;
	Rectangle mapRect;


	MapPanel()
	{

		bitmap = this.getToolkit().getImage( Config.getConfigFilename( "hd.jpg" ) );
		bitmapRect = new Rectangle();

		bitmapRect.x = 3477885; bitmapRect.y = 5476060;

//		bitmapRect.x = 3477975; bitmapRect.y = 5475590;

//		bitmapRect.x = 3478050;
//		bitmapRect.y = 5475720;
		bitmapRect.width = 2029;
		bitmapRect.height = 1939;

		transp = 1.0;
		// fit tour from 01 mar 2002
		//[x=3477885,y=5476060,width=2029,height=1939]

		// to fit to GPS coordinates, use this:
		// java.awt.Rectangle[x=3477975,y=5475590,width=2029,height=1939]

		// to fit to our internal mapping, this is correct:
		// java.awt.Rectangle[x=3478050,y=5475720,width=2029,height=1939]
/*
		bitmap = this.getToolkit().getImage( Config.getConfigFilename( "graugruen.jpg" ) );
		bitmapRect = new Rectangle();
		bitmapRect.x = 3466472; bitmapRect.y = 5481444;
		bitmapRect.width = 20000;
		bitmapRect.height = 21000;
// Die Eckkoordinaten lauten:
// (links unten  x1, y1, rechts oben x2 y2)
// 3466472.499714, 5466444.50002, 3486472.500155,5481444.500351
*/

		MediaTracker tracker = new MediaTracker( this );

		tracker.addImage( bitmap, 0 );

		try
		{
			tracker.waitForAll();
		}
		catch ( InterruptedException e )
		{
			System.err.println( "Error: interrupted during loading images" );
		}

		horDil = verDil = 8.0;
		loadLat = loadLon = mapLat = mapLon = lat = lon = 0.0;
		loadZoom = zoom = 5000.0;
	}


	/**
	 *  Set horizontal and vertial dilution of precision for map display (in
	 *  meters).
	 *
	 *@param  horDil  Hor. dilution.
	 *@param  verDil  Ver. dilution.
	 */
	public void setDilution( double horDil, double verDil )
	{
		this.horDil = horDil;
		this.verDil = verDil;
		repaint();
	}


	/**
	 *  Set the display location and zoom factor.
	 *
	 *@param  lat   Latitude of display.
	 *@param  lon   Longitude of display.
	 *@param  zoom  Zoom factor.
	 */
	public 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;
		}

		this.lat = lat;
		this.lon = lon;
		//System.out.println("setLocation "+lat+","+lon+","+zoom);

		XY p = COORD.convertToGaussKrueger( lat, lon );
		XY p2 = COORD.convertToGaussKrueger( loadLat, loadLon );

		if ( getPixels( Math.abs( p.x - p2.x ) ) > ( 50 * getWidth() ) / ( 2 * 100 ) ||
				getPixels( Math.abs( p.y - p2.y ) ) > ( 50 * getHeight() ) / ( 2 * 100 ) ||
				Math.abs( this.zoom - zoom ) > 1 )
		{
			loadZoom = zoom;
			loadLat = lat;
			loadLon = lon;
			try
			{
				String startingUrl = "http://www.vicinity.com/gif?&FAM=shelleurovv&" +
						"CT=" + loadLat + ":" + loadLon + ":" + loadZoom + "&+" +
						"IC=" + loadLat + ":" + loadLon + ":8:&W=512&H=384&DU=KM";

				// MappingJPEG.handler?theme=default&lux=79732578&luy=74823034&rox=80460182&roy=75352200&width=550&height=400&tool=&xpoint=&ypoint=&scale=4999" width="550" height="400" border="0"
				/*
				double m = metersPerPixel();
				Rectangle mRect = new Rectangle();
				mRect.x = (int)(p.x-m*getWidth()/2);
				mRect.y = (int)(p.y+m*getHeight()/2);
				mRect.width = (int)(m*getWidth());
				mRect.height = (int)(m*getHeight());
				startingUrl = "http://193.197.72.6/smallworldweb/stadtplan/"+
							  "MappingJPEG.handler?theme=default&"+
							  "lux="+(mRect.x%100000)+
							  "000&luy="+(mRect.y-mRect.height)%100000+
							  "000&rox="+(mRect.x+mRect.width)%100000+
							  "000&roy="+(mRect.y)%100000+
							  "000&width=512&height=384&tool=&xpoint=&ypoint=&scale=5000";
				System.out.println(startingUrl);
*/
//				setImage( startingUrl );
				imageLoaded();
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}
		repaint();
	}


	/**
	 *  Gets the minimumSize attribute of the MapPanel object
	 *
	 *@return    The minimumSize value
	 */
	public Dimension getMinimumSize()
	{
		return new Dimension( 512, 384 );
	}


	/**
	 *  Gets the preferredSize attribute of the MapPanel object
	 *
	 *@return    The preferredSize value
	 */
	public Dimension getPreferredSize()
	{
		return new Dimension( 512, 384 );
	}


	/**
	 *@param  d
	 *@return
	 */
	public double getPixels( double d )
	{
		return 0.56 * d / zoom * 5000.0;
	}


	/**
	 *  Retrieve the location as pixel coordinates.
	 *
	 *@return    Map pixel coordinates.
	 */
	public Point getLocationPixels()
	{
		return new Point( x, y );
	}


	/**
	 *  Method
	 *
	 *@param  e  Parameter
	 */
	public void keyPressed( KeyEvent e )
	{
		char ch = e.getKeyChar();

		switch ( ch )
		{
			case '8':
				bitmapRect.y += 5;
				break;
			case '2':
				bitmapRect.y -= 5;
				break;
			case '4':
				bitmapRect.x -= 5;
				break;
			case '6':
				bitmapRect.x += 5;
				break;
			case 'w':
				bitmapRect.height -= 5;
				break;
			case 'y':
				bitmapRect.height += 5;
				break;
			case 'a':
				bitmapRect.width -= 5;
				break;
			case 's':
				bitmapRect.width += 5;
				break;
			case '+':
				transp += .1;
				if ( transp > 1.0 )
				{
					transp = 1.0;
				}
				break;
			case '-':
				transp -= .1;
				if ( transp < 0.0 )
				{
					transp = 0.0;
				}
				break;
		}

		if ( new String( "2468wyas+-" ).indexOf( ch ) >= 0 )
		{
			repaint();
			System.out.println( bitmapRect );
		}
	}


	/**
	 *  Method
	 *
	 *@param  e  Parameter
	 */
	public void keyTyped( KeyEvent e ) { }


	/**
	 *  Method
	 *
	 *@param  e  Parameter
	 */
	public void keyReleased( KeyEvent e ) { }


	/**
	 *  Retrieve the current meter to pixel ratio.
	 *
	 *@return    Current meter to pixel ratio.
	 */
	public double metersPerPixel()
	{
		return 1.0 / getPixels( 1.0 );
	}


	/**
	 *  Method
	 *
	 *@param  g1  Parameter
	 */
	public void paintComponent2( Graphics g1 )
	{
		//public void update(Graphics g)

		AlphaComposite ac;
		Graphics2D g = ( Graphics2D ) g1;

		g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
		g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );

		g.setColor( new Color( 100, 100, 100 ) );
		g.fillRect( 0, 0, getWidth(), getHeight() );

		// draw available bitmaps
		if ( mapImg != null )
		{
			g.drawImage( mapImg, 0, 0, getWidth(), getHeight(), this );
		}

		if ( bitmap != null && bitmapRect != null && mapRect != null )
		{
			{
				//if (bitmapRect.intersection( mapRect ).width>0 )

				double x1 = -getPixels( mapRect.x - bitmapRect.x );
				double y1 = getPixels( mapRect.y - bitmapRect.y );

				double w = getPixels( bitmapRect.width );
				double h = getPixels( bitmapRect.height );

				ac = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, ( float ) transp );
				g.setComposite( ac );

				g.drawImage( bitmap, ( int ) x1, ( int ) y1, ( int ) w, ( int ) h, this );
			}
		}

		// draw shaded border around map
		g.setColor( Color.black );
		ac = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f );
		g.setComposite( ac );
		g.setStroke( new BasicStroke( 5.0f ) );
		g.drawRect( 0 + 2, 0 + 2, getWidth() - 5, getHeight() - 5 );

		// draw position pointer
		try
		{
			XY p = COORD.convertToGaussKrueger( lat, lon );
			XY p2 = COORD.convertToGaussKrueger( mapLat, mapLon );
			x = ( int ) getPixels( p.x - p2.x ) + getWidth() / 2;
			y = ( int ) getPixels( p2.y - p.y ) + getHeight() / 2;

			int rx = ( int ) (  /*10.0**/horDil / zoom * 5000.0 ) / 2;
			int ry = ( int ) (  /*10.0**/verDil / zoom * 5000.0 ) / 2;

			if ( y >= 0 && y < getHeight() && x >= 0 && x < getWidth() )
			{
				g.setColor( Color.green );

				ac = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.7f );
				g.setComposite( ac );

				g.fillOval( x - rx / 2, y - ry / 2, rx, ry );

				g.setColor( Color.green );
				g.setStroke( new BasicStroke( 3.0f ) );

				g.drawLine( 0, y, getWidth(), y );
				g.drawLine( x, 0, x, getHeight() );
			}
		}
		// swallow exceptions resulting from illegal lat/lon values
		// (like the initial 0/0 setting...)
		catch ( Exception e )
		{
		}

		if ( loading )
		{
			g.setColor( Color.black );
			g.drawString( "Retreiving new map...", 10, 20 );
		}
	}


	/**
	 *  Method
	 *
	 *@param  g  Parameter
	 */
	public void paintComponent( Graphics g )
	{
		try
		{
			paintComponent2( g );
		}
		catch ( Throwable t )
		{
			System.out.println( "Abnormal error during paint: " + t );
		}
	}


	void imageLoaded()
	{
		mapLat = loadLat;
		mapLon = loadLon;
		zoom = loadZoom;
		XY p = COORD.convertToGaussKrueger( mapLat, mapLon );
		double m = metersPerPixel();

		mapRect = new Rectangle();
		mapRect.x = ( int ) ( p.x - m * getWidth() / 2 );
		mapRect.y = ( int ) ( p.y + m * getHeight() / 2 );
		mapRect.width = ( int ) ( m * getWidth() );
		mapRect.height = ( int ) ( m * getHeight() );

		repaint();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -