Loading...
Searching...
No Matches
LocationWindowCameraExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import com.navigine.idl.java.LocationWindow;
4import com.navigine.idl.java.Camera;
5import com.navigine.idl.java.CameraListener;
6import com.navigine.idl.java.CameraCallback;
7import com.navigine.idl.java.AnimationType;
8import com.navigine.idl.java.CameraUpdateReason;
9import com.navigine.idl.java.Point;
10
11import java.util.ArrayList;
12
19 private LocationWindow locationWindow;
20 private CameraListener cameraListener;
21
23 demonstrateLocationWindowCameraMethods();
24 }
25
29 private void demonstrateLocationWindowCameraMethods() {
30 System.out.println("=== LocationWindowCamera Methods ===");
31
32 demonstrateCameraProperties();
33 demonstrateGetSetCamera();
34 demonstrateCameraListeners();
35 demonstrateFlyToMethod();
36 demonstrateMoveToMethod();
37 demonstrateZoomProperties();
38 demonstrateCameraScenarios();
39 }
40
44 private void demonstrateCameraProperties() {
45 System.out.println("--- Camera Properties Setup ---");
46
47 if (locationWindow == null) {
48 System.out.println("LocationWindow not available yet");
49 return;
50 }
51
52 // [java_LocationWindow_minZoomFactor]
53 // Set minimum zoom factor
54 locationWindow.setMinZoomFactor(10.0f);
55 System.out.println("Set minimum zoom factor to 10.0 pixels per meter");
56 // [java_LocationWindow_minZoomFactor]
57
58 // [java_LocationWindow_maxZoomFactor]
59 // Set maximum zoom factor
60 locationWindow.setMaxZoomFactor(1000.0f);
61 System.out.println("Set maximum zoom factor to 1000.0 pixels per meter");
62 // [java_LocationWindow_maxZoomFactor]
63 }
64
68 private void demonstrateGetSetCamera() {
69 System.out.println("--- getCamera and setCamera Methods ---");
70
71 if (locationWindow == null) {
72 System.out.println("LocationWindow not available yet");
73 return;
74 }
75
76 // [java_LocationWindow_getCamera]
77 // Get current camera position
78 Camera currentCamera = locationWindow.getCamera();
79 System.out.println("Current camera position:");
80 System.out.println(" Point: (" + currentCamera.point.x + ", " + currentCamera.point.y + ")");
81 System.out.println(" Zoom: " + currentCamera.zoom);
82 System.out.println(" Rotation: " + currentCamera.rotation + "°");
83 // [java_LocationWindow_getCamera]
84
85 // [java_Camera_constructor]
86 // Create camera with constructor
87 Point newPoint = new Point(100.0, 200.0);
88 Camera newCamera = new Camera(newPoint, 50.0, 0.0);
89 System.out.println("Created camera with point (" + newPoint.x + ", " + newPoint.y + "), zoom 50.0, rotation 0°");
90 // [java_Camera_constructor]
91
92 // [java_Camera_access]
93 // Access camera properties
94 System.out.println("Camera properties:");
95 System.out.println(" Point: (" + newCamera.point.x + ", " + newCamera.point.y + ")");
96 System.out.println(" Zoom: " + newCamera.zoom);
97 System.out.println(" Rotation: " + newCamera.rotation + "°");
98 // [java_Camera_access]
99
100 // [java_LocationWindow_setCamera]
101 // Set camera position without animation
102 locationWindow.setCamera(newCamera);
103 System.out.println("Set camera position to (" + newPoint.x + ", " + newPoint.y + ") with zoom 50.0 and rotation 0°");
104 // [java_LocationWindow_setCamera]
105
106 // [java_LocationWindow_setCamera_2]
107 // Set camera position with rotation
108 Point rotatedPoint = new Point(150.0, 250.0);
109 Camera rotatedCamera = new Camera(rotatedPoint, 75.0, 45.0);
110 locationWindow.setCamera(rotatedCamera);
111 System.out.println("Set camera position to (" + rotatedPoint.x + ", " + rotatedPoint.y + ") with zoom 75.0 and rotation 45°");
112 // [java_LocationWindow_setCamera_2]
113
114 // Test different camera positions
115 Camera[] testCameras = {
116 new Camera(new Point(0.0, 0.0), 10.0, 0.0), // Overview camera
117 new Camera(new Point(50.0, 75.0), 500.0, 0.0), // Detail camera
118 new Camera(new Point(100.0, 150.0), 100.0, 30.0), // Angled camera
119 };
120
121 for (int i = 0; i < testCameras.length; i++) {
122 Camera camera = testCameras[i];
123 locationWindow.setCamera(camera);
124 System.out.println("Test camera " + i + ": Point (" + camera.point.x + ", " + camera.point.y + "), Zoom " + camera.zoom + ", Rotation " + camera.rotation + "°");
125 }
126 }
127
131 private void demonstrateCameraListeners() {
132 System.out.println("--- Camera Listener Methods ---");
133
134 if (locationWindow == null) {
135 System.out.println("LocationWindow not available yet");
136 return;
137 }
138
139 // [java_LocationWindow_addCameraListener]
140 // Add camera listener
141 cameraListener = new CameraListenerImpl();
142 locationWindow.addCameraListener(cameraListener);
143 System.out.println("Added camera listener");
144 // [java_LocationWindow_addCameraListener]
145
146 // [java_LocationWindow_removeCameraListener]
147 // Remove camera listener
148 locationWindow.removeCameraListener(cameraListener);
149 cameraListener = null;
150 System.out.println("Removed camera listener");
151 // [java_LocationWindow_removeCameraListener]
152
153 // Test multiple listeners
154 CameraListener[] listeners = {
155 new CameraListenerImpl(),
156 new CameraListenerImpl(),
157 new CameraListenerImpl(),
158 };
159
160 for (int i = 0; i < listeners.length; i++) {
161 locationWindow.addCameraListener(listeners[i]);
162 System.out.println("Added camera listener " + i);
163 }
164
165 for (int i = 0; i < listeners.length; i++) {
166 locationWindow.removeCameraListener(listeners[i]);
167 System.out.println("Removed camera listener " + i);
168 }
169 }
170
174 private void demonstrateFlyToMethod() {
175 System.out.println("--- flyTo Method ---");
176
177 if (locationWindow == null) {
178 System.out.println("LocationWindow not available yet");
179 return;
180 }
181
182 // [java_LocationWindow_flyTo]
183 // Fly to position with smooth animation
184 Point targetPoint = new Point(150.0, 250.0);
185 Camera targetCamera = new Camera(targetPoint, 75.0, 45.0);
186 CameraCallback callback = new CameraCallbackImpl();
187 locationWindow.flyTo(targetCamera, 2000, callback);
188 System.out.println("Started fly to animation to point (" + targetPoint.x + ", " + targetPoint.y + ")");
189 // [java_LocationWindow_flyTo]
190
191 // [java_LocationWindow_flyTo_2]
192 // Fly to another position with different duration
193 Point targetPoint2 = new Point(300.0, 400.0);
194 Camera targetCamera2 = new Camera(targetPoint2, 25.0, 180.0);
195 CameraCallback callback2 = new CameraCallbackImpl();
196 locationWindow.flyTo(targetCamera2, 3000, callback2);
197 System.out.println("Started fly to animation to point (" + targetPoint2.x + ", " + targetPoint2.y + ") with 3 second duration");
198 // [java_LocationWindow_flyTo_2]
199
200 // Test fly to with different parameters
201 Object[][] flyToTests = {
202 {new Point(50.0, 50.0), 100.0, 0.0, 1000},
203 {new Point(200.0, 300.0), 200.0, 90.0, 1500},
204 {new Point(400.0, 100.0), 50.0, 270.0, 2500},
205 };
206
207 for (int i = 0; i < flyToTests.length; i++) {
208 Point point = (Point) flyToTests[i][0];
209 double zoom = (Double) flyToTests[i][1];
210 double rotation = (Double) flyToTests[i][2];
211 int duration = (Integer) flyToTests[i][3];
212
213 Camera camera = new Camera(point, zoom, rotation);
214 CameraCallback testCallback = new CameraCallbackImpl();
215 locationWindow.flyTo(camera, duration, testCallback);
216 System.out.println("Fly to test " + i + ": Point (" + point.x + ", " + point.y + "), Zoom " + zoom + ", Rotation " + rotation + "°, Duration " + duration + "ms");
217 }
218 }
219
223 private void demonstrateMoveToMethod() {
224 System.out.println("--- moveTo Method ---");
225
226 if (locationWindow == null) {
227 System.out.println("LocationWindow not available yet");
228 return;
229 }
230
231 // [java_LocationWindow_moveTo]
232 // Move to position with linear animation
233 Point targetPoint = new Point(200.0, 300.0);
234 Camera targetCamera = new Camera(targetPoint, 100.0, 90.0);
235 CameraCallback callback = new CameraCallbackImpl();
236 locationWindow.moveTo(targetCamera, 1500, AnimationType.LINEAR, callback);
237 System.out.println("Started move to with linear animation");
238 // [java_LocationWindow_moveTo]
239
240 // [java_LocationWindow_moveTo_2]
241 // Move to position with cubic animation
242 CameraCallback callback2 = new CameraCallbackImpl();
243 locationWindow.moveTo(targetCamera, 1500, AnimationType.CUBIC, callback2);
244 System.out.println("Started move to with cubic animation");
245 // [java_LocationWindow_moveTo_2]
246
247 // [java_LocationWindow_moveTo_3]
248 // Move to position with sine animation
249 CameraCallback callback3 = new CameraCallbackImpl();
250 locationWindow.moveTo(targetCamera, 1500, AnimationType.SINE, callback3);
251 System.out.println("Started move to with sine animation");
252 // [java_LocationWindow_moveTo_3]
253
254 // [java_LocationWindow_moveTo_4]
255 // Move to position without animation
256 Point instantPoint = new Point(300.0, 400.0);
257 Camera instantCamera = new Camera(instantPoint, 25.0, 180.0);
258 CameraCallback instantCallback = new CameraCallbackImpl();
259 locationWindow.moveTo(instantCamera, 0, AnimationType.NONE, instantCallback);
260 System.out.println("Executed instant move to position (" + instantPoint.x + ", " + instantPoint.y + ")");
261 // [java_LocationWindow_moveTo_4]
262
263 // Test move to with different animation types
264 AnimationType[] animationTypes = {
265 AnimationType.LINEAR,
266 AnimationType.CUBIC,
267 AnimationType.QUINT,
268 AnimationType.SINE,
269 AnimationType.NONE,
270 };
271
272 for (int i = 0; i < animationTypes.length; i++) {
273 AnimationType animationType = animationTypes[i];
274 Point testPoint = new Point(100.0 + i * 50.0, 200.0 + i * 50.0);
275 Camera testCamera = new Camera(testPoint, 50.0 + i * 10.0, i * 30.0);
276 CameraCallback testCallback = new CameraCallbackImpl();
277 locationWindow.moveTo(testCamera, 1000, animationType, testCallback);
278 System.out.println("Move to test " + i + ": Animation " + animationType.name() + ", Point (" + testPoint.x + ", " + testPoint.y + ")");
279 }
280 }
281
285 private void demonstrateZoomProperties() {
286 System.out.println("--- Zoom Properties ---");
287
288 if (locationWindow == null) {
289 System.out.println("LocationWindow not available yet");
290 return;
291 }
292
293 // [java_LocationWindow_getZoomFactor]
294 // Get current zoom factor
295 float currentZoom = locationWindow.getZoomFactor();
296 System.out.println("Current zoom factor: " + currentZoom + " pixels per meter");
297 // [java_LocationWindow_getZoomFactor]
298
299 // [java_LocationWindow_setZoomFactor]
300 // Set new zoom factor
301 locationWindow.setZoomFactor(200.0f);
302 System.out.println("Set zoom factor to 200.0 pixels per meter");
303 // [java_LocationWindow_setZoomFactor]
304
305 // [java_LocationWindow_getMinZoomFactor]
306 // Get minimum zoom factor
307 float minZoom = locationWindow.getMinZoomFactor();
308 System.out.println("Minimum zoom factor: " + minZoom + " pixels per meter");
309 // [java_LocationWindow_getMinZoomFactor]
310
311 // [java_LocationWindow_getMaxZoomFactor]
312 // Get maximum zoom factor
313 float maxZoom = locationWindow.getMaxZoomFactor();
314 System.out.println("Maximum zoom factor: " + maxZoom + " pixels per meter");
315 // [java_LocationWindow_getMaxZoomFactor]
316
317 // Test zoom factor changes
318 float[] zoomLevels = {50.0f, 100.0f, 150.0f, 300.0f, 500.0f, 800.0f};
319
320 for (int i = 0; i < zoomLevels.length; i++) {
321 float zoom = zoomLevels[i];
322 locationWindow.setZoomFactor(zoom);
323 System.out.println("Set zoom factor to " + zoom + " pixels per meter");
324
325 // Verify the change
326 float actualZoom = locationWindow.getZoomFactor();
327 System.out.println("Actual zoom factor: " + actualZoom + " pixels per meter");
328 }
329 }
330
334 private void demonstrateCameraScenarios() {
335 System.out.println("--- Camera Scenarios ---");
336
337 if (locationWindow == null) {
338 System.out.println("LocationWindow not available yet");
339 return;
340 }
341
342 // [java_LocationWindow_camera_scenarios]
343 // Create cameras for different scenarios
344 Camera overviewCamera = new Camera(new Point(0.0, 0.0), 10.0, 0.0);
345 Camera detailCamera = new Camera(new Point(50.0, 75.0), 500.0, 0.0);
346 Camera angledCamera = new Camera(new Point(100.0, 150.0), 100.0, 30.0);
347
348 System.out.println("Created cameras for different scenarios:");
349 System.out.println(" Overview camera: zoom " + overviewCamera.zoom);
350 System.out.println(" Detail camera: zoom " + detailCamera.zoom);
351 System.out.println(" Angled camera: rotation " + angledCamera.rotation + "°");
352 // [java_LocationWindow_camera_scenarios]
353
354 // Test scenario transitions
355 Camera[] scenarios = {overviewCamera, detailCamera, angledCamera};
356 String[] scenarioNames = {"Overview", "Detail", "Angled"};
357
358 for (int i = 0; i < scenarios.length; i++) {
359 Camera scenario = scenarios[i];
360 String name = scenarioNames[i];
361
362 CameraCallback scenarioCallback = new CameraCallbackImpl();
363 locationWindow.moveTo(scenario, 2000, AnimationType.CUBIC, scenarioCallback);
364 System.out.println("Transitioning to " + name + " scenario");
365 }
366 }
367
371 public void cleanup() {
372 if (cameraListener != null && locationWindow != null) {
373 locationWindow.removeCameraListener(cameraListener);
374 cameraListener = null;
375 System.out.println("Camera listener removed");
376 }
377 }
378}
379
383class CameraListenerImpl implements CameraListener {
384 // [java_CameraListener_onCameraPositionChanged]
385 @Override
386 public void onCameraPositionChanged(CameraUpdateReason reason, boolean finished) {
387 String reasonText = (reason == CameraUpdateReason.GESTURES) ? "user gestures" : "application";
388 String statusText = finished ? "finished" : "in progress";
389
390 System.out.println("Camera position changed: " + reasonText + ", status: " + statusText);
391
392 if (finished) {
393 System.out.println("Camera movement completed");
394 }
395 }
396 // [java_CameraListener_onCameraPositionChanged]
397}
398
402class CameraCallbackImpl implements CameraCallback {
403 @Override
404 public void onMoveFinished(boolean completed) {
405 if (completed) {
406 System.out.println("Camera movement completed successfully");
407 } else {
408 System.out.println("Camera movement was cancelled");
409 }
410 }
411}
412
420 public static Camera createCenteredCamera(double x, double y, double zoom) {
421 return new Camera(new Point(x, y), zoom, 0.0);
422 }
423
427 public static Camera createRotatedCamera(double x, double y, double zoom, double rotation) {
428 return new Camera(new Point(x, y), zoom, rotation);
429 }
430
434 public static boolean isValidCamera(Camera camera) {
435 return camera.zoom > 0 &&
436 camera.rotation >= 0 && camera.rotation < 360;
437 }
438
442 public static Camera createAreaCamera(double centerX, double centerY, double width, double height) {
443 // Calculate zoom based on area size
444 double zoom = 100.0 / Math.max(width, height);
445 return new Camera(new Point(centerX, centerY), zoom, 0.0);
446 }
447
451 public static Camera createPathCamera(ArrayList<Point> points) {
452 if (points.isEmpty()) {
453 return new Camera(new Point(0.0, 0.0), 100.0, 0.0);
454 }
455
456 // Find path center
457 double centerX = 0.0, centerY = 0.0;
458 for (Point point : points) {
459 centerX += point.x;
460 centerY += point.y;
461 }
462 centerX /= points.size();
463 centerY /= points.size();
464
465 // Calculate area size
466 double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
467 double maxX = Double.MIN_VALUE, maxY = Double.MIN_VALUE;
468
469 for (Point point : points) {
470 minX = Math.min(minX, point.x);
471 minY = Math.min(minY, point.y);
472 maxX = Math.max(maxX, point.x);
473 maxY = Math.max(maxY, point.y);
474 }
475
476 double width = maxX - minX;
477 double height = maxY - minY;
478
479 return createAreaCamera(centerX, centerY, width, height);
480 }
481}
482
486class Main {
487 public static void main(String[] args) {
488 // Create example instance
490
491 // Assume we have LocationWindow
492 // LocationWindow locationWindow = getLocationWindow();
493 // example.locationWindow = locationWindow;
494
495 // Demonstrate methods
496 System.out.println("=== LocationWindowCamera Examples ===");
497
498 // Cleanup
499 example.cleanup();
500
501 System.out.println("Examples ready to use!");
502 }
503}