zoomouttool.java~139~
来自「一个很有特点的地图平台,可以以动画方试展现电子地图,有拉近,拉远,滑动功能,最主」· JAVA~139~ 代码 · 共 119 行
JAVA~139~
119 行
package mapcenter.maptools;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ZoomOutTool extends MapTool{
private int x1,y1,x2,y2;
private boolean DraggFlag = false;
public ZoomOutTool() {
super("/image/zoomout1.gif","/image/zoomout2.gif","缩小","缩小工具",'O');
ImageIcon icon = new ImageIcon(getClass().getResource("/image/zoomout.gif"));
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(icon.getImage(),new Point(0,0),"缩小");
super.cursor = cursor;
}
public void actionPerformed(ActionEvent evt) {
super.actionPerformed(evt);
}
//============= MouseListener接口实现 =============
public void mouseEntered(MapMouse evt) {}
public void mouseExited(MapMouse evt) {}
public void mouseClicked(MapMouse evt) {}
public void mouseMoved(MapMouse evt) {}
public void mousePressed(MapMouse evt) {
x1 = evt.getX();
y1 = evt.getY();
}
public void mouseDragged(MapMouse evt) {
//======= 绘制轨迹线 ======
Graphics g = mapView.getGraphics();
Color c = g.getColor();
g.setColor(Color.black);
g.setXORMode(Color.white);
if(x2 != 0 && y2 != 0){
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
x2 = evt.getX();
y2 = evt.getY();
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
g.setColor(c);
g.setPaintMode();
DraggFlag = true;
}
public void mouseReleased(MapMouse evt) {
try{
Image renderImage = mapView.getMapHandle().getImg();
int mapWidth = renderImage.getWidth(null);
int mapHeight = renderImage.getHeight(null);
int containW = mapView.getWidth();
int containH = mapView.getHeight();
int marginHor = (mapWidth - containW)/2;
int marginVer = (mapHeight - containH)/2;
double centerX = 0, centerY = 0, zoompen = 0.5;
if(DraggFlag == true){
centerX = x1 + (x2-x1)/2 + marginHor;
centerY = y1 + (y2-y1)/2 + marginVer;
if(Math.abs(x2-x1) > 10)zoompen = Math.abs(x2-x1)/mapView.getWidth();
}else{
centerX = x1 + marginHor;
centerY = y1 + marginVer;
}
zoomMoveing(centerX,centerY,1/zoompen);
mapView.getMapHandle().zoomMap(centerX,centerY,1/zoompen);
mapView.renderMap();
x1=x2=y1=y2=0;
DraggFlag = false;
}catch(Exception e){log.error(e);}
}
//============= KeyListener接口实现 =============
public void keyTyped(KeyEvent evt) {}
public void keyPressed(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) { }
//============= MapPainter接口实现 ===============
public void paintOnMap(Graphics g) {
}
//============= ToolTipTextSetter接口实现 ========
public String getToolTipText(MapMouse evt) {return "缩小";}
public void zoomMoveing(double pointX,double pointY,double zoompen)throws Exception{
Image renderImage = mapView.getMapHandle().getImg();
Image bufferImage = mapView.getBuffImg();
Graphics bufferGrap = bufferImage.getGraphics();
double mapWidth = renderImage.getWidth(null);
double mapHeight = renderImage.getHeight(null);
double containW = mapView.getWidth();
double containH = mapView.getHeight();
double marginHor = (mapWidth - containW) / 2;
double marginVer = (mapHeight - containH) / 2;
int moveCount = 20;
int interval = 30;
double WidthStep = (mapWidth / zoompen - mapWidth) / moveCount;
double HeightStep = (mapHeight / zoompen - mapHeight) / moveCount;
double ScreenX = pointX - marginHor;
double ScreenY = pointY - marginVer;
double HorStep = (mapWidth / 2 - pointX) / moveCount;
double VerStep = (mapHeight / 2 - pointY) / moveCount;
for (int i = 0; i < moveCount; i++) {
Thread.sleep(interval);
ScreenX += HorStep;
ScreenY += VerStep;
mapWidth += WidthStep;
mapHeight += HeightStep;
double left = pointX * (mapWidth/renderImage.getWidth(null)) - ScreenX;
double top = pointY * (mapHeight/renderImage.getHeight(null)) - ScreenY;
bufferGrap.clearRect(0, 0, (int)containW, (int)containH);
bufferGrap.drawImage(renderImage, -(int)left, -(int)top, (int)mapWidth, (int)mapHeight, null);
mapView.getGraphics().drawImage(bufferImage, 0, 0, null);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?