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