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

📄 gumviewer.java

📁 实现了对GUM类型的图像的放大缩小漫游的功能
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			Picture.setImage(null);
			Picture.setPreferredSize(new Dimension(0,0));
			Picture.updateUI();
		}
		setLeftButtons(true);

	}
/*********************************************************************
 *select(int x,int y)根据当前坐标确定选中的元素
 *                     无返回值                                       
 *********************************************************************/
		public void select(int x,int y)
	{
		guPolygon   tempInPolygon=null;
		double      polygonUpLeft_Y=0;
		double      polygonSclae=0;
		int         colorPolygon=0;
		int         tempminDistance=m_minDistance+1;
		guLine      tempMinLine=null;
		double      lineUpLeft_Y=0;
		double      lineSclae=0;
		int         colorLine=0;
		
		for(int p=0;p<selected.length;p++)
		{
			map tempMap=(map)Maps.elementAt(selected[p]);
			double scale=tempMap.m_lowRight.X/width;
			for(int index=0;tempMap.S_guPolygon[index]!=-1;index++)
			{
				guPolygon   tempPolygon=(guPolygon)(tempMap.V_guPolygon.elementAt(tempMap.S_guPolygon[index]));
				if(x*scale>tempPolygon.m_rectangle.x1 && x*scale<tempPolygon.m_rectangle.x2 && y*scale>(tempMap.m_upLeft.Y-tempPolygon.m_rectangle.y1)  && y*scale<(tempMap.m_upLeft.Y-tempPolygon.m_rectangle.y2))
				{										
					entityType tempType=null;
					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))
						{
							colorPolygon=0-tempType.m_geometryDisplay.color;
							break;
						}
					}
			
					for(int i=0;i<tempPolygon.m_rings;i++)
					{
						guRing tempRing=(guRing)tempPolygon.V_guRing.elementAt(i);
						Polygon temp=new Polygon();
						for(int j=0;j<tempRing.V_coordinate.size();j++)
						{
							coordinate tempCoordinate=(coordinate)tempRing.V_coordinate.elementAt(j);
							temp.addPoint((int)(tempCoordinate.X/scale),(int)((tempMap.m_upLeft.Y-tempCoordinate.Y)/scale));	
						}
						if(temp.inside(x,y))
						{
							polygonUpLeft_Y=tempMap.m_upLeft.Y;
							polygonSclae=scale;
							tempInPolygon=tempPolygon;							
						}
					}
				}
			}
			if(tempMap.S_guLine[0]!=-1)
			{				
				for(int index=0;tempMap.S_guLine[index]!=-1;index++)
				{
					guLine    tempLine=(guLine)(tempMap.V_guLine.elementAt(tempMap.S_guLine[index]));
					if((x+m_minDistance)*scale>tempLine.m_rectangle.x1 && (x-m_minDistance)*scale<tempLine.m_rectangle.x2 && (y+m_minDistance)*scale>tempMap.m_upLeft.Y-tempLine.m_rectangle.y1  &&(y-m_minDistance)*scale<tempMap.m_upLeft.Y-tempLine.m_rectangle.y2)
					{
						int distance=m_minDistance+1;
						coordinate tempCoordinate=(coordinate)tempLine.V_coordinate.elementAt(0);
						int x1,x2,y1,y2;
						x1=(int)(tempCoordinate.X/scale);
						y1=(int)((tempMap.m_upLeft.Y-tempCoordinate.Y)/scale);
						
						for(int j=1;j<tempLine.V_coordinate.size();j++)
						{
							tempCoordinate=(coordinate)tempLine.V_coordinate.elementAt(j);
							x2=x1;
							y2=y1;
							x1=(int)(tempCoordinate.X/scale);
							y1=(int)((tempMap.m_upLeft.Y-tempCoordinate.Y)/scale);
							int tt=minInstance(x,y,x1, y1,x2, y2);
							distance=distance>tt?tt:distance;
							
						}
						if(tempminDistance>distance)
						{
							tempminDistance=distance;
							tempMinLine=tempLine;
							lineUpLeft_Y=tempMap.m_upLeft.Y;
							lineSclae=scale;
							entityType tempType=null;
							for(int k=0;k<tempMap.V_entityType.size();k++)
							{
								tempType=(entityType)tempMap.V_entityType.elementAt(k);
								if(tempType.m_name.equals(tempMinLine.m_name))
								{
									colorLine=0-tempType.m_geometryDisplay.color;
									break;
								}
							}
						}
					}
						
				}

			
		}
		}
		if(tempminDistance<=m_minDistance &&  m_selectlevel)
		{			
			Graphics g=Picture.getGraphics();
			Picture.repaintImmediately(g);
			g.setColor(new Color(colorLine));
			coordinate from=(coordinate)tempMinLine.V_coordinate.elementAt(0);
			coordinate to=new coordinate();
			for(int j=1;j<tempMinLine.V_coordinate.size();j++)
			{
				to=(coordinate)tempMinLine.V_coordinate.elementAt(j);
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae-1),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae-1),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae+1),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae+1),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae-1),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae+1));
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae-1),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae+1));
				from=to;
			}
			g.setColor(new Color(172,172,255));
			String info=" 长度:"+(int)(tempMinLine.m_length)+"米";
			g.fillRect(x,y,info.length()*9,20);
			g.setColor(Color.white);
			g.drawString(info,x,y+15);
		}
		else if(tempInPolygon!=null)
		{
			Graphics g=Picture.getGraphics();
			Picture.repaintImmediately(g);
			g.setColor(new Color(colorPolygon));
			for(int i=0;i<tempInPolygon.m_rings;i++)
			{
				guRing tempRing=(guRing)tempInPolygon.V_guRing.elementAt(i);
				Polygon temp=new Polygon();
				for(int j=0;j<tempRing.V_coordinate.size();j++)
				{
					coordinate tempCoordinate=(coordinate)tempRing.V_coordinate.elementAt(j);
					temp.addPoint((int)(tempCoordinate.X/polygonSclae),(int)((polygonUpLeft_Y-tempCoordinate.Y)/polygonSclae));	
				}
				g.fillPolygon(temp);
			}
			g.setColor(new Color(172,172,255));
			int tempint=(int)tempInPolygon.m_acreage/100;
			String tempstr=String.valueOf((double)(tempint)/10000);

			String info=" 面积:"+tempstr+"平方公里";
			g.fillRect(x,y,info.length()*9,20);
			g.setColor(Color.white);
			g.drawString(info,x,y+15);	
		}
		else if(tempminDistance<=m_minDistance && !m_selectlevel)
		{			
			Graphics g=Picture.getGraphics();
			Picture.repaintImmediately(g);
			g.setColor(new Color(colorLine));
			coordinate from=(coordinate)tempMinLine.V_coordinate.elementAt(0);
			coordinate to=new coordinate();
			for(int j=1;j<tempMinLine.V_coordinate.size();j++)
			{
				to=(coordinate)tempMinLine.V_coordinate.elementAt(j);
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae-1),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae-1),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae+1),(int)((lineUpLeft_Y-from.Y)/lineSclae),(int)(to.X/lineSclae+1),(int)((lineUpLeft_Y-to.Y)/lineSclae));
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae-1),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae+1));
				g.drawLine((int)(from.X/lineSclae),(int)((lineUpLeft_Y-from.Y)/lineSclae-1),(int)(to.X/lineSclae),(int)((lineUpLeft_Y-to.Y)/lineSclae+1));
				from=to;
			}
			g.setColor(new Color(172,172,255));
			String info=" 长度:"+(int)(tempMinLine.m_length)+"米";
			g.fillRect(x,y,info.length()*9,20);
			g.setColor(Color.white);
			g.drawString(info,x,y+15);
		}
	} 
/*********************************************************************
 *  minInstance(int x0,int y0,int x1,int y1,int x2,int y2)
 *                               计算点到线段的最小距离
 *                   x0,y0: 点的坐标
 *             x1,y1,x2,y2: 线段的端点坐标
 *                  返回值: 点到线段的最小距离
 *********************************************************************/
		private int minInstance(int x0,int y0,int x1,int y1,int x2,int y2)
	{
			if(y2<y1)
			{
				int temp=y2;
				y2=y1;
				y1=temp;
				temp=x1;
				x1=x2;
				x2=temp;				
			}
			
			if((y0-y1)*(y2-y1)<(x2-x1)*(x1-x0))
				return((int)Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)));
			else if((y0-y2)*(y2-y1)<(x2-x1)*(x2-x0))
				return (int)(Math.abs((y0-y1)*(x2-x1)+(y2-y1)*(x1-x0))/Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
			else
				return((int)Math.sqrt((x0-x2)*(x0-x2)+(y0-y2)*(y0-y2)));
				
	}
/*********************************************************************
 *               openFile()打开文件
 *                  无返回值 
 *********************************************************************/
		public void openFile()
	{
			JFileChooser fc = new JFileChooser(".//");
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			fc.setMultiSelectionEnabled(true);
			GumFileFilter ff=new GumFileFilter();
			fc.setFileFilter(ff);
			if(fc.showOpenDialog(GumViewer.this)==JFileChooser.APPROVE_OPTION){
					
				File[] file =fc.getSelectedFiles();
				selected=new int[file.length];
				int k=0;
				for(int i=0;i<file.length;i++)
				{
					if(file[i].isFile())
					{	
						boolean find=false;
						for(int j=0;j<FileFullNames.size();j++)
							if(file[i].getAbsolutePath().equals((String)FileFullNames.elementAt(j)))
							{
								selected[k++]=j;
								find=true;
								break;
							}
						if(!find)
						{
							if(ReadOneFile(file[i].getAbsolutePath()))
							{
								if(file[i].getName().substring(file[i].getName().length()-4,file[i].getName().length()).toLowerCase().equals(".gum"))
									FileNames.addElement(file[i].getName().substring(0,file[i].getName().length()-4));
								else
									FileNames.addElement(file[i].getName());
								FileFullNames.addElement(file[i].getAbsolutePath());
								selected[k++]=FileFullNames.size()-1;
							}
						}
					}
					FileList.updateUI();
				}
				System.arraycopy(selected,0,selected,0,k);
				FileList.setSelectedIndices(selected);
						
			}
	}
/*********************************************************************
 *          LoadImageIcon(String filename)读取图像文件转化为ICON
 *                          返回值: 图像文件相应的ImageIcon
 *********************************************************************/		
	private ImageIcon LoadImageIcon(String filename) {
		String path = "/resources/images/" + filename;
		try{
			return new ImageIcon(getClass().getResource(path)); 
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(this,"读取文件\""+filename+"\"失败",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
			return null;
		}
   	 }
/********************************************************************
 * showSetting() 显示功能设置
 *               无返回值
 ********************************************************************/	
	public void showSetting()
	{
		if(dlg_setting==null)
		{
			dlg_setting=new JDialog(this,"功能设置", false);	
			JTabbedPane tp=new JTabbedPane();
			dlg_setting.getContentPane().add(tp, BorderLayout.CENTER);
			
			JPanel  showSetting = new JPanel();
			showSetting.setLayout(new GridLayout(4,1));
			JButton btn_bkg=new JButton("选择背景颜色");
			btn_bkg.addActionListener(new ActionListener() {
			    public void actionPerformed(ActionEvent e) {
					Color temp=JColorChooser.showDialog(new JDialog(),"地图背景颜色选择",m_imgBackground)  ;
					if(!temp.equals(m_imgBackground))
					{
						m_imgBackground=temp;
						jlb_color.repaint();
						ShowMap();
					}				
				}
			});
			JPanel jpl_color=new JPanel();
			jlb_color=new JLabel(){
				public void paintComponent(Graphics g)
				{
					super.paintComponent(g);
					Rectangle myrect=getVisibleRect();
					g.setColor(m_imgBackground);
					g.fill3DRect(myrect.x+4,myrect.y+4,myrect.width-8,myrect.height-8,true);
				}
			};
			
			jpl_color.setLayout(new GridLayout(1,2));
			jpl_color.add(btn_bkg);
			jpl_color.add(jlb_color);
			showSetting.add(jpl_color);
			
			jlb_radii=new JLabel("点半径(像素):");
			jf_radii = new JTextField(String.valueOf(m_radii));
			JPanel jpl_radii=new JPanel();
			jpl_radii.setLayout(new GridLayout(1,2));
			jpl_radii.add(jlb_radii);
			jpl_radii.add(jf_radii);
			showSetting.add(jpl_radii);
			
			jlb_minins=new JLabel("线条选择精度(像素):");
			jf_minins = new JTextField(String.valueOf(m_minDistance));
			JPanel jpl_minins=new JPanel();
			jpl_minins.setLayout(new GridLayout(1,2));
			jpl_minins.add(jlb_minins);
			jpl_minins.add(jf_minins);
			showSetting.add(jpl_minins);
				
			JButton btn_ok1=new JButton("确定");
			btn_ok1.addActionListener(new ActionListener() {
			    public void actionPerformed(ActionEvent e) {
					try { 
						m_radii = Integer.parseInt(jf_radii.getText().trim())>0?Integer.parseInt(jf_radii.getText().trim()):1;
						m_minDistance = Integer.parseInt(jf_minins.getText().trim())>0?Integer.parseInt(jf_minins.getText().trim()):1;
						ReDrawMap();
					}
				catch (Exception ex){System.out.println(ex);}
			    }
			});
			showSetting.add(btn_ok1);
			
			tp.add("显示设置",showSetting);
			JPanel spSetting=new JPanel();
			spSetting.setLayout(new GridLayout(4,1));
			isShow[0]=new JCheckBox("显示区域",true);
			spSetting.add(isShow[0]);
			isShow[1]=new JCheckBox("显示线条",true);
			spSetting.add(isShow[1]);
			isShow[2]=new JCheckBox("显示点",false);
			spSetting.add(isShow[2]);	
			JButton   btn_ok =new JButton("确定");
			btn_ok.addActionListener(new ActionListener() {
			    public void actionPerformed(ActionEvent e) {
					for(int i=0;i<3;i++)
						smallthreadsetting[i]=isShow[i].isSelected();
					DrawSmallThread   ds = new DrawSmallThread();
					ds.start();
			    }
			});
			spSetting.add(btn_ok);
			ToolBar.add(btn_zoomout);			
			tp.add("小地图设置",spSetting);
		}
		dlg_setting.pack();
		dlg_setting.show();
	}
/*****************************************************************
 * drawRotateString() 在图片上显示旋转的文字方法
 *                      无返回值

⌨️ 快捷键说明

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