Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/test/TransformTest2.java


1   package com.arranger.jarl.test;
2   
3   import com.arranger.jarl.util.IOUtil;
4   import com.arranger.jarl.util.WidgetUtil;
5   
6   import java.awt.*;
7   import java.awt.font.FontRenderContext;
8   import java.awt.geom.*;
9   import java.io.File;
10  import java.util.ArrayList;
11  
12  /**
13   * TransformTest2 created on Feb 28, 2003
14   */
15  public class TransformTest2 extends TransformTest1 {
16  
17      protected static final double AMPLITUDE = 10.0;
18      protected static final double FREQUENCY = 10.0;
19      protected static final double DEFAULT_PRECISION = .1;
20  
21      public void testTransform() throws Exception {
22          new File("unit").mkdirs();
23          int yOffset = 0;
24          for (int index = 17; index <= 17; index++) {
25              System.out.println("index: " + index);
26              Image image = createImage();
27              Graphics2D graphics2D = (Graphics2D)image.getGraphics();
28              Shape sourceShape = getShape(index);
29              graphics2D.setPaint(Color.white);
30              graphics2D.draw(centerShape(sourceShape));
31  
32              //transform
33              Point2D[] resultPoints = transform(sourceShape, DEFAULT_PRECISION, graphics2D, yOffset += 20);
34              Shape resulShape = WidgetUtil.pointsToShape(resultPoints, false);
35              graphics2D.setPaint(Color.blue);
36              graphics2D.draw(centerShape(resulShape));
37  
38              IOUtil.save(image, new File("unit/transformTest" + index + ".bmp"));
39          }
40          //WidgetUtil.dumpPoints(resultPoints);
41      }
42  
43      public void testLinearTransform() throws Exception {
44          new File("unit").mkdirs();
45          int yOffset = 0;
46          for (int index = 0; index <= 360; index += 45) {
47              System.out.println("index: " + index);
48              Image image = createImage();
49              Graphics2D graphics2D = (Graphics2D)image.getGraphics();
50  
51              Shape shape = new Arc2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2, 0, index, Arc2D.OPEN);
52              Point2D[] points = WidgetUtil.getPoints(shape);
53  
54              GeneralPath generalPath = new GeneralPath();
55              generalPath.moveTo(0, 0);
56              generalPath.lineTo((float)points[points.length - 1].getX(), (float)points[points.length - 1].getY());
57              generalPath.lineTo(m_imageWidth / 2, m_imageHeight / 2);
58              Shape sourceShape = generalPath;
59  
60              graphics2D.setPaint(Color.white);
61              graphics2D.draw(centerShape(sourceShape));
62  
63              //transform
64  
65              Point2D[] resultPoints = transform(sourceShape, DEFAULT_PRECISION, graphics2D, yOffset += 20);
66              Shape resultShape = WidgetUtil.pointsToShape(resultPoints, false);
67              graphics2D.setPaint(Color.blue);
68              graphics2D.draw(centerShape(resultShape));
69  
70              IOUtil.save(image, new File("unit/transformLinearTest" + index + ".bmp"));
71          }
72      }
73  
74      protected Point2D[] transform(Shape sourceShape, double precision, Graphics2D graphics2D, int offset) {
75          Point2D[] shapePoints = WidgetUtil.getPoints(sourceShape);
76          //WidgetUtil.dumpPoints(shapePoints);
77  
78          java.util.List points = new ArrayList();
79          double totalX = 0;
80          for (int sourceIndex = 0; sourceIndex < shapePoints.length - 1; sourceIndex++) {
81              double x1 = shapePoints[sourceIndex].getX();
82              double y1 = shapePoints[sourceIndex].getY();
83              double x2 = shapePoints[sourceIndex + 1].getX();
84              double y2 = shapePoints[sourceIndex + 1].getY();
85  
86              /** debug */
87              Point2D origStart = new Point2D.Double(x1, y1);
88              Point2D origEnd = new Point2D.Double(x2, y2);
89              graphics2D.setPaint(Color.white);
90              paintPoints(origStart, origEnd, "orig: ", 0, (sourceIndex * 20) + offset, graphics2D);
91              /** debug */
92  
93              double distance = Point2D.distance(x1, y1, x2, y2);
94              double[] preTransPoints = new double[(int)((distance / precision) + 1) * 2];
95  
96              /** pluggable function */
97              int writeIndex = 0;
98              double x = 0;
99              for (x = 0; x <= distance; x += precision) {
100                 double degrees = (totalX + x) * FREQUENCY;
101                 double y = Math.sin(Math.toRadians(degrees)) * AMPLITUDE;
102                 preTransPoints[writeIndex++] = x;
103                 preTransPoints[writeIndex++] = y;
104             }
105             /** pluggable function */
106 
107             //filter out dups...
108             int pointLength = writeIndex;
109             while (preTransPoints[0] == preTransPoints[pointLength - 2] &&
110                 preTransPoints[1] == preTransPoints[pointLength - 1]) {
111                 pointLength -= 2;
112             }
113             System.out.println("num pretans points: " + pointLength);
114             System.out.println(
115                 preTransPoints[0] + ", " +
116                 preTransPoints[1] + " -- > " +
117                 preTransPoints[pointLength - 2] + ", " +
118                 preTransPoints[pointLength - 1]
119             );
120 
121             //got the simple points, now rotate and translate
122             double rads = findRadians(x1, y1, x2, y2);
123             double degrees = Math.toDegrees(rads) % 360;
124             //System.out.println(degrees);
125             if (x2 < x1 && y2 < y1) {
126             } else if (x2 < x1 && degrees < 180) {
127                 degrees = 180 - degrees;
128                 rads = Math.toRadians(degrees);
129             } else if (y2 < y1 && degrees > 180) {
130                 degrees = 180 - degrees;
131                 rads = Math.toRadians(degrees);
132             }
133 
134             AffineTransform affineTransform = AffineTransform.getTranslateInstance(x1, y1);
135             affineTransform.concatenate(AffineTransform.getRotateInstance(rads));
136 
137             //apply the transformation to the points
138             double[] dstPoints = new double[pointLength];
139             affineTransform.transform(preTransPoints, 0, dstPoints, 0, pointLength / 2);
140             for (int index = 0; index < dstPoints.length; index += 2) {
141                 points.add(new Point2D.Double(dstPoints[index], dstPoints[index + 1]));
142             }
143 
144             int removeIndex = 0;
145             while (points.get(0).equals(points.get(points.size() - 1))) {
146                 points.remove(points.size() - 1);
147                 removeIndex++;
148             }
149 
150             /** debug */
151             int yOffset = 375;
152             int baseIndex = pointLength;
153             baseIndex -= (removeIndex * 2);
154             Point2D preStart = new Point2D.Double(preTransPoints[0], preTransPoints[1]);
155             Point2D preEnd = new Point2D.Double(preTransPoints[baseIndex - 2], preTransPoints[baseIndex - 1]);
156 
157             Point2D postStart = new Point2D.Double(dstPoints[0], dstPoints[1]);
158             Point2D postEnd = new Point2D.Double(dstPoints[baseIndex - 2], dstPoints[baseIndex - 1]);
159 
160             graphics2D.setPaint(Color.blue);
161             paintPoints(preStart, preEnd, "pre: ", 0, (sourceIndex * 20) + offset + yOffset, graphics2D);
162             paintPoints(postStart, postEnd, "post: ", m_imageWidth * .5, (sourceIndex * 20) + yOffset + offset, graphics2D);
163             /** debug */
164 
165             totalX += distance;
166         }
167 
168         Point2D[] resultPoints = new Point2D[points.size()];
169         resultPoints = (Point2D[])points.toArray(resultPoints);
170 
171         return resultPoints;
172     }
173 
174     protected void paintPoints(Point2D point2D, Point2D point2D2, String prefix, double xOffset, double yOffset, Graphics2D graphics2D) {
175         double rads = findRadians(point2D.getX(), point2D.getY(), point2D2.getX(), point2D2.getY());
176         int degrees = (int)Math.toDegrees(rads);
177         paintText(graphics2D, prefix + getPointText(point2D) +
178             " --> " +
179             getPointText(point2D2) +
180             " --> angle: " + degrees +
181             " --> distance: " + (int)(Point2D.distance(point2D.getX(), point2D.getY(), point2D2.getX(), point2D2.getY())),
182             20 + xOffset, yOffset);
183     }
184 
185     protected String getPointText(Point2D point2D) {
186         return "[" + (int)(point2D.getX()) + ", " + (int)(point2D.getY()) + "]";
187     }
188 
189     protected void paintText(Graphics2D graphics2D, String text, double x, double y) {
190         FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
191         Font font = new Font(null, Font.PLAIN, 11);
192         Shape shape = font.createGlyphVector(fontRenderContext, text).getOutline();
193 
194         AffineTransform affineTransform = AffineTransform.getTranslateInstance(x, y);
195         shape = affineTransform.createTransformedShape(shape);
196 
197         graphics2D.fill(shape);
198     }
199 
200     protected double findRadians(double x1, double y1, double x2, double y2) {
201         if (x1 < x2 && y1 <= y2) {
202             double tmp = (y1 - y2) / (x1 - x2);
203             return Math.atan(tmp);
204         } else if (x1 >= x2 && y1 < y2) {
205             double tmp = (x1 - x2) / (y1 - y2);
206             return Math.atan(tmp) + (Math.PI / 2);
207         } else if (x1 > x2 && y1 >= y2) {
208             double tmp = (y1 - y2) / (x1 - x2);
209             return Math.atan(tmp) + Math.PI;
210         } else if (x1 <= x2 && y1 > y2) {
211             double tmp = (x1 - x2) / (y1 - y2);
212             return Math.atan(tmp) + (Math.PI * 3 / 2);
213         } else {
214             return 0;
215         }
216     }
217 
218     protected Shape getShape(int index) {
219 
220         switch (index) {
221             case 0:
222                 return new Line2D.Double(
223                     0 + m_imageWidth * .25,
224                     0 + m_imageHeight * .25,
225                     0 - m_imageWidth * .25,
226                     0 - m_imageHeight * .25);             //36work
227             case 1:
228                 return new Line2D.Double(
229                     0 + m_imageWidth * .25,
230                     0 - m_imageHeight * .25,
231                     0 - m_imageWidth * .25,
232                     0 + m_imageHeight * .25);               //216NoWork
233             case 2:
234                 return new Line2D.Double(
235                     0 - m_imageWidth * .25,
236                     0 + m_imageHeight * .25,
237                     0 + m_imageWidth * .25,
238                     0 - m_imageHeight * .25);               //-216Work
239             case 3:
240                 return new Line2D.Double(
241                     0 - m_imageWidth * .25,
242                     0 - m_imageHeight * .25,
243                     0 + m_imageWidth * .25,
244                     0 + m_imageHeight * .25);              //-36NoWork
245             case 4:
246                 //-36 & -36
247                 GeneralPath generalPath = new GeneralPath();
248                 generalPath.moveTo((float)(0 - m_imageWidth * .25), (float)(0 - m_imageHeight * .25));
249                 generalPath.lineTo((float)(0 - m_imageWidth * 0), (float)(0 - m_imageHeight * 0));
250                 generalPath.lineTo((float)(0 + m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
251                 return generalPath;
252             case 5:
253                 //-36 & -216
254                 generalPath = new GeneralPath();
255                 generalPath.moveTo((float)(0 - m_imageWidth * .25), (float)(0 - m_imageHeight * .25));
256                 generalPath.lineTo((float)(0 - m_imageWidth * 0), (float)(0 - m_imageHeight * 0));
257                 generalPath.lineTo((float)(0 + m_imageWidth * .25), (float)(0 - m_imageHeight * .25));
258                 return generalPath;
259             case 6:
260                 //-216 && -36
261                 generalPath = new GeneralPath();
262                 generalPath.moveTo((float)(0 - m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
263                 generalPath.lineTo((float)(0 - m_imageWidth * 0), (float)(0 - m_imageHeight * 0));
264                 generalPath.lineTo((float)(0 + m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
265                 return generalPath;
266             case 7:
267                 //36 && 216
268                 generalPath = new GeneralPath();
269                 generalPath.moveTo((float)(0 + m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
270                 generalPath.lineTo((float)(0 - m_imageWidth * 0), (float)(0 - m_imageHeight * 0));
271                 generalPath.lineTo((float)(0 - m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
272                 return generalPath;
273             case 8:
274                 //36 --> 0, 180 --> 216
275                 generalPath = new GeneralPath();
276                 generalPath.moveTo((float)(0 + m_imageWidth * .25), (float)(0 + m_imageHeight * .25));
277                 generalPath.quadTo(
278                     (float)(0 - m_imageWidth * 0),
279                     (float)(0 - m_imageHeight * 0),
280                     (float)(0 - m_imageWidth * .25),
281                     (float)(0 + m_imageHeight * .25));
282                 return generalPath;
283             case 9:
284                 return new Arc2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2, 180, -180, Arc2D.OPEN);
285             case 10:
286                 return new Arc2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2, 180, 180, Arc2D.OPEN);
287             case 11:
288                 return new Arc2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2, 270, 180, Arc2D.OPEN);
289             case 12:
290                 return new Arc2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2, 90, 180, Arc2D.OPEN);
291             case 13:        //vertical1
292                 return new Line2D.Double(
293                     0 - m_imageWidth * 0,
294                     0 - m_imageHeight * .25,
295                     0 - m_imageWidth * 0,
296                     0 + m_imageHeight * .25);
297             case 14:        //vertical2
298                 return new Line2D.Double(
299                     0 - m_imageWidth * 0,
300                     0 + m_imageHeight * .25,
301                     0 - m_imageWidth * 0,
302                     0 - m_imageHeight * .25);
303             case 15:        //horiz1
304                 return new Line2D.Double(
305                     0 - m_imageWidth * .25,
306                     0 + m_imageHeight * .0,
307                     0 + m_imageWidth * .25,
308                     0 + m_imageHeight * 0);
309             case 16:        //horiz2
310                 return new Line2D.Double(
311                     0 + m_imageWidth * .25,
312                     0 + m_imageHeight * .0,
313                     0 - m_imageWidth * .25,
314                     0 - m_imageHeight * 0);
315             case 17:        //rectangle
316                 return new Rectangle2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2);
317             case 18:
318                 return new Ellipse2D.Double(0, 0, m_imageWidth / 2, m_imageHeight / 2);
319             default:
320                 return null;
321         }
322     }
323 }