KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.MouseEvent JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22
23 /**
24  * This class represents a zoom interactor.
25  * To use it, just redefine the {@link
26  * InteractorAdapter#startInteraction(InputEvent)} method.
27  *
28  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
29  * @version $Id: AbstractImageZoomInteractor.java,v 1.4 2004/08/18 07:15:32 vhardy Exp $
30  */

31 public class AbstractImageZoomInteractor extends InteractorAdapter {
32
33     /**
34      * Whether the interactor has finished.
35      */

36     protected boolean finished = true;
37
38     /**
39      * The mouse x start position.
40      */

41     protected int xStart;
42
43     /**
44      * The mouse y start position.
45      */

46     protected int yStart;
47
48     /**
49      * The mouse x current position.
50      */

51     protected int xCurrent;
52
53     /**
54      * The mouse y current position.
55      */

56     protected int yCurrent;
57
58     /**
59      * Tells whether the interactor has finished.
60      */

61     public boolean endInteraction() {
62         return finished;
63     }
64
65     // MouseListener ///////////////////////////////////////////////////////
66

67     /**
68      * Invoked when a mouse button has been pressed on a component.
69      */

70     public void mousePressed(MouseEvent JavaDoc e) {
71         if (!finished) {
72             JGVTComponent c = (JGVTComponent)e.getSource();
73             c.setPaintingTransform(null);
74             return;
75         }
76         
77         finished = false;
78
79         xStart = e.getX();
80         yStart = e.getY();
81     }
82
83     /**
84      * Invoked when a mouse button has been released on a component.
85      */

86     public void mouseReleased(MouseEvent JavaDoc e) {
87         finished = true;
88
89         JGVTComponent c = (JGVTComponent)e.getSource();
90
91         AffineTransform JavaDoc pt = c.getPaintingTransform();
92         if (pt != null) {
93             AffineTransform JavaDoc rt = (AffineTransform JavaDoc)c.getRenderingTransform().clone();
94             rt.preConcatenate(pt);
95             c.setRenderingTransform(rt);
96         }
97     }
98
99     // MouseMotionListener /////////////////////////////////////////////////
100

101     /**
102      * Invoked when a mouse button is pressed on a component and then
103      * dragged. Mouse drag events will continue to be delivered to
104      * the component where the first originated until the mouse button is
105      * released (regardless of whether the mouse position is within the
106      * bounds of the component).
107      */

108     public void mouseDragged(MouseEvent JavaDoc e) {
109         JGVTComponent c = (JGVTComponent)e.getSource();
110
111         xCurrent = e.getX();
112         yCurrent = e.getY();
113
114         AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(xStart, yStart);
115         int dy = yCurrent - yStart;
116         if (dy < 0) {
117             dy = (dy > -5) ? 15 : dy - 10;
118         } else {
119             dy = (dy < 5) ? 15 : dy + 10;
120         }
121         double s = dy / 15.0;
122         s = (s > 0) ? s : -1 / s;
123
124         at.scale(s, s);
125         at.translate(-xStart, -yStart);
126         c.setPaintingTransform(at);
127     }
128 }
129
Popular Tags