📄 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 + -