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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/2.0-RC2/src/skrueger/geotools/XMapPaneAction_ZoomIn.java revision 639 by alfonx, Thu Jan 28 16:07:22 2010 UTC branches/2.0-RC2/src/skrueger/geotools/XMapPaneAction_Zoom.java revision 649 by alfonx, Thu Jan 28 17:24:15 2010 UTC
# Line 1  Line 1 
1  package skrueger.geotools;  package skrueger.geotools;
2    
 import java.awt.Color;  
 import java.awt.Graphics;  
3  import java.awt.Point;  import java.awt.Point;
4    import java.awt.Rectangle;
5  import java.awt.event.MouseEvent;  import java.awt.event.MouseEvent;
6  import java.awt.event.MouseWheelEvent;  import java.awt.event.MouseWheelEvent;
 import java.awt.geom.Point2D;  
7    
8  import org.opengis.geometry.DirectPosition;  import org.opengis.geometry.DirectPosition;
9    
10  public class XMapPaneAction_ZoomIn implements XMapPaneAction {  import com.vividsolutions.jts.geom.Coordinate;
11            import com.vividsolutions.jts.geom.Envelope;
12          /**  
13           * Stores last position of a drag event in window coordinates  public abstract class XMapPaneAction_Zoom implements XMapPaneAction {
          */  
         protected Point lastPos;  
           
         @Override  
         public void performClick(XMapPane mapPane, MouseEvent ev,  
                         DirectPosition coord) {  
                   
                 mapPane.zoomTo(ev.getPoint(), 1 / 2.);  
         }  
14    
15          @Override          @Override
16          public void performDragging(XMapPane mapPane, MouseEvent ev,          public void performDragging(XMapPane mapPane, MouseEvent ev,
17                          Point dragStartPos, DirectPosition startCoord,                          Point dragStartPos, Point dragLastPos,
18                          DirectPosition endCoord) {                          DirectPosition startCoord, DirectPosition endCoord) {
                   
                 mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev.getPoint());  
         }  
19    
20          @Override                  if (dragLastPos != null)
21          public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt,                          mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos,
22                          DirectPosition coord) {                                          dragLastPos);
23    
24                  final int units = wheelEvt.getUnitsToScroll();                  mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev
25                  if (units > 0)                                  .getPoint());
                         mapPane.zoomTo(wheelEvt.getPoint(), 1. + .11 * units);  
                 else  
                         mapPane.zoomTo(wheelEvt.getPoint(), 2. / -units);  
26          }          }
27    
28          @Override          public static class In extends XMapPaneAction_Zoom {
         public void performDragged(XMapPane mapPane, MouseEvent ev,  
                         Point dragStartPos, DirectPosition startCoord,  
                         DirectPosition endCoord) {  
                 // TODO Auto-generated method stub  
29    
30                    @Override
31                    public void performClick(XMapPane mapPane, MouseEvent ev,
32                                    DirectPosition coord) {
33    
34                            mapPane.zoomTo(ev.getPoint(), 1 / 2.);
35                    }
36    
37                    @Override
38                    public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt,
39                                    DirectPosition coord) {
40    
41                            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    
48                    @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          }          }
93    
94  }          public static class Out extends XMapPaneAction_Zoom {
95    
96                    @Override
97                    public void performClick(XMapPane mapPane, MouseEvent ev,
98                                    DirectPosition coord) {
99    
100                            mapPane.zoomTo(ev.getPoint(), 2.);
101                    }
102    
103                    @Override
104                    public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt,
105                                    DirectPosition coord) {
106    
107                            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    
114    
115                    @Override
116                    public void performDragged(XMapPane mapPane, MouseEvent ev,
117                                    Point dragStartPos, Point dragLastPos,
118                                    DirectPosition startCoord, DirectPosition endCoord) {
119    
120                            if (dragLastPos != null)
121                                    mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos,
122                                                    dragLastPos);
123    
124                            // 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    
132                            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    }

Legend:
Removed from v.639  
changed lines
  Added in v.649

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26