KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > util > GeomUtil


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 package org.nightlabs.editor2d.util;
27
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.geom.AffineTransform JavaDoc;
30
31 import org.apache.log4j.Logger;
32
33 /**
34  * A Utility class which provides useful Methods for geometric operations
35  *
36  * @author Daniel.Mazurek <at> NightLabs <dot> de
37  *
38  */

39 public class GeomUtil
40 {
41     public static final Logger LOGGER = Logger.getLogger(GeomUtil.class);
42     
43     public GeomUtil() {
44         super();
45     }
46
47     private static AffineTransform JavaDoc at = new AffineTransform JavaDoc();
48     
49     /**
50      * creates a AffineTransform which transforms the second Rectangle (x2, y2, w2, h2)
51      * into the first Rectangle (x1, y1, w1, h1)
52      *
53      * @param x1 the x-Coordinate of the first rectangle
54      * @param y1 the y-Coordinate of the first rectangle
55      * @param w1 the width of the first rectangle
56      * @param h1 the height of the first rectangle
57      * @param x2 the x-Coordinate of the second rectangle
58      * @param y2 the y-Coordinate of the second rectangle
59      * @param w2 the width of the second rectangle
60      * @param h2 the height of the second rectangle
61      * @return a AffineTransform which represents the transformation from the second
62      * rectangle into the first rectangle
63      *
64      * @see AffineTransform
65      */

66   public static AffineTransform JavaDoc getAffineTransform(int x1, int y1, int w1, int h1,
67       int x2, int y2, int w2, int h2)
68   {
69     // if both Rectangles are equal do nothing
70
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2)
71     {
72 // LOGGER.debug("Both Rectangles are Equal!");
73
at.setToIdentity();
74       return at;
75     }
76           
77     // if only a Translation is performed, just translate
78
if (w1 == w2 && h1 == h2)
79     {
80 // LOGGER.debug("Only Translation!");
81
at.setToIdentity();
82       at.translate(x2 - x1, y2 - y1);
83       return at;
84     }
85     else if (x1 == x2 && y1 == y2)
86     {
87 // LOGGER.debug("Only Scale");
88
at.setToIdentity();
89       float ratioY = (float)h2 / (float)h1;
90       float ratioX = (float)w2 / (float)w1;
91       float distanceX = (float)x1 - ((float)x1*ratioX);
92       float distanceY = (float)y1 - ((float)y1*ratioY);
93       at.translate(distanceX, distanceY);
94       at.scale(ratioX, ratioY);
95       return at;
96     }
97     else
98     {
99 // LOGGER.debug("Scale + Translation");
100
// translate to origin and scale
101
double ratioX = ((double)w2) / ((double)w1);
102       double ratioY = ((double)h2) / ((double)h1);
103       double x = (double)x1;
104       double y = (double)y1;
105       double distanceX = x - (x*ratioX);
106       double distanceY = y - (y*ratioY);
107       at.setToIdentity();
108       at.translate(distanceX, distanceY);
109       at.scale(ratioX, ratioY);
110       
111       // translate back
112
AffineTransform JavaDoc at2 = new AffineTransform JavaDoc();
113       distanceX = x2 - x1;
114       distanceY = y2 - y1;
115       at2.translate(distanceX, distanceY);
116       
117       at.preConcatenate(at2);
118     }
119     return at;
120   }
121     
122   /**
123    * creates a AffineTransform which transforms the second Rectangle (newBounds)
124      * into the first Rectangle (oldBounds)
125    *
126    * @param oldBounds the target Rectangle
127    * @param newBounds the source Rectangle
128    * @return a AffineTransform which represents the transformation from the newBounds
129    * into oldBounds
130    */

131   public static AffineTransform JavaDoc getAffineTransform(Rectangle JavaDoc oldBounds, Rectangle JavaDoc newBounds)
132   {
133     return getAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
134         newBounds.x, newBounds.y, newBounds.width, newBounds.height);
135   }
136 }
137
Popular Tags