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

📄 draw1.java

📁 java写的一交通导航程序点击任意两站点就可以以可视化的方式将转车次数最少的乘车方案找出来 注:连ACCESS数据库 odbc数据源名称为lv
💻 JAVA
字号:
import java.awt.*;
import java.applet.Applet;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.lang.String;
import java.lang.Math.*;

public class draw1 extends JPanel implements Runnable//站点间的可视化显示和fangwen1相对应
            //文件的执行顺序是:先远行jilu,再远行dropbiao,然后远行fangwen1,最后远行draw1
           // 而且draw1和fangwen1的出发站和目的站都要一样
{      	static int pd;//鼠标事件的区分标志
        static float pd1;//放大和缩小的区分标志
        static float scale=1.0f;//放缩比例
 		Image pic;
 		Image pic1;
 		static double k;
 		static String che;
 	   // static double a,a1;//用来求两点之间的斜率
 		BufferedImage buf,buf1,zhand;
 		static int px=0,py=0,x1,y1,x2,y2; 
 		static List mylist,mylist1,mylist2;
 		int width,height;
 		int zd,zd1,zd2,ix,jx,mmm;//mmm是直达和转一次的区分标志
 		int x,y,xx,yy,xxxx,yyyy;
 		int e,w1,hhhhh;
 		double xxx,yyy,movex,movey;  //圆所在的坐标
 		double cd=1;
 		int uu=0;//执行闪动的标志
		BasicStroke bs1=new BasicStroke(3.0f);
		BasicStroke bs2=new BasicStroke(8.0f);
		BasicStroke bs3=new BasicStroke(10.0f);
		static int pwidth=820;
		static int pheight=740;
		
		
		static int ii=0;//显示车次路线和目的路线的区分标志
	    static String syou[]=new String[301];//用来保存所有的站点
	    String u[]=new String[2];
		String v[]=new String[1];
        String w[]=new String[1];
        String z[]=new String[1];
        String c[]=new String[1];
        Thread 	newThread;
        Graphics2D bufg,bufg1,grah;
        ptop ppppp;
        static xmx rect_change;
        static draw1 self;
        Thread tt=null;
        boolean flash=false;
        public void setptop(ptop pp2){
        	flash=false;
        	if(tt!=null) {
        		tt.stop();
        	} 
        	bufg1.drawImage(pic,0,0,null);
        	ppppp=pp2;
        	tt=new Thread(this);

        	tt.start();
        }	
		public draw1()
		{  System.out.println("draw1开始执行了");
			self=this;
		    pic=getToolkit().getImage("tp1.jpg");
			MediaTracker mt=new MediaTracker(this);
			mt.addImage( pic,1);
			try{mt.waitForAll();}catch(Exception e){}
			width=pic.getWidth(this);
			height=pic.getHeight(this);
	
			
			pic1=Toolkit.getDefaultToolkit().getImage("motuo.gif");
			MediaTracker mt1=new MediaTracker(this);
			mt1.addImage( pic1,1);
			try{mt1.waitForAll();}catch(Exception e){}
			w1=pic1.getWidth(null);
			hhhhh=pic1.getHeight(null);
		
			buf=new BufferedImage(2*w1,2*hhhhh,BufferedImage.TYPE_INT_ARGB);
			bufg=buf.createGraphics();
			bufg.drawImage(pic1,w1/2,hhhhh/2,null);  //为什么不要这一句就不行?因为这一句可以放在paint函数中
		
			buf1=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
			bufg1=buf1.createGraphics();
			bufg1.drawImage(pic,0,0,width,height,null);
			bufg1.setColor(new Color(0,0,0,0));
			bufg1.fillRect(0,0,width,height);
			bufg1.drawImage(pic,0,0,null);
			
			zhand=new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);
			grah=zhand.createGraphics();
			newThread=new Thread(this);
			
                 px=py=0;
            this.addMouseListener(new MouseAdapter(){   //处理图象的拖放
            	public void mousePressed(MouseEvent evt){
            	  	x1=evt.getX();
            		y1=evt.getY();
            		
            	}
            	public void mouseReleased(MouseEvent evt){
            		x2=evt.getX();
            		y2=evt.getY();
        	
            		px=px+x2-x1;
            		py=py+y2-y1;
            		if(px>=0)px=0;//控制拖动的范围
            		if((int)(scale*px)<=(pwidth-(int)(width*scale)))px=pwidth-(int)(width*scale);
            		
            		if(py>=0)py=0;
            		if((int)(scale*py)<=(pheight-(int)(height*scale)))py=pheight-(int)(height*scale);
            		
            		if(pd==1)//放大或缩小被按了
            		{
            		  if((x2-x1)<5&&(y2-y1)<5)//点击一个地方
            		  {
            		  	scale=scale*draw1.pd1;
            		  	if(scale*width<pwidth) scale=(float)pwidth/width;
            		  	else if (scale*height<pheight) scale=(float)pheight/height;
            		  	if(scale>2) scale=2;
            		  }
            		  else  { pd=0;}               //拖动鼠标
            		  		
            		}
            	
            		
            		repaint();	
            	}
            	});
 
          }
 
    public void run()//动态的画线
	{   System.out.println("run开始执行了");
	    Font font=new Font("ITALIC",Font.BOLD,12);
	    bufg1.setColor(Color.red);
		xx=ppppp.pp[0].x;yy=ppppp.pp[0].y;
	    int h=1,kkk=0;
	    do//动态的画出路线
		{	x=xx;y=yy;xx=ppppp.pp[h].x;yy=ppppp.pp[h].y;
		    k=(double)((double)(yy-y)/(double)(xx-x));
		    double kh=Math.atan(k);
		    bufg=buf.createGraphics();
		   	bufg.setBackground(new Color(0,0,0,0));
			bufg.clearRect(0,0,2*w1,2*hhhhh);
		    bufg.translate(w1,hhhhh);
		    bufg.rotate(kh);
		    
		    if(xx<x) bufg.drawImage(pic1,w1/2,-hhhhh/2,-w1,hhhhh,this);
		    else bufg.drawImage(pic1,-w1/2,-hhhhh/2,w1,hhhhh,this);
			sanjiao sj=new sanjiao(x,y,xx,yy);

			xxx=x;yyy=y;
			if(x==xx)//垂直的
			{   
				movex=0;xx++;
				if(y<=yy)
				movey=1;
				else
				movey=-1;	
			while (kkk==0)
			{
			try{Thread.sleep(10);}catch(InterruptedException E){}				
			xxx=xxx+movex;
			yyy=yyy+movey;
			xxxx=(int)xxx;
			yyyy=(int)yyy;
			if(yyyy==y||yyyy==yy)kkk=1;
			bufg1.setColor(Color.blue);
			bufg1.fillOval(xxxx,yyyy,4,4);
			repaint();
			}
			}
			else
			{
		    kkk=0;
			movex=cd*sj.cos;
			movey=cd*sj.sin;
		
			while (((x<=xxx)&&(xxx<=xx))||((xx<=xxx)&&(xxx<=x)))
			{
			try{Thread.sleep(10);}catch(InterruptedException E){}				
			xxx=xxx+movex;
			yyy=yyy+movey;
			xxxx=(int)xxx;
			yyyy=(int)yyy;
			bufg1.setColor(Color.blue);
			bufg1.fillOval(xxxx,yyyy,4,4);
			px=-xxxx+pwidth/2;
			py=-yyyy+pheight/2;
			if(px>=0)px=0;//控制拖动的范围
            if((int)(scale*px)<=(pwidth-(int)(width*scale)))px=pwidth-(int)(width*scale);
            if(py>=0)py=0;
            if((int)(scale*py)<=(pheight-(int)(height*scale)))py=pheight-(int)(height*scale);
            System.out.println(""+ppppp.zhongzhuanz);
            if(h==ppppp.zhongzhuanz)
            bufg1.drawString(""+ppppp.pt[h],x,y);
			repaint();
			}
			
			}	
	    	bufg1.setColor(Color.red);
	    	bufg1.setFont(font);
			bufg1.drawString(""+ppppp.pt[h-1],x,y);
			System.out.println("第"+(h-1)+"个站点"+ppppp.pt[h-1]);
			h++;
		}while(newThread!=null&&h<=ppppp.zd);//第一个条件是什么意思?
		repaint();
		bufg1.setFont(font);	
		System.out.println("最后一个站点是"+ppppp.pt[h-1]);
		bufg1.drawString(""+ppppp.pt[h-1],ppppp.pp[h-1].x,ppppp.pp[h-1].y);
    	repaint();
		int i,j=0;	
	    bufg1.setColor(Color.red);
	  
	    bufg1.setFont(font);
	    grah.setBackground(new Color(0,0,0,0));	 
	    grah.setColor(Color.red);
	    System.out.println("run结束了一半");
	    flash=true;
	  	while(true)	
	  	{ 
	      repaint();
	      grah.clearRect(0,0,15,15);
	      if(j==0)
	      {  grah.setColor(Color.green); 
	  		grah.fillOval(3,3,9,9);
		  	j=1;
		  }
		  else
		  {  grah.setColor(Color.red);   
		  	grah.fillOval(0,0,15,15);
	  		j=0;
		  }
	  	  try{Thread.sleep(200);}catch(InterruptedException E){}
	  	}  
	}
	public void update(Graphics kg)	{	paint(kg);}	
	public void paint(Graphics g)
		{   
			g.drawImage(buf1,(int)(scale*px),(int)(scale*py),(int)(scale*width),(int)(scale*height),null);
			//g.drawImage(zhand,(int)(scale*px),(int)(scale*py),(int)(scale*width),(int)(scale*height),null);
			g.drawImage(buf,(int)(scale*(xxxx-w1+px)),(int)(scale*(yyyy-hhhhh+py)),null);
        	if(rect_change.active==true) g.drawImage(rect_change.buf,rect_change.point.x+px-100,rect_change.point.y+py-50,null);
        	if(flash==true){
        		for(int i=0;i<=ppppp.zd;i++){
        			g.drawImage(zhand,(int)(scale*(ppppp.pp[i].x+px-8)),(int)(scale*(ppppp.pp[i].y+py-8)),null);
        		}
        	}
        }
      
   
      public static  void main(String a[])throws Exception
		{   int j=0;
		    Frame myframe=new Frame();
		    myframe.setLayout(null);
			myframe.setSize(1024,768);
			draw1 mydraw=new draw1();
            mydraw.setBounds(200,0,draw1.pwidth,draw1.pheight);
			myframe.add(mydraw);
			
			btnpane btnpanel=new btnpane(mydraw);
		    btnpanel.setBounds(0,30,190,760);
			myframe.add(btnpanel);
			rect_change=new xmx(self,"华子石东",btnpanel.allself);
			//ptop obj=new ptop("华子石东","拱北");
			//mydraw.setptop(obj);

			myframe.show();//显示图形
			
	    }   
}
class btnpane extends JPanel implements ActionListener ,ItemListener{
	Button zoomout;
	Button zoomin;
	Button query;
	Button queryche;
	Button queryzhan;
	CheckboxGroup group=new CheckboxGroup();
	Checkbox start=new Checkbox("选择起点站:",group,true);
	Checkbox end=new Checkbox("选择终点站:",group,false);
	
    //CheckboxGroup group1=new CheckboxGroup();
    Checkbox checi=new Checkbox("公交车次",group,false);
	Checkbox zhandian1=new Checkbox("公交站点",group,false);
	float bl=1.1f;
	List mylist;
	
	static all allself;
	static String nameself;  //保存所选中的站点
	int cc;
	static String checiself;//保存选中的列表筐中的内容
	static String cheself;//保存所选中的车次
	TextField t_begin=new TextField();
	TextField t_end=new TextField();
	TextField t_checi=new TextField();
	TextField t_zhandian1=new TextField();
	
	TextField shuoming=new TextField();
	
	draw1 mydraw;
	ptop ptopobj;
	List chelist;
	btnpane(draw1 draw){
		mydraw=draw;
		mylist=new List();
		all allzhan=new all();
		allself=allzhan;
		for(int i=0;i<allself.num;i++) 	mylist.add(allself.name[i]);
	
		this.setBackground(Color.white);
		this.setLayout(null);
		zoomout=new Button("放大");
		zoomin=new Button("缩小");
		query=new Button("查询");
	    queryche=new Button("查询");
	    queryzhan=new Button("查询"); 
	    
		zoomout.setBounds(10,10,80,25);
		zoomin.setBounds(100,10,80,25);
		start.setBounds(10,40,85,25);
		
		t_begin.setBounds(100,40,90,25);
		end.setBounds(10,70,85,25);
		
		checi.setBounds(5,150,70,25);
		zhandian1.setBounds(5,180,70,25);
		
	
		
		t_end.setBounds(100,70,90,25);
			
		t_checi.setBounds(75,150,80,25);
		t_zhandian1.setBounds(75,180,80,25);
		
		shuoming.setBounds(5,665,190,25);
		query.setBackground(Color.red);
		query.setBounds(30,100,130,40);
		
		queryche.setBackground(Color.green);
		queryche.setBounds(155,150,40,20);
		
		queryzhan.setBackground(Color.orange);
		queryzhan.setBounds(155,180,40,20);
		
		mylist.setBounds(5,210,180,450);
		add(zoomout);
		add(zoomin);
		add(start);
		add(end);
		add(mylist);
		add(t_begin);
		add(t_end);
		add(shuoming);
		add(query);
	    add(checi);
	    add(zhandian1);
	    add(t_checi);
	    add(t_zhandian1);
	    add(queryche);
	    add(queryzhan);
	    
	    //mylist.hide();
	    chelist=new List();
	    chelist.setBounds(5,210,180,450);
	    for(int i=0;i<allself.numc;i++) chelist.add(allself.namecheci[i]);
	
	    add(chelist);
	    chelist.hide();
	    chelist.addActionListener(this);
		query.addActionListener(this);
		queryche.addActionListener(this);
		queryzhan.addActionListener(this);
		zoomout.addActionListener(this);
		zoomin.addActionListener(this);
		mylist.addActionListener(this);
		start.addItemListener(this);
		end.addItemListener(this);
		zhandian1.addItemListener(this);
		checi.addItemListener(this);
		
		
	}
	public void itemStateChanged(ItemEvent e){
		if((e.getSource()==checi)){
			mylist.hide();
			System.out.println("您点中了车次");
			chelist.setVisible(true);
		}
		else{
			chelist.hide();
			mylist.setVisible(true);
		}
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==zoomout){//点中了放大
			draw1.pd1=1.1f;
			draw1.pd=1;
			draw1.self.repaint();
		}
		else if (e.getSource()==zoomin){//点中了缩小
			draw1.pd1=0.9f;
			draw1.pd=1;
			draw1.self.repaint();
		}
		else if(e.getSource()==query){   //点中了路线查询
			ptopobj=new ptop(t_begin.getText(),t_end.getText(),"12",0);
			mydraw.setptop(ptopobj);
			
			//mydraw.repaint();//为什么不要这个repaint()也行啊?
		}
		else if(e.getSource()==queryche)//点中了车次查询
		{
			ptopobj=new ptop(t_begin.getText(),t_end.getText(),""+cheself,1);
			mydraw.setptop(ptopobj);
			//mydraw.repaint();
		}
		
		else if(e.getSource()==mylist){
			nameself=mylist.getSelectedItem();
			if(start.getState()==true) {
				t_begin.setText(nameself);
				end.setState(true);
			}
			else if (end.getState()==true){
				t_end.setText(nameself);
				start.setState(true);
			} 
			else if(zhandian1.getState()==true){
				t_zhandian1.setText(nameself);
			}
			draw1.rect_change=new xmx(draw1.self,nameself,allself);
		}
		
		else if(e.getSource()==chelist){
		  checiself=chelist.getSelectedItem();
		  cc=checiself.indexOf("路");
		  cheself=checiself.substring(0,cc);
			if(checi.getState()==true){
				t_checi.setText(cheself);
			}
		}
	/*if(checi.getState()==true)
		{
		  t_checi.setText(nameself);	
		}
		else
		if(zhandian1.getState()==true)
		{
		  t_zhandian1.setText(nameself);	
		}
		else
		if(e.getSource()==queryche)
		{
			
		}
		else
		if(e.getSource()==queryzhan)
		{
			
		}*/
		
		
	}
}


⌨️ 快捷键说明

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