/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/XMapPaneAction_Zoom.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/geotools/XMapPaneAction_Zoom.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 649 - (hide annotations)
Thu Jan 28 17:24:15 2010 UTC (15 years, 1 month ago) by alfonx
File MIME type: text/plain
File size: 5667 byte(s)


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    
8     import org.opengis.geometry.DirectPosition;
9    
10 alfonx 644 import com.vividsolutions.jts.geom.Coordinate;
11     import com.vividsolutions.jts.geom.Envelope;
12    
13 alfonx 646 public abstract class XMapPaneAction_Zoom implements XMapPaneAction {
14 alfonx 644
15 alfonx 649 @Override
16     public void performDragging(XMapPane mapPane, MouseEvent ev,
17     Point dragStartPos, Point dragLastPos,
18     DirectPosition startCoord, DirectPosition endCoord) {
19    
20     if (dragLastPos != null)
21     mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos,
22     dragLastPos);
23    
24     mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev
25     .getPoint());
26     }
27    
28 alfonx 648 public static class In extends XMapPaneAction_Zoom {
29 alfonx 646
30 alfonx 648 @Override
31     public void performClick(XMapPane mapPane, MouseEvent ev,
32     DirectPosition coord) {
33 alfonx 644
34 alfonx 648 mapPane.zoomTo(ev.getPoint(), 1 / 2.);
35     }
36 alfonx 639
37 alfonx 648 @Override
38     public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt,
39     DirectPosition coord) {
40 alfonx 639
41 alfonx 648 final int units = wheelEvt.getUnitsToScroll();
42     if (units > 0)
43     mapPane.zoomTo(wheelEvt.getPoint(), 1. + .11 * units);
44     else
45     mapPane.zoomTo(wheelEvt.getPoint(), 2. / -units);
46     }
47 alfonx 639
48 alfonx 648 @Override
49     public void performDragged(XMapPane mapPane, MouseEvent ev,
50     Point dragStartPos, Point dragLastPos,
51     DirectPosition startCoord, DirectPosition endCoord) {
52    
53     if (dragLastPos != null)
54     mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos,
55     dragLastPos);
56    
57     // If this is similar to a click, let mouseClicked handle it!
58     if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev
59     .getPoint().y
60     - dragStartPos.y)) < 160) {
61     // performClick(mapPane, ev, coord)
62     return;
63     }
64    
65     final Rectangle bounds = mapPane.getBounds();
66    
67     Envelope mapArea = mapPane.getMapArea();
68    
69     // Replace with transform and translate
70     final double mapWidth = mapArea.getWidth();
71     final double mapHeight = mapArea.getHeight();
72    
73     final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width)
74     + mapArea.getMinX();
75     final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height)
76     + mapArea.getMinY();
77     final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width)
78     + mapArea.getMinX();
79     final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height)
80     + mapArea.getMinY();
81    
82     final double left = Math.min(startX, endX);
83     final double right = Math.max(startX, endX);
84     final double bottom = Math.min(startY, endY);
85     final double top = Math.max(startY, endY);
86     final Coordinate ll = new Coordinate(left, bottom);
87     final Coordinate ur = new Coordinate(right, top);
88    
89     mapPane.setMapArea(new Envelope(ll, ur));
90    
91     }
92 alfonx 644 }
93    
94 alfonx 648 public static class Out extends XMapPaneAction_Zoom {
95 alfonx 639
96 alfonx 648 @Override
97     public void performClick(XMapPane mapPane, MouseEvent ev,
98     DirectPosition coord) {
99 alfonx 644
100 alfonx 648 mapPane.zoomTo(ev.getPoint(), 2.);
101 alfonx 644 }
102    
103 alfonx 648 @Override
104     public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt,
105     DirectPosition coord) {
106 alfonx 644
107 alfonx 648 final int units = wheelEvt.getUnitsToScroll();
108     if (units <= 0)
109     mapPane.zoomTo(wheelEvt.getPoint(), 1. + .11 * units);
110     else
111     mapPane.zoomTo(wheelEvt.getPoint(), 2. / -units);
112     }
113 alfonx 644
114    
115 alfonx 648 @Override
116     public void performDragged(XMapPane mapPane, MouseEvent ev,
117     Point dragStartPos, Point dragLastPos,
118     DirectPosition startCoord, DirectPosition endCoord) {
119 alfonx 644
120 alfonx 648 if (dragLastPos != null)
121     mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos,
122     dragLastPos);
123 alfonx 639
124 alfonx 648 // If this is similar to a click, let mouseClicked handle it!
125     if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev
126     .getPoint().y
127     - dragStartPos.y)) < 160) {
128     // performClick(mapPane, ev, coord)
129     return;
130     }
131 alfonx 646
132 alfonx 648 final Rectangle bounds = mapPane.getBounds();
133    
134     Envelope mapArea = mapPane.getMapArea();
135    
136     // Replace with transform and translate
137     final double mapWidth = mapArea.getWidth();
138     final double mapHeight = mapArea.getHeight();
139    
140     final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width)
141     + mapArea.getMinX();
142     final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height)
143     + mapArea.getMinY();
144     final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width)
145     + mapArea.getMinX();
146     final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height)
147     + mapArea.getMinY();
148    
149     // make the dragged rectangle in screen coords the new map size?
150     final double left = Math.min(startX, endX);
151     final double right = Math.max(startX, endX);
152     final double bottom = Math.min(startY, endY);
153     final double top = Math.max(startY, endY);
154     final double nWidth = (mapWidth * mapWidth) / (right - left);
155     final double nHeight = (mapHeight * mapHeight) / (top - bottom);
156     final double deltaX1 = left - mapArea.getMinX();
157     final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
158     final double deltaY1 = bottom - mapArea.getMinY();
159     final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
160     final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
161     mapArea.getMinY() - nDeltaY1);
162     final double deltaX2 = mapArea.getMaxX() - right;
163     final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
164     final double deltaY2 = mapArea.getMaxY() - top;
165     final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
166     final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
167     mapArea.getMaxY() + nDeltaY2);
168    
169     mapPane.setMapArea(new Envelope(ll, ur));
170    
171     }
172     }
173 alfonx 646 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26