KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > util > J2DUtil


1 /**
2  * <p> Project: com.nightlabs.gui </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 10.01.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.util;
9
10 import java.awt.geom.AffineTransform JavaDoc;
11 import java.awt.geom.PathIterator JavaDoc;
12 import java.awt.geom.Point2D JavaDoc;
13 import java.awt.geom.Rectangle2D JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.draw2d.Polyline;
17 import org.eclipse.draw2d.geometry.PointList;
18 import org.eclipse.swt.graphics.Rectangle;
19
20 import com.nightlabs.editor2d.j2d.GeneralShape;
21 import com.nightlabs.rcp.util.ColorUtil;
22
23
24 public class J2DUtil
25 extends ColorUtil
26 {
27
28   public static final Logger LOGGER = Logger.getLogger(J2DUtil.class);
29   
30     public static org.eclipse.draw2d.geometry.Point toDraw2D(Point2D JavaDoc p) {
31       return new org.eclipse.draw2d.geometry.Point(p.getX(), p.getY());
32     }
33     
34     public static Point2D JavaDoc toPoint2D(org.eclipse.draw2d.geometry.Point p) {
35       return new Point2D.Double JavaDoc(p.x, p.y);
36     }
37     
38   public static Rectangle toSWTRectangle(org.eclipse.draw2d.geometry.Rectangle rect) {
39     return new Rectangle(rect.x, rect.y, rect.width, rect.height);
40   }
41   
42   public static org.eclipse.draw2d.geometry.Rectangle toDraw2D(Rectangle rect) {
43     return new org.eclipse.draw2d.geometry.Rectangle(rect.x, rect.y, rect.width, rect.height);
44   }
45   
46     public static java.awt.Rectangle JavaDoc toAWTRectangle(org.eclipse.draw2d.geometry.Rectangle rectangle) {
47       return new java.awt.Rectangle JavaDoc(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
48     }
49     
50     public static org.eclipse.draw2d.geometry.Rectangle toDraw2D(Rectangle2D JavaDoc r2d) {
51       java.awt.Rectangle JavaDoc r = r2d.getBounds();
52       return new org.eclipse.draw2d.geometry.Rectangle(r.x, r.y, r.width, r.height);
53     }
54
55     public static Rectangle2D JavaDoc toRectangle2D(org.eclipse.draw2d.geometry.Rectangle r) {
56       return new Rectangle2D.Double JavaDoc(r.x, r.y, r.width, r.height);
57     }
58     
59     public static void transformAWTGeneralShape(GeneralShape gs,
60              java.awt.Rectangle JavaDoc oldBounds,
61              java.awt.Rectangle JavaDoc newBounds,
62              boolean cloneGS)
63     {
64       transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
65           newBounds.x, newBounds.y, newBounds.width, newBounds.height, cloneGS);
66     }
67     
68     public static void transformAWTGeneralShape(GeneralShape gs,
69              java.awt.Rectangle JavaDoc oldBounds,
70              java.awt.Rectangle JavaDoc newBounds)
71     {
72       transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
73           newBounds.x, newBounds.y, newBounds.width, newBounds.height, false);
74     }
75     
76     public static void transformGeneralShape(GeneralShape gs,
77                                                                                  org.eclipse.draw2d.geometry.Rectangle oldBounds,
78                                                                                  org.eclipse.draw2d.geometry.Rectangle newBounds,
79                                                                                  boolean cloneGS)
80     {
81       transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
82           newBounds.x, newBounds.y, newBounds.width, newBounds.height, false);
83     }
84         
85     public static void transformGeneralShape(GeneralShape gs,
86              org.eclipse.draw2d.geometry.Rectangle oldBounds,
87              org.eclipse.draw2d.geometry.Rectangle newBounds)
88     {
89         transformGeneralShape(gs, oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
90         newBounds.x, newBounds.y, newBounds.width, newBounds.height, false);
91     }
92     
93     protected static AffineTransform JavaDoc at = new AffineTransform JavaDoc();
94     
95     public static void transformGeneralShape(GeneralShape generalShape, int x1, int y1, int w1, int h1,
96         int x2, int y2, int w2, int h2, boolean cloneGS)
97     {
98       // TODO: if cloneGS is true return the cloned GeneralShape in a seperate Method
99
// else return the transformed generalShape for convience
100
GeneralShape gs;
101       if (cloneGS) {
102         gs = (GeneralShape) generalShape.clone();
103       } else {
104         gs = generalShape;
105       }
106                 
107       // if both Rectangles are equal do nothing
108
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) {
109         LOGGER.debug("Both Rectangles are Equal!");
110         return;
111       }
112               
113       // if only a Translation is performed, just translate
114
if (w1 == w2 && h1 == h2)
115       {
116         at.setToIdentity();
117         at.translate(x2 - x1, y2 - y1);
118         gs.transform(at);
119       }
120       // translate to origin and scale
121
else
122       {
123           double ratioX = ((double)w2) / ((double)w1);
124           double ratioY = ((double)h2) / ((double)h1);
125         double x = (double)x1;
126         double y = (double)y1;
127         double distanceX = x - (x*ratioX);
128         double distanceY = y - (y*ratioY);
129         at.setToIdentity();
130         at.translate(distanceX, distanceY);
131         at.scale(ratioX, ratioY);
132         gs.transform(at);
133           
134         // translate back
135
distanceX = x2 - x1;
136         distanceY = y2 - y1;
137         at.setToIdentity();
138         at.translate(distanceX, distanceY);
139         gs.transform(at);
140       }
141     }
142      
143   public static AffineTransform JavaDoc getAffineTransform(int x1, int y1, int w1, int h1,
144       int x2, int y2, int w2, int h2)
145   {
146     // if both Rectangles are equal do nothing
147
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2)
148     {
149       LOGGER.debug("Both Rectangles are Equal!");
150       at.setToIdentity();
151       return at;
152     }
153           
154     // if only a Translation is performed, just translate
155
if (w1 == w2 && h1 == h2)
156     {
157 // LOGGER.debug("Only Translation!");
158
at.setToIdentity();
159       at.translate(x2 - x1, y2 - y1);
160       return at;
161     }
162     else if (x1 == x2 && y1 == y2)
163     {
164 // LOGGER.debug("Only Scale");
165
at.setToIdentity();
166       float ratioY = (float)h2 / (float)h1;
167       float ratioX = (float)w2 / (float)w1;
168       float distanceX = (float)x1 - ((float)x1*ratioX);
169       float distanceY = (float)y1 - ((float)y1*ratioY);
170       at.translate(distanceX, distanceY);
171       at.scale(ratioX, ratioY);
172       return at;
173     }
174     else
175     {
176 // LOGGER.debug("Scale + Translation");
177
// translate to origin and scale
178
double ratioX = ((double)w2) / ((double)w1);
179       double ratioY = ((double)h2) / ((double)h1);
180       double x = (double)x1;
181       double y = (double)y1;
182       double distanceX = x - (x*ratioX);
183       double distanceY = y - (y*ratioY);
184       at.setToIdentity();
185       at.translate(distanceX, distanceY);
186       at.scale(ratioX, ratioY);
187       
188       // translate back
189
AffineTransform JavaDoc at2 = new AffineTransform JavaDoc();
190       distanceX = x2 - x1;
191       distanceY = y2 - y1;
192       at2.translate(distanceX, distanceY);
193       
194       at.preConcatenate(at2);
195     }
196     return at;
197   }
198
199   public static AffineTransform JavaDoc getTranslateAffineTransform(java.awt.Rectangle JavaDoc oldBounds,
200       java.awt.Rectangle JavaDoc newBounds)
201   {
202     return getTranslateAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
203         newBounds.x, newBounds.y, newBounds.width, newBounds.height);
204   }
205   
206   /*
207    * Should only be used in Combination with getScaledAffineTransform() by calling it afterwards
208    */

209   public static AffineTransform JavaDoc getTranslateAffineTransform(int x1, int y1, int w1, int h1,
210       int x2, int y2, int w2, int h2)
211   {
212     // if both Rectangles are equal do nothing
213
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) {
214       LOGGER.debug("Both Rectangles are Equal!");
215       at.setToIdentity();
216       return at;
217     }
218           
219     // if only a Translation is performed, just translate
220
if (w1 == w2 && h1 == h2)
221     {
222       at.setToIdentity();
223     }
224     // translate to origin and scale
225
else
226     {
227       at.setToIdentity();
228       int distanceX = x2 - x1;
229       int distanceY = y2 - y1;
230       at.translate(distanceX, distanceY);
231     }
232     return at;
233   }
234
235   public static AffineTransform JavaDoc getScaleAffineTransform(java.awt.Rectangle JavaDoc oldBounds,
236       java.awt.Rectangle JavaDoc newBounds)
237   {
238     return getScaleAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
239         newBounds.x, newBounds.y, newBounds.width, newBounds.height);
240   }
241   
242   /*
243    * Should only be used in Combination with getTranslateAffineTransform() by calling it first
244    */

245   public static AffineTransform JavaDoc getScaleAffineTransform(int x1, int y1, int w1, int h1,
246       int x2, int y2, int w2, int h2)
247   {
248     // if both Rectangles are equal do nothing
249
if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) {
250       at.setToIdentity();
251       return at;
252     }
253     
254     // if only a Translation is performed, just translate
255
if (w1 == w2 && h1 == h2)
256     {
257       at.setToIdentity();
258       at.translate(x2 - x1, y2 - y1);
259     }
260     // translate to origin and scale
261
else
262     {
263       double ratioX = ((double)w2) / ((double)w1);
264       double ratioY = ((double)h2) / ((double)h1);
265       double x = (double)x1;
266       double y = (double)y1;
267       double distanceX = x - (x*ratioX);
268       double distanceY = y - (y*ratioY);
269       at.setToIdentity();
270       at.translate(distanceX, distanceY);
271       at.scale(ratioX, ratioY);
272     }
273     return at;
274   }
275   
276   public static AffineTransform JavaDoc getAffineTransform(org.eclipse.draw2d.geometry.Rectangle oldBounds,
277       org.eclipse.draw2d.geometry.Rectangle newBounds)
278   {
279     return getAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
280         newBounds.x, newBounds.y, newBounds.width, newBounds.height);
281   }
282   
283   public static AffineTransform JavaDoc getAffineTransform(java.awt.Rectangle JavaDoc oldBounds,
284       java.awt.Rectangle JavaDoc newBounds)
285   {
286     return getAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height,
287         newBounds.x, newBounds.y, newBounds.width, newBounds.height);
288   }
289   
290   public static double calcRotationInRadians(double _degrees)
291   {
292     double degreesToRotate = 0;
293         
294     if (_degrees > 360 || _degrees < -360)
295       degreesToRotate = _degrees%360;
296             
297     return Math.toRadians(degreesToRotate);
298   }
299     
300   public static PointList getPathSegments(GeneralShape gs)
301   {
302     PointList points = new PointList();
303     if (gs != null)
304     {
305       double[] coords = new double[6];
306       org.eclipse.draw2d.geometry.Point p, p2, p3;
307       for (PathIterator JavaDoc pi = gs.getPathIterator(null); !pi.isDone(); pi.next())
308       {
309         int segType = pi.currentSegment(coords);
310         switch (segType)
311         {
312             case (PathIterator.SEG_MOVETO):
313                 p = new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]);
314                 points.addPoint(p);
315               break;
316             case (PathIterator.SEG_LINETO):
317                 p = new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]);
318                 points.addPoint(p);
319               break;
320             case (PathIterator.SEG_QUADTO):
321                 p = new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]);
322                 p2 = new org.eclipse.draw2d.geometry.Point(coords[2], coords[3]);
323                 points.addPoint(p);
324                 points.addPoint(p2);
325               break;
326             case (PathIterator.SEG_CUBICTO):
327                 p = new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]);
328                     p2 = new org.eclipse.draw2d.geometry.Point(coords[2], coords[3]);
329                     p3 = new org.eclipse.draw2d.geometry.Point(coords[4], coords[5]);
330                 points.addPoint(p);
331                 points.addPoint(p2);
332                 points.addPoint(p3);
333               break;
334             case (PathIterator.SEG_CLOSE):
335     
336               break;
337         }
338       }
339     }
340     return points;
341   }
342   
343   public static Polyline toPolyline(GeneralShape gs, org.eclipse.draw2d.geometry.Rectangle newBounds)
344   {
345     at.setToIdentity();
346     org.eclipse.draw2d.geometry.Rectangle oldBounds = toDraw2D(gs.getBounds());
347     transformGeneralShape(gs, oldBounds, newBounds, true);
348     return toPolyline(gs);
349   }
350   
351   public static Polyline toPolyline(GeneralShape gs)
352   {
353     Polyline polyline = new Polyline();
354     double[] coords = new double[6];
355     
356     for (PathIterator JavaDoc pi = gs.getPathIterator(new AffineTransform JavaDoc());
357                 !pi.isDone(); pi.next())
358     {
359       int segType = pi.currentSegment(coords);
360       switch (segType) {
361       case (PathIterator.SEG_MOVETO):
362         pi.currentSegment(coords);
363         polyline.addPoint(new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]));
364         break;
365       case (PathIterator.SEG_LINETO):
366         pi.currentSegment(coords);
367         polyline.addPoint(new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]));
368         break;
369       case (PathIterator.SEG_QUADTO):
370         pi.currentSegment(coords);
371         polyline.addPoint(new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]));
372         break;
373       case (PathIterator.SEG_CUBICTO):
374         pi.currentSegment(coords);
375             polyline.addPoint(new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]));
376         break;
377       case (PathIterator.SEG_CLOSE):
378 // pi.currentSegment(coords);
379
// polyline.addPoint(new org.eclipse.draw2d.geometry.Point(coords[0], coords[1]));
380
break;
381       }
382     }
383     return polyline;
384   }
385   
386   public static GeneralShape toGeneralShape(Polyline polyline)
387   {
388     PointList points = polyline.getPoints();
389     GeneralShape gs = new GeneralShape();
390     for (int i=0; i<points.size(); i++)
391     {
392       org.eclipse.draw2d.geometry.Point p = points.getPoint(i);
393       if (i==0)
394         gs.moveTo(p.x, p.y);
395       else
396         gs.lineTo(p.x, p.y);
397     }
398     return gs;
399   }
400     
401   public static GeneralShape removePathSegment(GeneralShape generalShape, int index)
402   {
403     if (generalShape == null)
404       throw new IllegalArgumentException JavaDoc("Param generalShape MUST not be null!");
405       
406     if (index > generalShape.getNumTypes())
407       throw new IndexOutOfBoundsException JavaDoc("Param index is out of GeneralShape PathSegment Bounds!");
408
409     if (index == 0)
410       removeFirstPathSegment(generalShape);
411     
412     float[] coords = new float[6];
413     int pathIndex = 0;
414     GeneralShape gs = new GeneralShape();
415     boolean indexSet = false;
416     for (PathIterator JavaDoc pi = generalShape.getPathIterator(new AffineTransform JavaDoc()); !pi.isDone(); pi.next())
417     {
418       if (pathIndex == index)
419       {
420         pathIndex = -1;
421         indexSet = true;
422         continue;
423       }
424                 
425       int segType = pi.currentSegment(coords);
426       switch (segType)
427       {
428           case (PathIterator.SEG_MOVETO):
429             gs.moveTo(coords[0], coords[1]);
430             if (!indexSet)
431               pathIndex++;
432             break;
433           case (PathIterator.SEG_LINETO):
434             gs.lineTo(coords[0], coords[1]);
435             if (!indexSet)
436               pathIndex++;
437             break;
438           case (PathIterator.SEG_QUADTO):
439             gs.quadTo(coords[0], coords[1], coords[2], coords[3]);
440             if (!indexSet)
441               pathIndex++;
442             break;
443           case (PathIterator.SEG_CUBICTO):
444             gs.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
445             if (!indexSet)
446               pathIndex++;
447             break;
448           case (PathIterator.SEG_CLOSE):
449             gs.closePath();
450             if (!indexSet)
451               pathIndex++;
452             break;
453       }
454     }
455     return gs;
456   }
457   
458   public static GeneralShape removeFirstPathSegment(GeneralShape generalShape)
459   {
460     float[] coords = new float[6];
461     int pathIndex = 0;
462     GeneralShape gs = new GeneralShape();
463     for (PathIterator JavaDoc pi = generalShape.getPathIterator(null); !pi.isDone(); pi.next())
464     {
465       int segType = pi.currentSegment(coords);
466       switch (segType)
467       {
468           case (PathIterator.SEG_MOVETO):
469             pathIndex++;
470             break;
471           case (PathIterator.SEG_LINETO):
472             if (pathIndex == 1)
473               gs.moveTo(coords[0], coords[1]);
474             else
475               gs.lineTo(coords[0], coords[1]);
476             pathIndex++;
477             break;
478           case (PathIterator.SEG_QUADTO):
479             gs.quadTo(coords[0], coords[1], coords[2], coords[3]);
480             pathIndex++;
481             break;
482           case (PathIterator.SEG_CUBICTO):
483             gs.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
484             pathIndex++;
485             break;
486           case (PathIterator.SEG_CLOSE):
487             pathIndex++;
488             break;
489       }
490     }
491     return gs;
492   }
493   
494   public static GeneralShape addPathSegment(GeneralShape generalShape, int type, int index, float[] newCoords)
495   {
496     float[] coords = new float[6];
497     GeneralShape gs = new GeneralShape();
498     int pathIndex = 0;
499     boolean indexSet = false;
500     for (PathIterator JavaDoc pi = generalShape.getPathIterator(null); !pi.isDone(); pi.next())
501     {
502       if (pathIndex == index)
503       {
504         switch (type)
505         {
506             case (PathIterator.SEG_MOVETO):
507               gs.moveTo(newCoords[0], newCoords[1]);
508               break;
509             case (PathIterator.SEG_LINETO):
510               gs.lineTo(newCoords[0], newCoords[1]);
511               break;
512               case (PathIterator.SEG_QUADTO):
513                 gs.quadTo(newCoords[0], newCoords[1], newCoords[2], newCoords[3]);
514                 break;
515               case (PathIterator.SEG_CUBICTO):
516                 gs.curveTo(newCoords[0], newCoords[1], newCoords[2], newCoords[3], newCoords[4], newCoords[5]);
517                 break;
518         }
519         pathIndex = -1;
520         indexSet = true;
521       }
522       
523       int segType = pi.currentSegment(coords);
524       switch (segType)
525       {
526           case (PathIterator.SEG_MOVETO):
527             gs.moveTo(coords[0], coords[1]);
528             if (!indexSet)
529               pathIndex++;
530             break;
531           case (PathIterator.SEG_LINETO):
532             gs.lineTo(coords[0], coords[1]);
533             if (!indexSet)
534               pathIndex++;
535             break;
536           case (PathIterator.SEG_QUADTO):
537             gs.quadTo(coords[0], coords[1], coords[2], coords[3]);
538             if (!indexSet)
539               pathIndex++;
540             break;
541           case (PathIterator.SEG_CUBICTO):
542             gs.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
543             if (!indexSet)
544               pathIndex++;
545             break;
546           case (PathIterator.SEG_CLOSE):
547             gs.closePath();
548             if (!indexSet)
549               pathIndex++;
550             break;
551       }
552     }
553     return gs;
554   }
555     
556 }
557
558
Popular Tags