1 |
package skrueger.geotools; |
2 |
|
3 |
import java.awt.Point; |
4 |
import java.awt.Rectangle; |
5 |
import java.awt.event.MouseEvent; |
6 |
import java.awt.event.MouseWheelEvent; |
7 |
import java.awt.geom.Point2D; |
8 |
|
9 |
import org.opengis.geometry.DirectPosition; |
10 |
|
11 |
import schmitzm.geotools.JTSUtil; |
12 |
|
13 |
import com.vividsolutions.jts.geom.Coordinate; |
14 |
import com.vividsolutions.jts.geom.Envelope; |
15 |
|
16 |
public abstract class XMapPaneAction_Zoom implements XMapPaneAction { |
17 |
|
18 |
@Override |
19 |
public void performDragging(XMapPane mapPane, MouseEvent ev, |
20 |
Point dragStartPos, Point dragLastPos, |
21 |
DirectPosition startCoord, DirectPosition endCoord) { |
22 |
|
23 |
if (dragLastPos != null) |
24 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
25 |
dragLastPos); |
26 |
|
27 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev |
28 |
.getPoint()); |
29 |
} |
30 |
|
31 |
public static class In extends XMapPaneAction_Zoom { |
32 |
|
33 |
@Override |
34 |
public void performClick(XMapPane mapPane, MouseEvent ev, |
35 |
DirectPosition coord) { |
36 |
|
37 |
mapPane.zoomTo(ev.getPoint(), 1 / 2.); |
38 |
} |
39 |
|
40 |
@Override |
41 |
public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt, |
42 |
DirectPosition coord) { |
43 |
|
44 |
int units = wheelEvt.getUnitsToScroll(); |
45 |
// Positiver Wert --> Zoom in --> Faktor < 1 |
46 |
// Negativer Wert --> Zoom out --> Faktir > 1 |
47 |
|
48 |
// SK: 9.9.2007 zoom jetzt wie bei GoogleEarth |
49 |
double zFactor = units > 0 ? 1.3 : 1 / 1.3; |
50 |
// vorher double zFactor = units > 0 ? 1/1.2 : 1.2; |
51 |
|
52 |
// Fenster-Koordinaten zu Karten-Koordinaten transformieren |
53 |
Point2D mapCoord = XMapPane.getMapCoordinatesFromEvent(wheelEvt); |
54 |
// Relative Position des Mauszeigers zum Kartenausschnitt |
55 |
// -> Nach Zoom soll dieselbe Kartenposition unterhalb des Mauszeigers |
56 |
// erscheinen, wie vor dem Zoom |
57 |
double relX = (mapCoord.getX() - mapPane.getMapArea().getMinX()) |
58 |
/ mapPane.getMapArea().getWidth(); |
59 |
double relY = (mapCoord.getY() - mapPane.getMapArea().getMinY()) |
60 |
/ mapPane.getMapArea().getHeight(); |
61 |
|
62 |
// Neuen Karten-Ausschnitt berechnen |
63 |
Coordinate ll = new Coordinate(mapCoord.getX() |
64 |
- mapPane.getMapArea().getWidth() * relX * zFactor, mapCoord.getY() |
65 |
- mapPane.getMapArea().getHeight() * relY * zFactor); |
66 |
Coordinate ur = new Coordinate(mapCoord.getX() |
67 |
+ mapPane.getMapArea().getWidth() * (1 - relX) * zFactor, mapCoord |
68 |
.getY() |
69 |
+ mapPane.getMapArea().getHeight() * (1 - relY) * zFactor); |
70 |
mapPane.setMapArea(new Envelope(ll, ur)); |
71 |
} |
72 |
|
73 |
@Override |
74 |
public void performDragged(XMapPane mapPane, MouseEvent ev, |
75 |
Point dragStartPos, Point dragLastPos, |
76 |
DirectPosition startCoord, DirectPosition endCoord) { |
77 |
|
78 |
if (dragLastPos != null) |
79 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
80 |
dragLastPos); |
81 |
|
82 |
// If this is similar to a click, let mouseClicked handle it! |
83 |
if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev |
84 |
.getPoint().y |
85 |
- dragStartPos.y)) < 160) { |
86 |
// performClick(mapPane, ev, coord) |
87 |
return; |
88 |
} |
89 |
|
90 |
final Rectangle bounds = mapPane.getBounds(); |
91 |
|
92 |
Envelope mapArea = mapPane.getMapArea(); |
93 |
|
94 |
// Replace with transform and translate |
95 |
final double mapWidth = mapArea.getWidth(); |
96 |
final double mapHeight = mapArea.getHeight(); |
97 |
|
98 |
final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width) |
99 |
+ mapArea.getMinX(); |
100 |
final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height) |
101 |
+ mapArea.getMinY(); |
102 |
final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width) |
103 |
+ mapArea.getMinX(); |
104 |
final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height) |
105 |
+ mapArea.getMinY(); |
106 |
|
107 |
final double left = Math.min(startX, endX); |
108 |
final double right = Math.max(startX, endX); |
109 |
final double bottom = Math.min(startY, endY); |
110 |
final double top = Math.max(startY, endY); |
111 |
final Coordinate ll = new Coordinate(left, bottom); |
112 |
final Coordinate ur = new Coordinate(right, top); |
113 |
|
114 |
mapPane.setMapArea(new Envelope(ll, ur)); |
115 |
|
116 |
} |
117 |
|
118 |
/** |
119 |
* @param param is ignored |
120 |
*/ |
121 |
@Override |
122 |
public void performKeyboard(XMapPane mapPane, Object param) { |
123 |
mapPane.setMapArea( JTSUtil.expandEnvelope(mapPane.getMapArea(), -.10) ); |
124 |
} |
125 |
|
126 |
} |
127 |
|
128 |
public static class Out extends XMapPaneAction_Zoom { |
129 |
|
130 |
/** |
131 |
* @param param is ignored |
132 |
*/ |
133 |
@Override |
134 |
public void performKeyboard(XMapPane mapPane, Object param) { |
135 |
mapPane.setMapArea( JTSUtil.expandEnvelope(mapPane.getMapArea(), .10) ); |
136 |
} |
137 |
|
138 |
@Override |
139 |
public void performClick(XMapPane mapPane, MouseEvent ev, |
140 |
DirectPosition coord) { |
141 |
|
142 |
mapPane.zoomTo(ev.getPoint(), 2.); |
143 |
} |
144 |
|
145 |
@Override |
146 |
public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt, |
147 |
DirectPosition coord) { |
148 |
int units = wheelEvt.getUnitsToScroll(); |
149 |
// Positiver Wert --> Zoom in --> Faktor < 1 |
150 |
// Negativer Wert --> Zoom out --> Faktir > 1 |
151 |
|
152 |
// SK: 9.9.2007 zoom jetzt wie bei GoogleEarth |
153 |
double zFactor = units > 0 ? 1.3 : 1 / 1.3; |
154 |
// vorher double zFactor = units > 0 ? 1/1.2 : 1.2; |
155 |
|
156 |
// Fenster-Koordinaten zu Karten-Koordinaten transformieren |
157 |
Point2D mapCoord = XMapPane.getMapCoordinatesFromEvent(wheelEvt); |
158 |
// Relative Position des Mauszeigers zum Kartenausschnitt |
159 |
// -> Nach Zoom soll dieselbe Kartenposition unterhalb des Mauszeigers |
160 |
// erscheinen, wie vor dem Zoom |
161 |
double relX = (mapCoord.getX() - mapPane.getMapArea().getMinX()) |
162 |
/ mapPane.getMapArea().getWidth(); |
163 |
double relY = (mapCoord.getY() - mapPane.getMapArea().getMinY()) |
164 |
/ mapPane.getMapArea().getHeight(); |
165 |
|
166 |
// Neuen Karten-Ausschnitt berechnen |
167 |
Coordinate ll = new Coordinate(mapCoord.getX() |
168 |
- mapPane.getMapArea().getWidth() * relX * zFactor, mapCoord.getY() |
169 |
- mapPane.getMapArea().getHeight() * relY * zFactor); |
170 |
Coordinate ur = new Coordinate(mapCoord.getX() |
171 |
+ mapPane.getMapArea().getWidth() * (1 - relX) * zFactor, mapCoord |
172 |
.getY() |
173 |
+ mapPane.getMapArea().getHeight() * (1 - relY) * zFactor); |
174 |
mapPane.setMapArea(new Envelope(ll, ur)); |
175 |
} |
176 |
|
177 |
|
178 |
@Override |
179 |
public void performDragged(XMapPane mapPane, MouseEvent ev, |
180 |
Point dragStartPos, Point dragLastPos, |
181 |
DirectPosition startCoord, DirectPosition endCoord) { |
182 |
|
183 |
if (dragLastPos != null) |
184 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
185 |
dragLastPos); |
186 |
|
187 |
// If this is similar to a click, let mouseClicked handle it! |
188 |
if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev |
189 |
.getPoint().y |
190 |
- dragStartPos.y)) < 160) { |
191 |
// performClick(mapPane, ev, coord) |
192 |
return; |
193 |
} |
194 |
|
195 |
final Rectangle bounds = mapPane.getBounds(); |
196 |
|
197 |
Envelope mapArea = mapPane.getMapArea(); |
198 |
|
199 |
// Replace with transform and translate |
200 |
final double mapWidth = mapArea.getWidth(); |
201 |
final double mapHeight = mapArea.getHeight(); |
202 |
|
203 |
final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width) |
204 |
+ mapArea.getMinX(); |
205 |
final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height) |
206 |
+ mapArea.getMinY(); |
207 |
final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width) |
208 |
+ mapArea.getMinX(); |
209 |
final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height) |
210 |
+ mapArea.getMinY(); |
211 |
|
212 |
// make the dragged rectangle in screen coords the new map size? |
213 |
final double left = Math.min(startX, endX); |
214 |
final double right = Math.max(startX, endX); |
215 |
final double bottom = Math.min(startY, endY); |
216 |
final double top = Math.max(startY, endY); |
217 |
final double nWidth = (mapWidth * mapWidth) / (right - left); |
218 |
final double nHeight = (mapHeight * mapHeight) / (top - bottom); |
219 |
final double deltaX1 = left - mapArea.getMinX(); |
220 |
final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth; |
221 |
final double deltaY1 = bottom - mapArea.getMinY(); |
222 |
final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight; |
223 |
final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1, |
224 |
mapArea.getMinY() - nDeltaY1); |
225 |
final double deltaX2 = mapArea.getMaxX() - right; |
226 |
final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth; |
227 |
final double deltaY2 = mapArea.getMaxY() - top; |
228 |
final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight; |
229 |
final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2, |
230 |
mapArea.getMaxY() + nDeltaY2); |
231 |
|
232 |
mapPane.setMapArea(new Envelope(ll, ur)); |
233 |
|
234 |
} |
235 |
} |
236 |
} |