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

📄 gumviewer.java

📁 实现了对GUM类型的图像的放大缩小漫游的功能
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import java.lang.*;
import java.awt.image.*;
import javax.swing.filechooser.*;
import java.math.*;
import java.awt.Toolkit;
import java.beans.*;
import java.io.*;

import java.awt.geom.*;import java.awt.font.FontRenderContext;import java.awt.font.TextLayout;
public class GumViewer extends JFrame {
	
	public  Vector            Maps;
   	public  Vector            FileNames;
	public  Vector            FileFullNames;
	public  JList             FileList;
	

	public  PicturePanel      Picture;
	public  JScrollBar        HorizontalScrollBar;
	public  JScrollBar        VerticalScrollBar;
	public  JScrollPane       pictureScrollPane;
	
	public  JToolBar          ToolBar;
	public  JButton           btn_zoomout;
	public  JButton           btn_zoomin;
	public  JButton           btn_open;
	
	public  JButton           btn_show;
	public  JDialog           dlg_setting;
	public  JButton           btn_drag;
	public  JButton           btn_selected;
	public  boolean           isDragged,isSelected;
	public  Color             col_default;
	public  Cursor            cur_drag1,cur_drag2,cur_normal;
	
	public  JPanel		      LeftBar;
	public  JButton           btn_up;
	public  JButton           btn_down;
	public  JButton           btn_top;
	public  JButton           btn_buttom;	
	public  JButton           btn_delete;
	
	public SwingSet2 mainPanel=new SwingSet2(this);
	private BufferedImage  img;
	public  int          width=-1;
	public  int          height=-1;
	public  int []       selected;
	public  int          m_radii=3;
	public  int          m_minDistance=20;
	public  double       m_fontsize=1;
	public  InfoPanel    infoPanel;
	
	//1
	private  boolean[]  smallthreadsetting={true,true,false,false};
	private  JCheckBox[] isShow=new JCheckBox[4];
	private  Color      m_imgBackground=Color.white;
	private  JLabel  jlb_radii=null,jlb_minins=null,jlb_color=null;
	private  JTextField jf_radii=null,jf_minins=null;
	private  boolean   m_isshow=false,m_selectlevel=true;


/*********************************************************************
 *   GumViewer(String title)构造函数 *   用于GumViewer类初始化,并构造和布局窗口元素 *                  title: 窗口说明文字
 ********************************************************************/	
	public GumViewer(String title)
	{
		super(title);
		LeftBar=new JPanel();
		LeftBar.setPreferredSize(new Dimension(30, 300));
		LeftBar.setMinimumSize(new Dimension(30, 300));
		ToolBar = new JToolBar();
		addButtons();
		
	
		Picture = new PicturePanel();
		PicturePanelMouseListener PMouse=new PicturePanelMouseListener();
		Picture.addMouseListener(PMouse);
		Picture.addMouseMotionListener(PMouse);
		Picture.setCursor(cur_normal);
	    pictureScrollPane = new JScrollPane(intro.myintro= new intro());
		pictureScrollPane.validate();
		HorizontalScrollBar = pictureScrollPane.getHorizontalScrollBar();

		HorizontalScrollBar.addMouseListener(new ScrollBarMouseListener());
		HorizontalScrollBar.setValueIsAdjusting(true);
 
		VerticalScrollBar   = pictureScrollPane.getVerticalScrollBar();
		VerticalScrollBar.addMouseListener(new ScrollBarMouseListener());
		VerticalScrollBar.setValueIsAdjusting(true); 
		
		pictureScrollPane.addComponentListener(new java.awt.event.ComponentAdapter() {
         		public void componentResized(ComponentEvent e) {
					Rectangle myrect=Picture.getVisibleRect();
					myrect.width+=20;
					myrect.height+=20;//+20为了防止缩小时滚动条消失的影响
					Picture.setPosition(myrect.x,myrect.y);
					img=null;
					System.gc(); 
					ReDrawMap();
				}
			});
		
		FileNames     = new Vector(10,10);
		FileFullNames = new Vector(10,10);
		Maps          = new Vector(10,10);
       	FileList      = new JList(FileNames);
		
		selected=FileList.getSelectedIndices();
       	FileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		
		infoPanel= new InfoPanel(200,200);
		SmallPanelMouseListener infoMouseListener=new SmallPanelMouseListener();
		infoPanel.addMouseListener(infoMouseListener);
		infoPanel.addMouseMotionListener(infoMouseListener);
		
		JPanel LeftPanel=new JPanel();
		LeftPanel.setLayout(new BorderLayout());
		LeftPanel.add(LeftBar, BorderLayout.WEST);
		LeftPanel.add(FileList,BorderLayout.CENTER);
		JSplitPane splitPaneLeft=new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                 LeftPanel , infoPanel);
		splitPaneLeft.setOneTouchExpandable(true);
		splitPaneLeft.setPreferredSize(new Dimension(200,400));
     	JSplitPane splitPaneMain = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                 splitPaneLeft, pictureScrollPane);
		splitPaneMain.setOneTouchExpandable(true);
 		JPanel showarea = new JPanel();
	   	showarea.setLayout(new BorderLayout());
		showarea.add(ToolBar, BorderLayout.NORTH);
		showarea.add(splitPaneMain, BorderLayout.CENTER);
		
		mainPanel.add(showarea, BorderLayout.CENTER);
		getContentPane().add(mainPanel);

		
	  }
/*********************************************************************
 * ReadOneFile(String filename)   根据文件名将文件读入 *                   filename: 文件名
 *                     返回值: 读入成功返回true,反之false*********************************************************************/	
		public boolean ReadOneFile(String filename)
	{
		try{
			Maps.addElement(new map(filename));
			return true;
		}
		catch (BadFileException e){
			JOptionPane.showMessageDialog(this,e,"Warning",JOptionPane.WARNING_MESSAGE);
			return false;
		}

	}
/********************************************************************* *                   ShowMap()
 *                    无返回值
*********************************************************************/
			public void ShowMap()
	{
		if(!Maps.isEmpty())
		{
			if(!m_isshow){
				m_isshow=true;
				pictureScrollPane.setViewportView(Picture);
			}
			map tempMap=(map)Maps.elementAt(selected[0]);
			height=(int)(infoPanel.n_getWidth()*tempMap.m_upLeft.Y/tempMap.m_lowRight.X);
			infoPanel.n_setHeight(height);
			infoPanel.setMapSize(tempMap.m_lowRight.X,tempMap.m_upLeft.Y);
			DrawSmallThread   ds = new DrawSmallThread();
			ds.start();
			ReDrawMap();
		}
	}
/*********************************************************************
 *                  ReDrawMap()根据不同位置重画当前显示的地图
 *                     无返回值                                    *********************************************************************/

		public void ReDrawMap()
	{
		setLeftButtons(false);
		if(selected.length>0)
		{
			Rectangle myrect=pictureScrollPane.getVisibleRect();
			myrect.x=Picture.getx();
			myrect.y=Picture.gety();
			myrect.width+=20;
			myrect.height+=20;//+20为了防止缩小时滚动条消失的影响
			if(img==null)
				img= new BufferedImage(myrect.width,myrect.height,BufferedImage.TYPE_INT_RGB);
			Graphics2D g= img.createGraphics() ;
			g.setBackground(m_imgBackground);
			g.clearRect(0,0,1024,768);

			for(int p=0;p<selected.length;p++)
			{
				map tempMap=(map)Maps.elementAt(selected[p]);
				//初始化地图显示设置
				int     lineIndex=0;
				int     nodeIndex=0;
				int     polygonIndex=0;

				height=(int)(width*tempMap.m_upLeft.Y/tempMap.m_lowRight.X);	
				
				
				double scale=tempMap.m_lowRight.X/width;
				
				for(int i=0;i<tempMap.V_guPolygon.size();i++)  //画多边形
				{			
					guPolygon tempPolygon=(guPolygon)tempMap.V_guPolygon.elementAt(i);
					if(((tempPolygon.m_rectangle.x1/scale)<myrect.x?(myrect.x+myrect.width-tempPolygon.m_rectangle.x1/scale):(tempPolygon.m_rectangle.x2/scale-myrect.x))<(tempPolygon.m_rectangle.x2-tempPolygon.m_rectangle.x1)/scale+myrect.width && 
					   (((tempMap.m_upLeft.Y-tempPolygon.m_rectangle.y1)/scale<myrect.y)?(myrect.y+myrect.height-(tempMap.m_upLeft.Y-tempPolygon.m_rectangle.y1)/scale):((tempMap.m_upLeft.Y-tempPolygon.m_rectangle.y2)/scale-myrect.y))<(tempPolygon.m_rectangle.y1-tempPolygon.m_rectangle.y2)/scale+myrect.height)
					{
						entityType tempType=new entityType();
						for(int j=0;j<tempMap.V_entityType.size();j++)
						{
							tempType=(entityType)tempMap.V_entityType.elementAt(j);
							if(tempType.m_name.equals(tempPolygon.m_name))
							{
								g.setColor(new Color(tempType.m_geometryDisplay.color));
								break;
							}
						}
						for(int j=0;j<tempPolygon.V_guRing.size();j++)
						{
							Polygon temp=new Polygon();
							guRing tempRing=(guRing)tempPolygon.V_guRing.elementAt(j);
						
					
							
							for(int k=0;k<tempRing.V_coordinate.size();k++)
							{
								coordinate tempCoordinate=(coordinate)tempRing.V_coordinate.elementAt(k);
								temp.addPoint((int)(tempCoordinate.X/scale-myrect.x),(int)((tempMap.m_upLeft.Y-tempCoordinate.Y)/scale-myrect.y));
							}
							g.fillPolygon(temp);
						}
						tempMap.S_guPolygon[polygonIndex++]=i;	
					}
				}//画多边形结束
				tempMap.S_guPolygon[polygonIndex]=-1;  //设置阀门
				
				
				for(int i=0;i<tempMap.V_guLine.size();i++)   //画线
				{
					guLine tempLine=(guLine)tempMap.V_guLine.elementAt(i);
					if(((tempLine.m_rectangle.x1/scale)<myrect.x?(myrect.x+myrect.width-tempLine.m_rectangle.x1/scale):(tempLine.m_rectangle.x2/scale-myrect.x))<(tempLine.m_rectangle.x2-tempLine.m_rectangle.x1)/scale+myrect.width && 
					   (((tempMap.m_upLeft.Y-tempLine.m_rectangle.y1)/scale<myrect.y)?(myrect.y+myrect.height-(tempMap.m_upLeft.Y-tempLine.m_rectangle.y1)/scale):((tempMap.m_upLeft.Y-tempLine.m_rectangle.y2)/scale-myrect.y))<(tempLine.m_rectangle.y1-tempLine.m_rectangle.y2)/scale+myrect.height)
					{
						entityType tempType=new entityType();
						for(int j=0;j<tempMap.V_entityType.size();j++)
						{
							tempType=(entityType)tempMap.V_entityType.elementAt(j);
							if(tempType.m_name.equals(tempLine.m_name))
							{
								g.setColor(new Color(tempType.m_geometryDisplay.color));
								break;
							}
						}
						coordinate from=(coordinate)tempLine.V_coordinate.elementAt(0);
						coordinate to=new coordinate();
						for(int j=1;j<tempLine.V_coordinate.size();j++)
						{
							to=(coordinate)tempLine.V_coordinate.elementAt(j);
							g.drawLine((int)(from.X/scale-myrect.x),(int)((tempMap.m_upLeft.Y-from.Y)/scale-myrect.y),(int)(to.X/scale-myrect.x),(int)((tempMap.m_upLeft.Y-to.Y)/scale-myrect.y));
							from=to;
						}
						tempMap.S_guLine[lineIndex++]=i;
					}
				}//画线结束
				tempMap.S_guLine[lineIndex]=-1;//设置阀门
				
				for(int i=0;i<tempMap.V_guNode.size();i++) //画点
				{	
					guNode tempNode=(guNode)tempMap.V_guNode.elementAt(i);
					
					int radii = m_radii;//(int)(m_radii*width/1000);  //点的半径
					
					if((int)(tempNode.m_coordinate.X/scale)>myrect.x-radii && (int)(tempNode.m_coordinate.X/scale)<myrect.x+myrect.width+radii && (int)((tempMap.m_upLeft.Y-tempNode.m_coordinate.Y)/scale)>myrect.y-radii && (int)((tempMap.m_upLeft.Y-tempNode.m_coordinate.Y)/scale)<myrect.y+myrect.height +radii)
					{   
						entityType tempType=new entityType();
						for(int j=0;j<tempMap.V_entityType.size();j++)
						{
							tempType=(entityType)tempMap.V_entityType.elementAt(j);
							if(tempType.m_name.equals(tempNode.m_name))
							{
								g.setColor(new Color(tempType.m_geometryDisplay.color));
								break;
							}
						}
						g.fillRoundRect((int)(tempNode.m_coordinate.X/scale-myrect.x-radii),(int)((tempMap.m_upLeft.Y-tempNode.m_coordinate.Y)/scale-myrect.y-radii),radii+radii,radii+radii,radii+radii,radii+radii);
						tempMap.S_guNode[nodeIndex++]=i;
					}
				}//画点结束
				tempMap.S_guNode[nodeIndex]=-1;//设置阀门
				

				for(int i=0;i<tempMap.V_guAnnotation.size();i++)//画文字
				{
					// tempAnnotation=new guAnnotation();
					guAnnotation tempAnnotation=(guAnnotation)tempMap.V_guAnnotation.elementAt(i);
					
					entityType tempType=new entityType();
					for(int j=0;j<tempMap.V_entityType.size();j++)
					{
						tempType=(entityType)tempMap.V_entityType.elementAt(j);
						if(tempType.m_name.equals(tempAnnotation.m_name))
						{
							g.setColor(new Color(tempType.m_geometryDisplay.color));
							break;
						}
					}
					
					int f=(int)(tempType.m_annoFont.height*width/8000);
					f=f>12?f:12;
					int x1,y1,x2,y2,yt;
					x1=(int)(tempAnnotation.m_beginNode.X/scale-myrect.x);
					x2=x1+tempAnnotation.m_text.length()*f*4/3;					
					yt=(int)((tempMap.m_upLeft.Y-tempAnnotation.m_beginNode.Y)/scale-myrect.y);
					y1=yt-tempAnnotation.m_text.length()*f;
					y2=yt+tempAnnotation.m_text.length()*f;
						
					if((x1<myrect.width)&& (x2>0) && (y1<myrect.height) && (y2>0))
					{
						g.setFont(new  Font(tempType.m_annoFont.faceName,Font.PLAIN,f));

						if(tempAnnotation.m_annoType==1  )
					
							drawRotateString(x1, yt, tempAnnotation.m_text,g, tempAnnotation.m_angle );

						else 
							g.drawString(tempAnnotation.m_text,x1,yt);
					}
					if(tempAnnotation.m_annoType==3)
					{
						System.out.println("s");
					}
					//System.out.println(i);
				}//画文字结束
			}
			g.dispose();
			img.flush();
			
			Picture.setPreferredSize(new Dimension(width, height));
			Picture.updateUI();
			
			Picture.setImage((Image)img);
			Picture.scrollRectToVisible(myrect);
			Picture.revalidate();
			infoPanel.repaint();
		}
		else
		{
			infoPanel.setImage(null);
			infoPanel.repaint();

⌨️ 快捷键说明

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