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

📄 directionpanel.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.*;

// created: 07.07.2000 U.Walther
// last change: 10.07.2000 U.Walther
// This panel realizes a display of the direction
// using a "arrow" polygon

/**
 *  Realizes a (compass-like) round direction display with an arrow and the
 *  display of the direction in degrees.
 *
 *@author     Uli Walther
 *@version    1.0
 */
public class DirectionPanel extends AbstractButton {

	protected double direction;
    protected int id = 0;

	public DirectionPanel()
	{
        super();

		direction = 0.0;

        setModel( new DefaultButtonModel() );

		addMouseListener(
			new java.awt.event.MouseAdapter() {
				public void mouseClicked( java.awt.event.MouseEvent evt )
				{
					mouseClick( evt );
				}
			}
				 );
	}


	/**
	 *  Set the direction in degrees (0..360).
	 *
	 *@param  d  Direction.
	 */
	public void setDirection( double d )
	{
		direction = d;
        repaint();
	}


	/**
	 *  Get the direction in degrees.
	 *
	 *@return    Direction.
	 */
	public double getDirection()
	{
		return direction;
	}


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


	/**
	 *  Method
	 *
	 *@param  evt  Parameter
	 */
	public void mouseClick( java.awt.event.MouseEvent evt )
	{
		if ( this.isEnabled() )
		{
			double x = evt.getX();
			double y = evt.getY();
			x -= getWidth() / 2;
			y -= getHeight() / 2;
			double l = Math.sqrt( x * x + y * y );
			x /= l;
			y /= l;
			//System.out.println("Clicked on X="+x+" Y="+y);

			//if (y!=0.0)
			double deg = Math.toDegrees( Math.atan2( x, -y ) );
			if ( deg < 0.0 )
			{
				deg += 360.0;
			}

			setDirection( deg );
            // fire event to evtl. listeners
            fireActionPerformed( new java.awt.event.ActionEvent(this, id++, "dirPanel" ) );
		}
	}


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

		Graphics2D g = ( Graphics2D ) g1;

		int x = getBounds().x;

		int y = getBounds().y;
		int w = getWidth() - 1;
		int h = getHeight() - 1;

		// I only support square views ;(
		w = Math.min( w, h );
		h = w;

		g.clearRect( 0, 0, w, h );

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

		g.setColor( Color.blue );
		g.fillOval( 0, 0, w, h );
		g.setColor( Color.black );
		g.setStroke( new BasicStroke( 2f ) );
		g.drawOval( 1, 1, w - 2, h - 2 );

		double rad = Math.toRadians( direction + 180 );
		AffineTransform t = AffineTransform.getRotateInstance( rad, w / 2, h / 2 );
		AffineTransform pre = g.getTransform();
		g.transform( t );

		g.setColor( Color.white );
		//        g.drawLine( w/2,h/2,w/2,0 );

		// array polygon definition
		int[] xp = {-10, 10, 5, 20, 0, -20, -5,};
		int[] yp = {0, 0, 80, 80, 100, 80, 80,};

		for ( int i = 0; i < xp.length; i++ )
		{
			xp[i] = h / 2 + xp[i] * h / 210;
			yp[i] = w / 2 + yp[i] * w / 210;
		}

		g.fillPolygon( xp, yp, xp.length );

		g.setTransform( pre );
		{
			g.setColor( Color.yellow );
			g.setFont( new Font( "arial", Font.BOLD, 25 * w / 100 ) );
			String txt = new Integer( ( int ) direction ).toString() + "

⌨️ 快捷键说明

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