KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

37     protected boolean finished;
38
39     /**
40      * The initial rotation angle.
41      */

42     protected double initialRotation;
43
44     /**
45      * Tells whether the interactor has finished.
46      */

47     public boolean endInteraction() {
48         return finished;
49     }
50
51     // MouseListener ///////////////////////////////////////////////////////
52

53     /**
54      * Invoked when a mouse button has been pressed on a component.
55      */

56     public void mousePressed(MouseEvent JavaDoc e) {
57         finished = false;
58         JGVTComponent c = (JGVTComponent)e.getSource();
59
60         Dimension JavaDoc d = c.getSize();
61         double dx = e.getX() - d.width / 2;
62         double dy = e.getY() - d.height / 2;
63         double cos = -dy / Math.sqrt(dx * dx + dy * dy);
64         initialRotation = (dx > 0) ? Math.acos(cos) : -Math.acos(cos);
65     }
66
67     /**
68      * Invoked when a mouse button has been released on a component.
69      */

70     public void mouseReleased(MouseEvent JavaDoc e) {
71         finished = true;
72         JGVTComponent c = (JGVTComponent)e.getSource();
73
74         AffineTransform JavaDoc at = rotateTransform(c.getSize(), e.getX(), e.getY());
75         at.concatenate(c.getRenderingTransform());
76         c.setRenderingTransform(at);
77     }
78
79     /**
80      * Invoked when the mouse exits a component.
81      */

82     public void mouseExited(MouseEvent JavaDoc e) {
83         finished = true;
84         JGVTComponent c = (JGVTComponent)e.getSource();
85         c.setPaintingTransform(null);
86     }
87
88     // MouseMotionListener /////////////////////////////////////////////////
89

90     /**
91      * Invoked when a mouse button is pressed on a component and then
92      * dragged. Mouse drag events will continue to be delivered to
93      * the component where the first originated until the mouse button is
94      * released (regardless of whether the mouse position is within the
95      * bounds of the component).
96      */

97     public void mouseDragged(MouseEvent JavaDoc e) {
98         JGVTComponent c = (JGVTComponent)e.getSource();
99
100         c.setPaintingTransform(rotateTransform(c.getSize(), e.getX(), e.getY()));
101     }
102
103     /**
104      * Returns the rotate transform.
105      */

106     protected AffineTransform JavaDoc rotateTransform(Dimension JavaDoc d, int x, int y) {
107         double dx = x - d.width / 2;
108         double dy = y - d.height / 2;
109         double cos = -dy / Math.sqrt(dx * dx + dy * dy);
110         double angle = (dx > 0) ? Math.acos(cos) : -Math.acos(cos);
111
112         angle -= initialRotation;
113
114         return AffineTransform.getRotateInstance(angle, d.width / 2, d.height / 2);
115     }
116 }
117
Popular Tags