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

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

/**
 *  Panel to display a file-based image (JPG or GIF).
 *
 *@author     Uli Walther
 *@version    1.0
 */
public class ImagePanel extends JPanel implements Runnable {
	Image mapImg;
	Thread thread;
	Object synchro = new Object();
	boolean loading = false;


	ImagePanel()
	{
		thread = null;
		mapImg = null;
	}


	ImagePanel( String startingUrl ) throws Exception
	{
		thread = null;
		mapImg = null;
		setImage( startingUrl );
	}


	/**
	 *  Set the image file to be displayed.
	 *
	 *@param  image       Image file name.
	 *@throws  Exception  All file exceptions.
	 */
	public void setImage( String image ) throws Exception
	{
		if ( thread != null )
		{
			thread.stop();
		}

		thread = new Thread( this, image );
		thread.start();
	}


	/**
	 *  Main processing method for the ImagePanel object
	 */
	public void run()
	{
		synchronized ( synchro )
		{
			loading = true;
			repaint();

			try
			{
				String image = Thread.currentThread().getName();
				Image newImg;

				/*
				URL u = new URL(image);
				URLConnection c = u.openConnection();
				InputStream in = c.getInputStream();
				FileOutputStream out = new FileOutputStream("_temp_map.gif");
				byte[] buffer = new byte[16384];
				int len;

				while ((len=in.read(buffer))!=-1)
				{
				out.write(buffer,0,len);
				}
				in.close();
				out.close();
				*/
				MediaTracker tracker = new MediaTracker( this );
				if ( image.startsWith( "http:" ) )
				{
					newImg = getToolkit().getImage( new URL( image ) );
				}
				else
				{
					newImg = getToolkit().getImage( image );
				}

				tracker.addImage( newImg, 0 );

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

				/*
				System.out.println("Image loaded!");
				System.out.println(mapImg.getWidth(this)+","+
				mapImg.getHeight(this));
				*/
				mapImg = newImg;
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}

			loading = false;

			imageLoaded();

			repaint();
		}
	}


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

		Graphics2D g = ( Graphics2D ) g1;

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

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

		if ( loading )
		{
			g.setColor( new Color( 0, 0, 255 ) );
			g.drawString( "Retreiving new image...", 10, 10 );
		}
	}


	void imageLoaded() { }
}

⌨️ 快捷键说明

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