001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt.geom;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Color;
024: import java.awt.Graphics;
025: import java.awt.Graphics2D;
026: import java.awt.Polygon;
027: import java.awt.Rectangle;
028: import java.awt.Shape;
029: import java.awt.Stroke;
030: import java.awt.Tools;
031: import java.awt.geom.AffineTransform;
032: import java.awt.geom.Arc2D;
033: import java.awt.geom.Ellipse2D;
034: import java.awt.geom.GeneralPath;
035: import java.awt.geom.Line2D;
036: import java.awt.geom.Point2D;
037: import java.awt.geom.Rectangle2D;
038: import java.awt.geom.RectangularShape;
039: import java.awt.geom.RoundRectangle2D;
040: import java.awt.image.BufferedImage;
041: import java.io.File;
042:
043: public class ShapeProducer {
044:
045: Color colorBackground = Color.white;
046: Color colorShape = Color.gray;
047: Color colorFill = Color.lightGray;
048: Color colorGrid = new Color(0xA0, 0xA0, 0xA0);
049:
050: double[][][] rect = new double[][][] {
051: { { 50, 50, 100, 200 }, null, null },
052: { { 50, 50, 200, 100 }, { 1, 0, 0, 1, 20, 30 }, null }, };
053:
054: double[][][] round = new double[][][] {
055: { { 50, 50, 400, 200, 140, 100 }, null, null },
056: { { 50, 50, 400, 200, 140, 100 }, null, { 10 } },
057: { { 50, 50, 400, 200, 140, 100 }, null, { 5 } },
058: { { 50, 50, 200, 400, 100, 140 }, { 1, 0, 0, 1, 30, 20 },
059: null }, };
060:
061: double[][][] ellipse = new double[][][] {
062: { { 50, 50, 100, 200 }, null, null },
063: { { 50, 50, 200, 100 }, null, { 5 } },
064: { { 50, 50, 200, 100 }, null, { 10 } },
065: { { 50, 50, 200, 100 }, { 1, 0, 0, 1, 20, 30 }, null }, };
066:
067: double[][][] arc = new double[][][] {
068: { { 50, 50, 200, 200, 0, 60 }, null, null, { 0 } },
069: { { 50, 50, 400, 400, -50, 170 }, null, null, { 1 } },
070: { { 50, 50, 200, 200, 100, 200 }, null, null, { 2 } },
071: { { 50, 50, 400, 400, -150, 270 }, null, null, { 0 } },
072: { { 50, 50, 200, 200, 200, 359 }, null, null, { 1 } },
073: { { 50, 50, 200, 400, -250, 360 }, null, null, { 2 } },
074: { { 50, 50, 400, 200, 300, 361 }, null, null, { 0 } },
075: { { 50, 50, 400, 400, -350, 390 }, null, null, { 1 } },
076: { { 50, 50, 200, 200, 400, 710 }, null, null, { 2 } },
077: { { 50, 50, 200, 400, -450, 730 }, null, null, { 0 } },
078:
079: { { 50, 50, 400, 200, 0, -60 }, null, null, { 1 } },
080: { { 50, 50, 400, 400, 50, -170 }, null, null, { 2 } },
081: { { 50, 50, 200, 200, -100, -200 }, null, null, { 0 } },
082: { { 50, 50, 200, 400, 150, -270 }, null, null, { 1 } },
083: { { 50, 50, 400, 200, -200, -359 }, null, null, { 2 } },
084: { { 50, 50, 400, 400, 250, -360 }, null, null, { 0 } },
085: { { 50, 50, 200, 200, -300, -361 }, null, null, { 1 } },
086: { { 50, 50, 200, 400, 350, -390 }, null, null, { 2 } },
087: { { 50, 50, 400, 200, -400, -710 }, null, null, { 0 } },
088: { { 50, 50, 400, 400, 450, -730 }, null, null, { 1 } },
089:
090: { { 50, 50, 400, 400, 20, 330 }, null, null, { 2 } },
091: { { 50, 50, 400, 400, 20, -330 }, null, null, { 0 } },
092:
093: { { 50, 50, 200, 400, 40, -80 }, null, null, { 1 } },
094: { { 50, 50, 200, 400, 40, -80 }, { 1, 2, 2, 1, 0, 0 },
095: null, { 2 } },
096: { { 50, 50, 200, 400, 40, -80 }, { 2, 0, 0, 2, 0, 0 },
097: null, { 0 } },
098:
099: { { 50, 50, 200, 200, 0, 60 }, null, { 10 }, { 1 } },
100: { { 50, 50, 200, 200, 100, 200 }, null, { 20 }, { 2 } },
101: { { 50, 50, 400, 400, -150, 270 }, null, { 30 }, { 0 } },
102: { { 50, 50, 400, 400, 20, 330 }, null, { 40 }, { 1 } },
103: { { 50, 50, 400, 400, 20, 330 }, null, { 10 }, { 2 } }, };
104:
105: double[][][] quad = new double[][][] {
106: { { 50, 50, 250, 100, 150, 250 }, null, null },
107: { { 250, 50, 50, 250, 200, 200 }, null, null } };
108:
109: double[][][] cubic = new double[][][] {
110: { { 50, 50, 150, 150, 250, 150, 350, 60 }, null, null },
111: { { 50, 50, 150, 200, 300, 320, 200, 100 }, null, null },
112: { { 300, 50, 50, 200, 300, 50, 350, 150 }, null, null },
113: { { 150, 250, 50, 250, 250, 50, 250, 200 }, null, null },
114: { { 450, 100, 50, 250, 150, 50, 300, 250 }, null, null },
115: { { 50, 300, 150, 250, 150, 100, 300, 50 }, null, null },
116: { { 50, 50, 100, 150, 250, 100, 50, 300 }, null, null }, };
117:
118: double[][][] polygon = new double[][][] {
119: { { 50, 50, 150, 250, 250, 150 }, null, null },
120: { { 50, 50, 150, 50, 150, 150, 50, 150 }, null, null },
121: { { 50, 300, 250, 150, 100, 100, 200, 250, 200, 50 }, null,
122: null },
123: {
124: { 50, 50, 200, 50, 200, 150, 100, 150, 100, 100,
125: 150, 100, 150, 200, 50, 200 }, null, null }, };
126:
127: String path;
128:
129: public ShapeProducer(String path) {
130: if (!path.endsWith(File.separator)) {
131: path += File.separator;
132: }
133: this .path = path;
134: /*
135: createRect();
136: createRound();
137: createEllipse();
138: createArc();
139: createQuad();
140: createCubic();
141: */
142: createPolygon();
143: }
144:
145: AffineTransform createTransform(double[] matrix) {
146: return matrix == null ? null : new AffineTransform(matrix);
147: }
148:
149: Rectangle2D createRect(double[] matrix) {
150: return new Rectangle2D.Double(matrix[0], matrix[1], matrix[2],
151: matrix[3]);
152: }
153:
154: RoundRectangle2D createRound(double[] matrix) {
155: return new RoundRectangle2D.Double(matrix[0], matrix[1],
156: matrix[2], matrix[3], matrix[4], matrix[5]);
157: }
158:
159: Ellipse2D createEllipse(double[] matrix) {
160: return new Ellipse2D.Double(matrix[0], matrix[1], matrix[2],
161: matrix[3]);
162: }
163:
164: Arc2D createArc(double[] matrix, int type) {
165: return new Arc2D.Double(matrix[0], matrix[1], matrix[2],
166: matrix[3], matrix[4], matrix[5], type);
167: }
168:
169: QuadCurve2D createQuad(double[] matrix) {
170: return new QuadCurve2D.Double(matrix[0], matrix[1], matrix[2],
171: matrix[3], matrix[4], matrix[5]);
172: }
173:
174: CubicCurve2D createCubic(double[] matrix) {
175: return new CubicCurve2D.Double(matrix[0], matrix[1], matrix[2],
176: matrix[3], matrix[4], matrix[5], matrix[6], matrix[7]);
177: }
178:
179: Polygon createPolygon(double[] matrix) {
180: Polygon p = new Polygon();
181: for (int i = 0; i < matrix.length;) {
182: p.addPoint((int) matrix[i++], (int) matrix[i++]);
183: }
184: return p;
185: }
186:
187: void createRect() {
188: for (double[][] element : rect) {
189: saveRectShape(path, createRect(element[0]),
190: createTransform(element[1]),
191: createFlatness(element[2]));
192: }
193: }
194:
195: void createRound() {
196: for (double[][] element : round) {
197: saveRectShape(path, createRound(element[0]),
198: createTransform(element[1]),
199: createFlatness(element[2]));
200: }
201: }
202:
203: void createEllipse() {
204: for (double[][] element : ellipse) {
205: saveRectShape(path, createEllipse(element[0]),
206: createTransform(element[1]),
207: createFlatness(element[2]));
208: }
209: }
210:
211: void createArc() {
212: for (double[][] element : arc) {
213: for (int j = 0; j < element[3].length; j++) {
214: saveRectShape(path, createArc(element[0],
215: (int) element[3][j]),
216: createTransform(element[1]),
217: createFlatness(element[2]));
218: }
219: }
220: }
221:
222: void createQuad() {
223: for (double[][] element : quad) {
224: saveRectShape(path, createQuad(element[0]),
225: createTransform(element[1]),
226: createFlatness(element[2]));
227: }
228: }
229:
230: void createCubic() {
231: for (double[][] element : cubic) {
232: saveRectShape(path, createCubic(element[0]),
233: createTransform(element[1]),
234: createFlatness(element[2]));
235: }
236: }
237:
238: void createPolygon() {
239: for (double[][] element : polygon) {
240: saveRectShape(path, createPolygon(element[0]),
241: createTransform(element[1]),
242: createFlatness(element[2]));
243: }
244: }
245:
246: String doubleToStr(double a) {
247: if (a == (int) a) {
248: return "" + (int) a;
249: }
250: return "" + a;
251: }
252:
253: Point2D transform(AffineTransform at, Point2D p) {
254: return at == null ? p : at.transform(p, null);
255: }
256:
257: void saveRectShape(String outputPath, Shape shape,
258: AffineTransform at, double flatness) {
259: PathIterator path = flatness < 0.0 ? shape.getPathIterator(at)
260: : shape.getPathIterator(at, flatness);
261: GeneralPath src = new GeneralPath(path.getWindingRule());
262: src.append(path, false);
263:
264: Rectangle r = src.getBounds();
265: int width = (int) (r.getX() + r.getWidth()) + 50;
266: int height = (int) (r.getY() + r.getHeight()) + 50;
267:
268: BufferedImage img = new BufferedImage(width, height,
269: BufferedImage.TYPE_INT_RGB);
270:
271: Graphics g = img.getGraphics();
272:
273: // Background
274: g.setColor(colorBackground);
275: g.fillRect(0, 0, img.getWidth(), img.getHeight());
276:
277: // Fill shape
278: /*
279: g.setColor(colorFill);
280: ((Graphics2D)g).fill(src);
281: */
282: for (int x = 0; x < img.getWidth(); x++) {
283: for (int y = 0; y < img.getHeight(); y++) {
284: if (x == 120 && y == 120) {
285: System.out.println("test");
286: }
287: if (src.contains(x, y)) {
288: img.setRGB(x, y, colorFill.getRGB());
289: }
290: }
291: }
292:
293: // Draw shape
294: Stroke oldStroke = ((Graphics2D) g).getStroke();
295: ((Graphics2D) g).setStroke(new BasicStroke(4.0f));
296: g.setColor(colorShape);
297: ((Graphics2D) g).draw(src);
298:
299: if (shape instanceof Arc2D) {
300: Arc2D arc = (Arc2D) shape;
301: switch (arc.getArcType()) {
302: case Arc2D.PIE:
303: // Exclude center of pie
304: Point2D p = transform(at, new Point2D.Double(
305: ((Arc2D) shape).getCenterX(), ((Arc2D) shape)
306: .getCenterY()));
307: g.fillOval((int) (p.getX() - 3), (int) (p.getY() - 3),
308: 6, 6);
309: break;
310: case Arc2D.OPEN:
311: Point2D p1 = transform(at, arc.getStartPoint());
312: Point2D p2 = transform(at, arc.getEndPoint());
313: // Exclude span
314: ((Graphics2D) g).draw(new Line2D.Double(p1, p2));
315: break;
316: }
317: }
318: if (shape instanceof QuadCurve2D) {
319: QuadCurve2D c = (QuadCurve2D) shape;
320: ((Graphics2D) g).draw(new Line2D.Double(c.getP1(), c
321: .getP2()));
322: }
323: if (shape instanceof CubicCurve2D) {
324: CubicCurve2D c = (CubicCurve2D) shape;
325: ((Graphics2D) g).draw(new Line2D.Double(c.getP1(), c
326: .getP2()));
327: }
328:
329: ((Graphics2D) g).setStroke(oldStroke);
330: g.setXORMode(colorGrid);
331: for (int x = 0; x < img.getWidth(); x += 50) {
332: g.drawLine(x, 0, x, img.getHeight());
333: }
334: for (int y = 0; y < img.getHeight(); y += 50) {
335: g.drawLine(0, y, img.getWidth(), y);
336: }
337: g.setPaintMode();
338:
339: String shapeDesc = getShapeDesc(shape);
340:
341: System.out.println(outputPath + shapeDesc);
342:
343: if (at != null) {
344: shapeDesc += "_" + getAffineDesc(at);
345: }
346:
347: if (flatness >= 0.0) {
348: shapeDesc += "_flat(" + doubleToStr(flatness) + ")";
349: }
350:
351: Tools.Shape.save(src, outputPath + shapeDesc + ".shape");
352: Tools.BufferedImage.saveIcon(img, outputPath + shapeDesc
353: + ".ico");
354:
355: }
356:
357: String getAffineDesc(AffineTransform at) {
358: return "affine(" + doubleToStr(at.getScaleX()) + ","
359: + doubleToStr(at.getShearY()) + ","
360: + doubleToStr(at.getShearX()) + ","
361: + doubleToStr(at.getScaleY()) + ","
362: + doubleToStr(at.getTranslateX()) + ","
363: + doubleToStr(at.getTranslateY()) + ")";
364: }
365:
366: String getShapeDesc(Shape shape) {
367: String shapeDesc = null;
368:
369: if (shape instanceof RectangularShape) {
370: RectangularShape rshape = (RectangularShape) shape;
371: shapeDesc = doubleToStr(rshape.getX()) + ","
372: + doubleToStr(rshape.getY()) + ","
373: + doubleToStr(rshape.getWidth()) + ","
374: + doubleToStr(rshape.getHeight());
375:
376: if (shape instanceof Ellipse2D) {
377: shapeDesc = "ellipse(" + shapeDesc + ")";
378: } else if (shape instanceof Rectangle2D) {
379: shapeDesc = "rect(" + shapeDesc + ")";
380: } else if (shape instanceof RoundRectangle2D) {
381: RoundRectangle2D rr = (RoundRectangle2D) shape;
382: shapeDesc = "round(" + shapeDesc + ","
383: + doubleToStr(rr.getArcWidth()) + ","
384: + doubleToStr(rr.getArcHeight()) + ")";
385: } else if (shape instanceof Arc2D) {
386: Arc2D a = (Arc2D) shape;
387: shapeDesc = "arc(" + shapeDesc + ","
388: + doubleToStr(a.getAngleStart()) + ","
389: + doubleToStr(a.getAngleExtent());
390:
391: switch (a.getArcType()) {
392: case Arc2D.CHORD:
393: shapeDesc += ",CHORD)";
394: break;
395: case Arc2D.OPEN:
396: shapeDesc += ",OPEN)";
397: break;
398: case Arc2D.PIE:
399: shapeDesc += ",PIE)";
400: break;
401: }
402: }
403: } else if (shape instanceof QuadCurve2D) {
404: QuadCurve2D c = (QuadCurve2D) shape;
405: shapeDesc = "quad(" + doubleToStr(c.getX1()) + ","
406: + doubleToStr(c.getY1()) + ","
407: + doubleToStr(c.getCtrlX()) + ","
408: + doubleToStr(c.getCtrlY()) + ","
409: + doubleToStr(c.getX2()) + ","
410: + doubleToStr(c.getY2()) + ")";
411: } else if (shape instanceof CubicCurve2D) {
412: CubicCurve2D c = (CubicCurve2D) shape;
413: shapeDesc = "cubic(" + doubleToStr(c.getX1()) + ","
414: + doubleToStr(c.getY1()) + ","
415: + doubleToStr(c.getCtrlX1()) + ","
416: + doubleToStr(c.getCtrlY1()) + ","
417: + doubleToStr(c.getCtrlX2()) + ","
418: + doubleToStr(c.getCtrlY2()) + ","
419: + doubleToStr(c.getX2()) + ","
420: + doubleToStr(c.getY2()) + ")";
421: } else if (shape instanceof Polygon) {
422: Polygon p = (Polygon) shape;
423: shapeDesc = "polygon(";
424: for (int i = 0; i < p.npoints; i++) {
425: shapeDesc += p.xpoints[i] + "," + p.ypoints[i];
426: if (i < p.npoints - 1) {
427: shapeDesc += ",";
428: }
429: }
430: shapeDesc += ")";
431: }
432:
433: return shapeDesc;
434: }
435:
436: double createFlatness(double[] buf) {
437: return buf == null ? -1.0 : buf[0];
438: }
439:
440: public static void main(String[] args) {
441: if (args.length < 1) {
442: System.out.println("ShapeProduccer [outputpath]");
443: return;
444: }
445: new ShapeProducer(args[0]);
446: }
447: }
|