KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > util > TransformUtil


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.util;
28
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.geom.AffineTransform JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.nightlabs.editor2d.j2d.GeneralShape;
34
35 public class TransformUtil
36 {
37     public static final Logger LOGGER = Logger.getLogger(TransformUtil.class);
38     protected static AffineTransform JavaDoc at = new AffineTransform JavaDoc();
39     
40     public static void transformGeneralShape(GeneralShape gs,
41             Rectangle JavaDoc oldBounds, Rectangle JavaDoc newBounds)
42     {
43         transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
44         newBounds.x, newBounds.y, newBounds.width, newBounds.height, false);
45     }
46     
47     public static void transformGeneralShape(GeneralShape gs,
48             org.eclipse.swt.graphics.Rectangle oldBounds,
49             org.eclipse.swt.graphics.Rectangle newBounds)
50     {
51         transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
52         newBounds.x, newBounds.y, newBounds.width, newBounds.height, false);
53     }
54     
55     public static void transformGeneralShape(GeneralShape generalShape, int x1, int y1, int w1, int h1,
56         int x2, int y2, int w2, int h2, boolean cloneGS)
57     {
58       // TODO: if cloneGS is true return the cloned GeneralShape in a seperate Method
59
// else return the transformed generalShape for convience
60
GeneralShape gs;
61       if (cloneGS) {
62         gs = (GeneralShape) generalShape.clone();
63       } else {
64         gs = generalShape;
65       }
66                 
67       // if both Rectangles are equal do nothing
68
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) {
69         LOGGER.debug("Both Rectangles are Equal!");
70         return;
71       }
72               
73       // if only a Translation is performed, just translate
74
if (w1 == w2 && h1 == h2)
75       {
76         at.setToIdentity();
77         at.translate(x2 - x1, y2 - y1);
78         gs.transform(at);
79       }
80       // translate to origin and scale
81
else
82       {
83           double ratioX = ((double)w2) / ((double)w1);
84           double ratioY = ((double)h2) / ((double)h1);
85         double x = (double)x1;
86         double y = (double)y1;
87         double distanceX = x - (x*ratioX);
88         double distanceY = y - (y*ratioY);
89         at.setToIdentity();
90         at.translate(distanceX, distanceY);
91         at.scale(ratioX, ratioY);
92         gs.transform(at);
93           
94         // translate back
95
distanceX = x2 - x1;
96         distanceY = y2 - y1;
97         at.setToIdentity();
98         at.translate(distanceX, distanceY);
99         gs.transform(at);
100       }
101     }
102
103     /**
104      *
105      * expands a Rectangle, the center is kept constant
106      * @param r the source Rectangle
107      * @param h the horizonal expand
108      * @param v the vertical expand
109      * @param clone determines if the source rectangle is expanded or a cloned object is returned
110      * @return the expanded Rectangle
111      */

112     public static Rectangle JavaDoc expand(Rectangle JavaDoc r, int h, int v, boolean clone)
113     {
114         if (!clone) {
115             r.x = r.x - v;
116             r.width = r.width + v;
117             r.y = r.y - h;
118             r.height = r.y + h;
119             return r;
120         }
121         else {
122             return new Rectangle JavaDoc(r.x - v, r.y - h, r.width + v, r.y + h);
123         }
124     }
125     
126     /**
127      *
128      * shrinks a Rectangle, the center is kept constant
129      * @param r the source Rectangle
130      * @param h the horizonal shrink
131      * @param v the vertical shrink
132      * @param clone determines if the source rectangle is shrinked or a cloned object is returned
133      * @return the shrinked Rectangle
134      */

135     public static Rectangle JavaDoc shrink(Rectangle JavaDoc r, int h, int v, boolean clone)
136     {
137         if (!clone) {
138             r.x = r.x + v;
139             r.width = r.width - v;
140             r.y = r.y + h;
141             r.height = r.height - h;
142             return r;
143         }
144         else {
145             return new Rectangle JavaDoc(r.x + v, r.width - v, r.y + h, r.height - h);
146         }
147     }
148     
149     public static Rectangle JavaDoc toAbsolute(Rectangle JavaDoc rect, double zoom, boolean clone)
150     {
151         if (!clone) {
152             if (zoom != 0) {
153                 rect.x = (int) ((double)rect.x / zoom);
154                 rect.y = (int) ((double)rect.y / zoom);
155                 rect.width = (int) ((double)rect.width / zoom);
156                 rect.height = (int) ((double)rect.height / zoom);
157             }
158             return rect;
159         }
160         else {
161             Rectangle JavaDoc r = new Rectangle JavaDoc(rect);
162             if (zoom != 0) {
163                 r.x = (int) ((double)rect.x / zoom);
164                 r.y = (int) ((double)rect.y / zoom);
165                 r.width = (int) ((double)rect.width / zoom);
166                 r.height = (int) ((double)rect.height / zoom);
167             }
168             return r;
169         }
170     }
171     
172     public static Rectangle JavaDoc toRelative(Rectangle JavaDoc rect, double zoom, boolean clone)
173     {
174         if (!clone) {
175             if (zoom != 0) {
176                 rect.x = (int) ((double)rect.x * zoom);
177                 rect.y = (int) ((double)rect.y * zoom);
178                 rect.width = (int) ((double)rect.width * zoom);
179                 rect.height = (int) ((double)rect.height * zoom);
180             }
181             return rect;
182         }
183         else {
184             Rectangle JavaDoc r = new Rectangle JavaDoc(rect);
185             if (zoom != 0) {
186                 r.x = (int) ((double)rect.x * zoom);
187                 r.y = (int) ((double)rect.y * zoom);
188                 r.width = (int) ((double)rect.width * zoom);
189                 r.height = (int) ((double)rect.height * zoom);
190             }
191             return r;
192         }
193     }
194 }
195
Popular Tags