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

📄 appframe.java

📁 MAP GIS 一种底层的JAVA程序
💻 JAVA
字号:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class AppFrame extends JFrame
{

	JMenu[] menus = { new JMenu("File"), 	new JMenu("Tools"), new JMenu("About") };
	JMenuItem[] fileMenuItem = {new JMenuItem("Open", KeyEvent.VK_O), new JMenuItem("Exit", KeyEvent.VK_E),};
	JMenuItem[] tools = {new JMenuItem("Pan", KeyEvent.VK_P), new JMenuItem("Zoom In", KeyEvent.VK_Z), new JMenuItem("Zoom Out", KeyEvent.VK_O), new JMenuItem("Reset", KeyEvent.VK_R)};
	JMenuItem[] about = {new JMenuItem("About...", KeyEvent.VK_A),};
	String currentTool;
	RubberbandRectangle rubberbandRect;
	File file;
	JLabel statusBar = new JLabel();
	JMenuBar menuBar = new JMenuBar();
	private IMap map;
	private ZbaMapPanel zp;
	//boolean isAutoScroll = false;
	//
	private Point startPoint  = new Point();
	private Point anchorPt    = new Point(0,0);
	private Point stretchedPt = new Point(0,0);
	private Point lastPt      = new Point(0,0);
	private Point endPt       = new Point(0,0);
	private boolean   firstStretch = true;
	//
	class OpenL implements ActionListener
	{
	    	public void actionPerformed(ActionEvent e)
	    	{
	    		
		      	JFileChooser c = new JFileChooser();
		      	// Demonstrate "Open" dialog:
		      	int rVal =  c.showOpenDialog(AppFrame.this);
		      	if(rVal == JFileChooser.APPROVE_OPTION)
		      	{
		        	file =  c.getSelectedFile();
		        	statusBar.setText(file.toString());

		        	map.loadData(file);
		        	map.reset();
		        	map.reDraw ( null ) ;
		        	Graphics g = zp.getGraphics() ;
		        	zp.update ( g ) ;
		        	g.dispose () ;
		        	currentTool = "Pan";
	      		}
      			if(rVal == JFileChooser.CANCEL_OPTION)
      			{
	      	  		statusBar.setText("Canceled");
      			}
      		
      			map.loadData(new File("WORLD.ZB1"));
		        map.reset();
		        map.reDraw ( null ) ;
		        Graphics g = zp.getGraphics() ;
		        zp.update ( g ) ;
		        g.dispose () ;
		        currentTool = "Pan";
    		}
  	}
  	class ToolsL implements ActionListener
  	{
  		public void actionPerformed( ActionEvent e)
  		{
   			String name = ( (JMenuItem)e.getSource()).getText();
  			statusBar.setText(name);
  			currentTool = name;
  			if(currentTool == "Reset")
  			{
  				map.reset();
  				map.reDraw(null);
  				zp.repaint();
  				currentTool = "Pan";
  				statusBar.setText("漫游");
  			}
  			if( currentTool == "Pan")
  				rubberbandRect.setActive(false);
  			else 	rubberbandRect.setActive(true);
  		}
  	}
  	class ExitAction extends AbstractAction
  	{

		ExitAction() {
	   	 super("exit");
		}

       public void actionPerformed(ActionEvent e) {
	    	System.exit(0);
		}
   	 }
   	public class ZbaMapPanel extends JPanel
   	{

   		public ZbaMapPanel()
   		{
			setBorder(BorderFactory.createEtchedBorder());
			setLayout(new BorderLayout());
   			setBackground(Color.white);
  		}
   		public void paint ( Graphics g )
   		{
    		int width = getWidth () ;
   			int height = getHeight () ;
   			Rectangle clientBound = new Rectangle ( 0, 0, width, height );
   			map.setClientBound ( clientBound ) ;
   			map.drawMap (g, 0, 0 ) ;
   			g.fillRect(2,2,4,4);
  		}
  		public void update(Graphics g)
  		{
   			paint(g);
  		}
	}

    	public class MapMouseListener extends MouseAdapter
    	{
    		public void mousePressed(MouseEvent e)
		{
			startPoint.setLocation(e.getX(), e.getY());
			if (currentTool == "Zoom In")
			{
				statusBar.setText("放大");
			}
			if(currentTool == "Pan")
			{
				statusBar.setText("漫游");
			}
			if (currentTool == "Zoom Out")
			{
				statusBar.setText("缩小");
			}
		}
		public void mouseReleased(MouseEvent e)
		{
			int dx = e.getX() - startPoint.x;
			int dy = e.getY() - startPoint.y;
			Rectangle rect = new Rectangle(startPoint.x, startPoint.y, dx, dy);
			if(currentTool == "Pan")
			{
				map.scroll(dx,dy);
			}
			if(currentTool == "Zoom In")
			{
				map.zoomIn(rect);
				zp.repaint();
			}
			if(currentTool == "Zoom Out")
			{
				map.zoomOut(rect);
				zp.repaint();
			}
		}
		public void mouseEntered(MouseEvent e)
		{
			if(currentTool == "Pan")
			{
				zp.setCursor(new Cursor(Cursor.MOVE_CURSOR));
			}
			if(currentTool == "Zoom In")
			{
				zp.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
			}
			if(currentTool == "Zoom Out")
			{
				zp.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
			}
			System.out.println("Entered");
		}
    	}
    	public class MapMouseMotionListener extends MouseMotionAdapter
    	{
    		public void mouseDragged(MouseEvent e)
    		{
    			if(currentTool == "Pan")
    			{
	    			int dx = e.getX() - startPoint.x;
					int dy = e.getY() - startPoint.y;
					Graphics g = zp.getGraphics();
					if( g != null)
					{
						try
						{
							map.drawMap(g, dx, dy);
						}
						finally
						{
							g.dispose();
						}
					}

				}
    		}
    		public void mouseMoved(MouseEvent e)
    		{
    			/*
    			if(isAutoScroll)
    			{
	    			int x = e.getX();
	    			int y = e.getY();
	    			int width = zp.getWidth();
	    			int height = zp.getHeight();
	    			if( width > 100 && height > 100)
	    			{
		    			if( x < 50 && x > 2)
		    				autoScroll.setXFlag(4);
		    			else if( x < width -3 && x > width - 50)
		    				autoScroll.setXFlag(2);
		    			else
		    				autoScroll.setXFlag(0);

		    			if( y < 50 && y > 2)
		    				autoScroll.setYFlag(1);
		      			else if( y < height - 3 && y > height - 50)
		    				autoScroll.setYFlag(3);
		    			else
		    				autoScroll.setYFlag(0);
		    			System.out.println("-------");
		    			if(autoScroll.getXFlag() != 0 || autoScroll.getYFlag() != 0)
		    				timer.scheduleAtFixedRate(autoScroll, 0, 100);
		    			else
		    				timer.cancel();
	    			}
	    		}
	    		*/

    		}
    	}


  	public AppFrame()
  	{
  		Container cp = getContentPane();
  		cp.setBackground(Color.white);
		setTitle("ZbaMap");
  		setDefaultCloseOperation( EXIT_ON_CLOSE) ;
  		int i;
  		OpenL openL = new OpenL();
  		MapMouseListener mapMouseListener = new MapMouseListener();
  		MapMouseMotionListener mapMouseMotionListener = new MapMouseMotionListener();
  		Action a = new ExitAction();
    		fileMenuItem[1].addActionListener(a);

    		ToolsL tooListener = new ToolsL();
    		for( i = 0; i < tools.length; i++)
  			tools[i].addActionListener(tooListener);
  		//初始化菜单
  		fileMenuItem[0].addActionListener(openL);
  		for( i = 0; i < menus.length; i++)
  			menuBar.add(menus[i]);
   		for( i = 0; i < fileMenuItem.length; i++)
  			menus[0].add(fileMenuItem[i]);
  		for( i = 0; i < tools.length; i++)
  			menus[1].add(tools[i]);
  		for( i = 0; i < about.length; i++)
  			menus[2].add(about[i]);
  		setJMenuBar(menuBar);

  		statusBar.setBorder(BorderFactory.createEtchedBorder());
    		cp.add(BorderLayout.SOUTH,  statusBar);
    		statusBar.setOpaque(true);
 		statusBar.setBorder(BorderFactory.createEtchedBorder());

    		map = new ZbaMap();
    		zp = new ZbaMapPanel();
    		zp.addMouseListener(mapMouseListener);
    		zp.addMouseMotionListener(mapMouseMotionListener);
    		cp.add(BorderLayout.CENTER,  zp);
   		map.setOwnerComponent ( zp );
   		rubberbandRect = new RubberbandRectangle();
  		rubberbandRect.setComponent(zp);
    	}

	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.
			getSystemLookAndFeelClassName());
		}
		catch(Exception e)
		{}
		AppFrame frame = new AppFrame();
		frame.setBackground(Color.black);
		frame.setSize(800, 600);
		frame.show();


	}
}

⌨️ 快捷键说明

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