1 26 27 package org.nightlabs.editor2d.viewer.util; 28 29 import java.awt.Rectangle ; 30 import java.awt.geom.AffineTransform ; 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 at = new AffineTransform (); 39 40 public static void transformGeneralShape(GeneralShape gs, 41 Rectangle oldBounds, Rectangle 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 GeneralShape gs; 61 if (cloneGS) { 62 gs = (GeneralShape) generalShape.clone(); 63 } else { 64 gs = generalShape; 65 } 66 67 if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) { 69 LOGGER.debug("Both Rectangles are Equal!"); 70 return; 71 } 72 73 if (w1 == w2 && h1 == h2) 75 { 76 at.setToIdentity(); 77 at.translate(x2 - x1, y2 - y1); 78 gs.transform(at); 79 } 80 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 distanceX = x2 - x1; 96 distanceY = y2 - y1; 97 at.setToIdentity(); 98 at.translate(distanceX, distanceY); 99 gs.transform(at); 100 } 101 } 102 103 112 public static Rectangle expand(Rectangle 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 (r.x - v, r.y - h, r.width + v, r.y + h); 123 } 124 } 125 126 135 public static Rectangle shrink(Rectangle 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 (r.x + v, r.width - v, r.y + h, r.height - h); 146 } 147 } 148 149 public static Rectangle toAbsolute(Rectangle 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 r = new Rectangle (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 toRelative(Rectangle 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 r = new Rectangle (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 |