?? mapwindow.java
字號:
package com.oyc.mapxtreme.applet;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
* 顯示地圖的畫布
* @author 三峽大學理學院 歐陽超
*
*/
public class MapWindow extends MapCanvas implements MouseListener, MouseMotionListener {
//地圖servlet路徑
private String m_servletName = null;
//工具參數: 放大,縮小,平移,測距離,信息訪問,范圍查詢
private int m_tool = 0;
private final int ZOOM_IN_TOOL = 1;
private final int ZOOM_OUT_TOOL = 2;
private final int PAN_TOOL = 3;
private final int DISTANCE_TOOL = 4;
private final int INFO_TOOL = 5;
private final int SEARCH_INRADIUS_TOOL = 6;
//鼠標拖動起點,終點,拖動寬度,高度
private int dragX1, dragY1, dragX2, dragY2, dragWidth, dragHeight;
//鼠標某次移動的坐標
private int moveX1, moveY1, moveX2, moveY2;
//標記當前鼠標是否在拖動
private boolean m_Dragged = false;
//標記當前鼠標是否單擊過
private boolean m_Clicked = false;
//url變量,每次請求地圖servlet時,重新給它賦值
private String url = null;
//視野變量
private double zoom;
//測距:總距離,測量次數
private int totalDis = 0;
private int disNum = 0;
/**
* 構造器
* @param applet
* @param width 地圖寬度
* @param height 地圖高度
*/
public MapWindow(MainApplet applet, String servletName, int width, int height){
super(applet, width, height);
this.m_servletName = servletName;
//注冊監聽器
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* 設置當前命令工具
* @param tool
*/
public void setCommandTool(int tool){
this.m_tool = tool;
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
if (m_tool==ZOOM_IN_TOOL || m_tool==ZOOM_OUT_TOOL || m_tool==DISTANCE_TOOL || m_tool==SEARCH_INRADIUS_TOOL) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
} else if (m_tool == PAN_TOOL) {
this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
} else if (m_tool == INFO_TOOL) {
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
super.clear(); //更新畫布,去掉標簽
super.repaint();
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if (this.m_tool == 0) { // 如果沒有選擇工具,則不處理
return;
}
this.m_Dragged = false;
this.dragX1 = e.getX();
this.dragY1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if (this.m_tool == 0) { //如果沒有選擇工具,則不處理
return;
}
//放大地圖
if(this.m_tool == ZOOM_IN_TOOL){
if(this.m_Dragged){ //拖動放大
//計算區域中心坐標
int a = dragX1 + dragWidth / 2;
int b = dragY1 + dragHeight / 2;
if(Math.abs(dragWidth) > Math.abs(dragHeight)){
zoom = Math.abs(dragWidth) * MapState.getCurZoom() / super.width;
}else{
zoom = Math.abs(dragHeight) * MapState.getCurZoom() / super.height;
}
url = m_servletName + "?method=zoom";
url += "&ptx=" + a;
url += "&pty=" + b;
url += "&zoom=" + zoom;
this.applet.adjustMapBySelf(url);
}else{ //單擊放大
zoom = MapState.getCurZoom() / 2.0;
url = m_servletName + "?method=zoom";
url += "&ptx=" + dragX1;
url += "&pty=" + dragY1;
url += "&zoom=" + zoom;
this.applet.adjustMapBySelf(url);
}
this.m_Dragged = false; //更改拖動標識狀態
MapState.setCurZoom(zoom); //更新地圖狀態
}
//縮小地圖
if(this.m_tool == ZOOM_OUT_TOOL){
if(this.m_Dragged){ //拖動縮小
//計算區域中心坐標
int a = dragX1 + dragWidth / 2;
int b = dragY1 + dragHeight / 2;
if(Math.abs(dragWidth) > Math.abs(dragHeight)){
zoom = super.height * MapState.getCurZoom() / Math.abs(dragHeight);
}else{
zoom = super.width * MapState.getCurZoom() / Math.abs(dragWidth);
}
url = m_servletName + "?method=zoom";
url += "&ptx=" + a;
url += "&pty=" + b;
url += "&zoom=" + zoom;
this.applet.adjustMapBySelf(url);
}else{ //單擊縮小
zoom = MapState.getCurZoom() * 2.0;
url = m_servletName + "?method=zoom";
url += "&ptx=" + dragX1;
url += "&pty=" + dragY1;
url += "&zoom=" + zoom;
this.applet.adjustMapBySelf(url);
}
this.m_Dragged = false; //更改拖動標識狀態
MapState.setCurZoom(zoom); //更新地圖狀態
}
//平移地圖
if (this.m_tool == PAN_TOOL){
if (this.m_Dragged){ //拖動平移
//計算請求的中心坐標
int a = width/2 - dragWidth;
int b = height/2 - dragHeight;
url = m_servletName + "?method=pan";
url += "&ptx=" + a;
url += "&pty=" + b;
this.applet.adjustMapBySelf(url);
this.m_Dragged = false; //更改拖動標識狀態
}
}
//范圍查詢
if(this.m_tool == SEARCH_INRADIUS_TOOL){
//計算半徑
int w = Math.abs(dragWidth);
int h = Math.abs(dragHeight);
int radius = (int)Math.sqrt(w*w + h*h);
this.applet.invokeJavascript("searchInRadius("+dragX1+","+dragY1+","+radius+")");
}
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if (this.m_tool == 0) { //如果沒有選擇工具,則不處理
return;
}
this.m_Dragged = true; //標識當前鼠標在拖動
//當前坐標及拖動的距離
dragX2 = e.getX();
dragY2 = e.getY();
dragWidth = dragX2 - dragX1;
dragHeight = dragY2 - dragY1;
//放大或縮小
if(this.m_tool == ZOOM_IN_TOOL || this.m_tool == ZOOM_OUT_TOOL){
if(dragHeight >= 0){ //正拖鼠標
if(this.m_tool == ZOOM_IN_TOOL){
super.drawRect(dragX1, dragY1, dragWidth, dragHeight);
}else{
super.drawRect(dragX1, dragY1, dragWidth, dragHeight);
}
}else{ //反拖鼠標
if(this.m_tool == ZOOM_IN_TOOL){
super.drawRect(dragX2, dragY2, Math.abs(dragWidth), Math.abs(dragHeight));
}else{
super.drawRect(dragX2, dragY2, Math.abs(dragWidth), Math.abs(dragHeight));
}
}
}
//平移
if(this.m_tool == PAN_TOOL){
super.panMap(dragWidth, dragHeight);
}
//綜合查詢,畫圓
if(this.m_tool == SEARCH_INRADIUS_TOOL){
int w = Math.abs(dragWidth);
int h = Math.abs(dragHeight);
int radius = (int)Math.sqrt(w*w + h*h);
super.drawOval(dragX1-radius, dragY1-radius, radius);
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(this.m_tool==DISTANCE_TOOL){ //測距
if(e.getClickCount() == 2){ //雙擊時,結束畫直線
this.m_Clicked = false;
this.disNum = 0;
this.totalDis = 0;
super.clear();
return ;
}
if(this.m_Clicked){ //已經單擊過,畫直線
super.drawLine(moveX1, moveY1, moveX2, moveY2, true, true, "測距量", 44, 18);
this.disNum ++;
this.countDistance(moveX1, moveY1, moveX2, moveY2);
}else{ //第一次單擊
this.m_Clicked = true;
}
}
//當前坐標
moveX1 = e.getX();
moveY1 = e.getY();
if(this.m_tool == INFO_TOOL){ //信息訪問
this.applet.invokeJavascript("showInfo("+ moveX1 +","+ moveY1 +")");
}
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//當前坐標
moveX2 = e.getX();
moveY2 = e.getY();
//在鼠標旁邊畫一個標簽,顯示當前狀態
if(this.m_tool == ZOOM_IN_TOOL){
super.drawLabel(moveX2, moveY2, 35, 18, "放大");
}
if(this.m_tool == ZOOM_OUT_TOOL){
super.drawLabel(moveX2, moveY2, 35, 18, "縮小");
}
if(this.m_tool == DISTANCE_TOOL){
super.drawLabel(moveX2, moveY2, 44, 18, "測距離");
}
if(this.m_tool == SEARCH_INRADIUS_TOOL){
super.drawLabel(moveX2, moveY2, 58, 18, "范圍查找");
}
//測距,畫直線
if(this.m_tool==DISTANCE_TOOL && this.m_Clicked){
super.drawLine(moveX1, moveY1, moveX2, moveY2, true, false, "測距量", 44, 18);
}
}
/**
* 測距離
* @param x1
* @param y1
* @param x2
* @param y2
*/
private void countDistance(int x1, int y1, int x2, int y2){
int a = Math.abs(x2 - x1);
int b = Math.abs(y2 - y1);
double c = Math.sqrt(a*a + b*b);
int curDis = (int) ((MapState.getCurZoom() * c / super.width) * 1000);
this.totalDis += curDis;
this.applet.invokeJavascript("countDistance("+ this.disNum +","+ curDis +","+ this.totalDis +")");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -