KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > gvt > AbstractZoomInteractor


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.swing.gvt;
19
20 import java.awt.BasicStroke JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27 import java.awt.geom.Line2D JavaDoc;
28
29 /**
30  * This class represents a zoom interactor.
31  * To use it, just redefine the {@link
32  * InteractorAdapter#startInteraction(InputEvent)} method.
33  *
34  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
35  * @version $Id: AbstractZoomInteractor.java,v 1.7 2004/08/18 07:15:32 vhardy Exp $
36  */

37 public class AbstractZoomInteractor extends InteractorAdapter {
38
39     /**
40      * Whether the interactor has finished.
41      */

42     protected boolean finished = true;
43
44     /**
45      * The mouse x start position.
46      */

47     protected int xStart;
48
49     /**
50      * The mouse y start position.
51      */

52     protected int yStart;
53
54     /**
55      * The mouse x current position.
56      */

57     protected int xCurrent;
58
59     /**
60      * The mouse y current position.
61      */

62     protected int yCurrent;
63
64     /**
65      * The zoom marker top line.
66      */

67     protected Line2D JavaDoc markerTop;
68
69     /**
70      * The zoom marker left line.
71      */

72     protected Line2D JavaDoc markerLeft;
73
74     /**
75      * The zoom marker bottom line.
76      */

77     protected Line2D JavaDoc markerBottom;
78
79     /**
80      * The zoom marker right line.
81      */

82     protected Line2D JavaDoc markerRight;
83
84     /**
85      * The overlay.
86      */

87     protected Overlay overlay = new ZoomOverlay();
88
89     /**
90      * Used to draw marker
91      */

92     protected BasicStroke JavaDoc markerStroke = new BasicStroke JavaDoc(1,
93                                                          BasicStroke.CAP_SQUARE,
94                                                          BasicStroke.JOIN_MITER,
95                                                          10,
96                                                          new float[] { 4, 4 }, 0);
97
98     /**
99      * Tells whether the interactor has finished.
100      */

101     public boolean endInteraction() {
102         return finished;
103     }
104
105     // MouseListener ///////////////////////////////////////////////////////
106

107     /**
108      * Invoked when a mouse button has been pressed on a component.
109      */

110     public void mousePressed(MouseEvent JavaDoc e) {
111         if (!finished) {
112             mouseExited(e);
113             return;
114         }
115         
116         finished = false;
117         markerTop = null;
118         markerLeft = null;
119         markerBottom = null;
120         markerRight = null;
121
122         xStart = e.getX();
123         yStart = e.getY();
124         JGVTComponent c = (JGVTComponent)e.getSource();
125         c.getOverlays().add(overlay);
126     }
127
128     /**
129      * Invoked when a mouse button has been released on a component.
130      */

131     public void mouseReleased(MouseEvent JavaDoc e) {
132         finished = true;
133         JGVTComponent c = (JGVTComponent)e.getSource();
134         c.getOverlays().remove(overlay);
135         overlay.paint(c.getGraphics());
136
137         xCurrent = e.getX();
138         yCurrent = e.getY();
139
140         if ((xCurrent - xStart) != 0 &&
141             (yCurrent - yStart) != 0) {
142
143             int dx = xCurrent - xStart;
144             int dy = yCurrent - yStart;
145             
146             if (dx < 0) {
147                 dx = -dx;
148                 xStart = xCurrent;
149             }
150             if (dy < 0) {
151                 dy = -dy;
152                 yStart = yCurrent;
153             }
154
155             Dimension JavaDoc size = c.getSize();
156
157             // Zoom factor
158
float scaleX = size.width / (float)dx;
159             float scaleY = size.height / (float)dy;
160             float scale = (scaleX < scaleY) ? scaleX : scaleY;
161         
162             // Zoom translate
163
AffineTransform JavaDoc at = new AffineTransform JavaDoc();
164             at.scale(scale, scale);
165             at.translate(-xStart, -yStart);
166
167             at.concatenate(c.getRenderingTransform());
168             c.setRenderingTransform(at);
169         }
170     }
171
172     /**
173      * Invoked when the mouse exits a component.
174      */

175     public void mouseExited(MouseEvent JavaDoc e) {
176         finished = true;
177         JGVTComponent c = (JGVTComponent)e.getSource();
178         c.getOverlays().remove(overlay);
179         overlay.paint(c.getGraphics());
180     }
181
182     // MouseMotionListener /////////////////////////////////////////////////
183

184     /**
185      * Invoked when a mouse button is pressed on a component and then
186      * dragged. Mouse drag events will continue to be delivered to
187      * the component where the first originated until the mouse button is
188      * released (regardless of whether the mouse position is within the
189      * bounds of the component).
190      */

191     public void mouseDragged(MouseEvent JavaDoc e) {
192         JGVTComponent c = (JGVTComponent)e.getSource();
193
194         overlay.paint(c.getGraphics());
195
196         xCurrent = e.getX();
197         yCurrent = e.getY();
198
199         // Constrain rectangle to window's Aspect Ratio.
200
float xMin, yMin, width, height;
201         if (xStart < xCurrent) {
202             xMin = xStart;
203             width = xCurrent - xStart;
204         } else {
205             xMin = xCurrent;
206             width = xStart - xCurrent;
207         }
208         if (yStart < yCurrent) {
209             yMin = yStart;
210             height = yCurrent - yStart;
211         } else {
212             yMin = yCurrent;
213             height = yStart - yCurrent;
214         }
215         Dimension JavaDoc d = c.getSize();
216         float compAR = d.width/(float)d.height;
217         if (compAR > width/height) {
218             width = compAR*height;
219         } else {
220             height = width/compAR;
221         }
222
223         markerTop = new Line2D.Float JavaDoc(xMin, yMin, xMin+width, yMin);
224         markerLeft = new Line2D.Float JavaDoc(xMin, yMin, xMin, yMin+height);
225         markerBottom = new Line2D.Float JavaDoc(xMin, yMin+height,
226                                         xMin+width, yMin+height);
227         markerRight = new Line2D.Float JavaDoc(xMin+width, yMin,
228                                         xMin+width, yMin+height);
229
230         overlay.paint(c.getGraphics());
231     }
232
233     /**
234      * To paint the interactor.
235      */

236     protected class ZoomOverlay implements Overlay {
237         
238         /**
239          * Paints this overlay.
240          */

241         public void paint(Graphics JavaDoc g) {
242             if (markerTop != null) {
243                 Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
244
245                 g2d.setXORMode(Color.white);
246                 g2d.setColor(Color.black);
247                 g2d.setStroke(markerStroke);
248
249                 g2d.draw(markerTop);
250                 g2d.draw(markerLeft);
251                 g2d.draw(markerBottom);
252                 g2d.draw(markerRight);
253             }
254         }
255     }
256 }
257
Popular Tags