简单的map

来源:互联网 发布:linux系统启动不了 编辑:程序博客网 时间:2024/06/14 00:42
import java.awt.BorderLayout;  
import java.awt.Dimension;  
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.event.*;  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
import java.awt.BorderLayout;  
import java.awt.Dimension;  
import java.awt.*;  
import javax.swing.BorderFactory;  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JPanel; 
import javax.swing.UIManager;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.BoxLayout;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.*;  
import javax.swing.event.*;  
import java.io.*;
import java.sql.*;


public class Map5 {  
  
     int xj=2;
int yi=2;
     int p_width = 0;
     int p_height = 0;
     private int pic_x;
     private int pic_y;
   private JPanel pan=null;
 JPanel panel=null;
    //前一个位置
     int begin_x,begin_y;
     boolean inThePic = false;
 public Map5() {  
    
  
      JFrame frame = new JFrame("校园地图");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().setLayout(null);//设置成 null
   pan=new JPanel();
        //pan.setBackground(Color.PINK);
        pan.setLayout(new GridLayout(1,1)); 
        pan.setBorder(BorderFactory.createTitledBorder( "功能目录"));
      pan.setBounds(0,0,150,700);
frame.add(pan);
        System.out.println("开始");
         dataload();
panel = new JPanel() {  //诶panel添加一个背景面板
        protected void paintComponent(Graphics g) {  
        // int p_width = 0;
        // int p_height = 0; 
String path=null;
//设置每次画图的区域为5*5的矩形
   for(int i =yi-2; i <= yi+2; i++)
           {
              for(int j =xj-2; j <=xj+2; j++)
              {
               path = (new StringBuilder()).append("photo\\").append("m").append(i).append("-").append(j).append("").append(".jpg").toString();
              //图片路径
System.out.print("读取图片:");
System.out.println(path);
                ImageIcon icon = new ImageIcon(path);//加载图片 
                Image img = icon.getImage();  
                g.drawImage(img, 256*(j-1),256*(i-1), this);//设置坐标
g.setColor(Color.red);//设置颜色   
  
              g.setFont(new Font("微软简行楷",Font.BOLD,30));//设置字体   
  
              g.drawString("学校东门", 1600, 920);//画文字   
              //g.drawString("学校", 1507, 909);//画文字  
  
   
  }
}
}  
}; 
      
       frame.add(panel);
  panel.setBounds(150,0,1800,2800);
       //panel.setLocation(-500,-500);
     panel.addMouseListener(new MouseAdapter(){  //鼠标动作 监听器 注册           
       /*********鼠标监听********/
  public void mousePressed(MouseEvent e)
               {
                    //检测 落点 是否在图片上,只有落点在图片上时 才起作用
                    if(inPicBounds(e.getX(), e.getY()))
                    {
                        begin_x = e.getX();
                        begin_y = e.getY();
                        inThePic = true;
System.out.println("X="+begin_x+"\t"+"Y="+begin_y); 
//记录当前坐标
xj=(begin_x/256)+1;
                    if(xj==1||xj==2){//当横坐标等于1或2时全部 取 3
xj=3;
}
   else if(xj==7||xj==8){//当横坐标等于7或8时全部 取 6
xj=6;
  }
yi=(begin_y/256)+1;

if(yi==1||yi==2){//当纵坐标等于1或2时全部 取 3
yi=3;
}
else if(yi==10||yi==11){//当纵坐标等于7或8时全部 取 9
yi=9;
}
System.out.println("(X<->J)="+xj+"\t"+"(Y<->I)="+yi);


                    }
                   
                 }
               

    public void mouseReleased(MouseEvent e){
                    inThePic = false;
    }
    public void mouseMoved(MouseEvent e){
    }
    
    public void mouseClicked(MouseEvent e){
    }
    
    public void mouseEntered(MouseEvent e){
    }
    
    public void mouseExited(MouseEvent e){
     
    }
            }
        );
  panel.addMouseMotionListener(new MouseMotionAdapter(){
       /********鼠标监听********/
 public void mouseDragged(MouseEvent e)
                {
            if(inThePic && checkPoint(e.getX(),e.getY()))
                    {System.out.println("判断语句");
                        //边界 检查
                      pic_x =pic_x - (begin_x - e.getX());
                      pic_y =pic_y - (begin_y - e.getY());
                      System.out.print("pic_x 移动=" + pic_x+"\t");
                      System.out.println("pic_y移动=" + pic_y);
                      begin_x = e.getX();
                      begin_y = e.getY();
 panel.setSize(2048,2816);

                      panel.setLocation(pic_x, pic_y);
  //panel.setPreferredSize(new Dimension(2560,3220));
//此句无效果
                    }
                }
            }
        ); 
tree();//调用Jtree方法
        frame.add(panel, BorderLayout.CENTER);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,700);
        frame.setVisible(true);  
    }  
   /*********图片点击********/
 private boolean inPicBounds(int px,int py)
    {
   System.out.println("点击图片");
        //if(px >= pic_x && px <= pic_x + pic.getWidth() &&
                           // py >= pic_y && py <= pic_y + pic.getHeight())
           return true;
        //else
           // return false;
 
    }
    
    
    /*********越界检查********/
 private boolean checkPoint(int px, int py)
    {
   System.out.println("检查落点");
        if(px <0 || py <0)
            return false;
        if(px >2560 || py > 3220)
            return false;
            return true; 
    }
/********JTree事件********/
public void tree(){
      JTree tree;  
    //定义几个初始节点  
    DefaultMutableTreeNode school = new DefaultMutableTreeNode("黄山学院");   
    DefaultMutableTreeNode Tang = new DefaultMutableTreeNode("食堂");
DefaultMutableTreeNode Sleep = new DefaultMutableTreeNode("学生宿舍");
    DefaultMutableTreeNode Bud = new DefaultMutableTreeNode("教学楼"); 
DefaultMutableTreeNode Gund = new DefaultMutableTreeNode("体育场地"); 
DefaultMutableTreeNode Lib = new DefaultMutableTreeNode("图书馆");
    DefaultMutableTreeNode Oth = new DefaultMutableTreeNode("其他");
    
DefaultMutableTreeNode Tone = new DefaultMutableTreeNode("一食堂");  
    DefaultMutableTreeNode Ttwo = new DefaultMutableTreeNode("二食堂");  
    DefaultMutableTreeNode Bone = new DefaultMutableTreeNode("一教");  
    DefaultMutableTreeNode Btwo = new DefaultMutableTreeNode("二教"); 
    DefaultMutableTreeNode Bthree = new DefaultMutableTreeNode("四教");
    DefaultMutableTreeNode Bfour = new DefaultMutableTreeNode("外语楼");
 
    DefaultMutableTreeNode Bsix= new DefaultMutableTreeNode("化工楼");
DefaultMutableTreeNode B7= new DefaultMutableTreeNode("艺术学院");
    DefaultMutableTreeNode g1= new DefaultMutableTreeNode("篮球场");
    DefaultMutableTreeNode g2= new DefaultMutableTreeNode("塑胶跑道");
DefaultMutableTreeNode g3 = new DefaultMutableTreeNode("足球操场");
DefaultMutableTreeNode g4 = new DefaultMutableTreeNode("羽毛球");
DefaultMutableTreeNode g5 = new DefaultMutableTreeNode("网球场");
DefaultMutableTreeNode g6 = new DefaultMutableTreeNode("排球场");

for(int i = 1; i <= 28; i++) {
            DefaultMutableTreeNode s = new DefaultMutableTreeNode((new StringBuilder()).append(i).append("栋").toString());
            Sleep.add(s);
        }
    
    DefaultMutableTreeNode o1 = new DefaultMutableTreeNode("大学生活动中心");
DefaultMutableTreeNode o2 = new DefaultMutableTreeNode("校医院");
DefaultMutableTreeNode o3 = new DefaultMutableTreeNode("广场");
DefaultMutableTreeNode o4 = new DefaultMutableTreeNode("听松湖");
DefaultMutableTreeNode o5 = new DefaultMutableTreeNode("学校东门");


 Tang.add(Tone);  
      Tang.add(Ttwo);  
      Bud.add(Bone);  
      Bud.add(Btwo); 
      Bud.add(Bthree);
 Bud.add(Bfour);
 Bud.add(Bsix);
 Bud.add(B7);//添加到Bud
   Gund.add(g1);
Gund.add(g2);
Gund.add(g3);
Gund.add(g4);
Gund.add(g5);
   Gund.add(g6);//添加到Gund
    
   Oth.add(o1);
   Oth.add(o2);
   Oth.add(o3);
   Oth.add(o4);
   Oth.add(o5);//添加到Oth
        school.add(Tang);  
        school.add(Bud); 
   school.add(Sleep);
school.add(Gund);
        school.add(Lib);
school.add(Oth);//添加到school
        tree = new JTree(school);   //以根节点创建树  
        tree.setShowsRootHandles(true);  
        tree.setRootVisible(true);    //设置节点是否可见,默认是true 
        DefaultTreeCellRenderer render = new DefaultTreeCellRenderer(); // 树叶
render.setTextSelectionColor(Color.red); // 选择时候的颜色
        render.setTextNonSelectionColor(Color.black); // 非选择时候颜色
        tree.setCellRenderer(render);
        pan.add(new JScrollPane(tree));
 tree.addTreeSelectionListener(new TreeSelectionListener()  
        {  
            //当JTree中被选择节点发生改变时,将触发该方法  
            public void valueChanged(TreeSelectionEvent e)  
            {  
                
            if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 食堂, 一食堂]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 1栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 2栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 3栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 4栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 5栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 6栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 7栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 8栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 9栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 10栋]")
){
  System.out.println("成功选中");


          panel.setLocation(540-1413,360-1647);
     xj=6;//设置要显示的坐标区域
     yi=6;
  }
else if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 篮球场]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 排球场]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 羽毛球场]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 网球场]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 塑胶跑道]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 其他, 校医院]")
   ||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 体育场地, 足球操场]")
){
        System.out.println("成功选中");
            panel.setLocation(540-842,360-1765);
       xj=4;
       yi=6;
  }
         else if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 11栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 12栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 13栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 14栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 15栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 16栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 17栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 18栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 19栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 20栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 21栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 22栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 23栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 24栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 25栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 26栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 27栋]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 学生宿舍, 28栋]")
){
 System.out.println("成功选中");
           panel.setLocation(540-917,360-2249);
      xj=3;
      yi=8;
  }
 else if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 一教]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 图书馆]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 艺术学院]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 化工楼]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 外语楼]")
   ||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 其他, 广场]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 其他, 学校东门]")
  ||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 其他, 听松湖]")
){
           System.out.println("成功选中");
           panel.setLocation(540-1152,360-1122);
      xj=6;
      yi=6;
  }
    else if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 食堂, 二食堂]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 其他, 大学生活动中心]")

){
           System.out.println("成功选中");
            panel.setLocation(540-1353,360-2179);
       xj=6;
       yi=8;
  }
    else if(e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 二教]")
||e.getNewLeadSelectionPath().toString().equals("[黄山学院, 教学楼, 四教]")

){                            
           System.out.println("成功选中");
           panel.setLocation(540-452,360-1462);
      xj=3;
      yi=6;
  }
 
            }  
        });   
}




/******从数据库下载图片到本地目录photo******/
 public void dataload(){
try{
   String dbURL = "jdbc:mysql://192.168.**.**:3306/photoserver"; 
   String userID = "root"; 
   String passwd = "pass"; 
File f = new File(".\\"+File.separator+"photo") ;// 实例化File类的对象
     f.mkdir() ; // 创建文件夹
 Class.forName("com.mysql.jdbc.Driver"); 
 Connection conn=(Connection) DriverManager.getConnection(dbURL, userID, passwd); 
 conn.setAutoCommit(false); 


 String sql2 = "SELECT image,name FROM image "; 
 PreparedStatement stmt2 = conn.prepareStatement(sql2); 
 ResultSet resultSet = stmt2.executeQuery(); 
 while (resultSet.next()) { 
 String name = resultSet.getString(2); 
 String description = resultSet.getString(1); 
 File image2 = new File("photo/" + name); 
 FileOutputStream fos = new FileOutputStream(image2); 
 byte[] buffer = new byte[1]; 
 InputStream is = resultSet.getBinaryStream(1); /*read(byte[] b),从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。write(byte[] b),
将 b.length 个字节从指定的 byte 数组写入此输出流。 */
 System.out.println("正在读取图片:"+name+"。。。。。");
 
 while (is.read(buffer) > 0) { 
 fos.write(buffer); 
 } 
 fos.close(); 
 } 
conn.close(); 
  } catch (Exception ex) { 
  ex.printStackTrace();
                      }    
}




       /** public static void main(String[] args) {  
        Panmap test = new Panmap();  
          } **/
/********main方法********/
public static void main(String[] args) throws Exception{
 try {
           UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Map5();
           // System.out.println("2");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
 
 } //此处main结束


}