[Polycasso-commit] SF.net SVN: polycasso:[158] trunk/polycasso/src/com/mebigfatguy/polycasso/ JavaS
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-12-07 14:22:08
|
Revision: 158
http://polycasso.svn.sourceforge.net/polycasso/?rev=158&view=rev
Author: dbrosius
Date: 2009-12-07 14:21:59 +0000 (Mon, 07 Dec 2009)
Log Message:
-----------
finish the java saver
Modified Paths:
--------------
trunk/polycasso/src/com/mebigfatguy/polycasso/JavaSaver.java
Modified: trunk/polycasso/src/com/mebigfatguy/polycasso/JavaSaver.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/JavaSaver.java 2009-12-07 14:21:37 UTC (rev 157)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/JavaSaver.java 2009-12-07 14:21:59 UTC (rev 158)
@@ -18,16 +18,103 @@
*/
package com.mebigfatguy.polycasso;
+import java.awt.Color;
import java.awt.Dimension;
+import java.awt.Polygon;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import org.apache.commons.io.IOUtils;
+
public class JavaSaver implements Saver {
-
+
+ private static final String EXTENSION = ".java";
+ private static final String TABS = "\t\t\t\t\t\t";
+
@Override
public void save(String fileName, Dimension imageSize, PolygonData[] data)
throws IOException {
- // TODO Auto-generated method stub
+ InputStream templateStream = null;
+ PrintWriter pw = null;
+
+ int sep = fileName.lastIndexOf(File.separator);
+ String className;
+ if (sep >= 0)
+ className = fileName.substring(sep + 1);
+ else
+ className = fileName;
+
+ if (className.endsWith(EXTENSION)) {
+ className = className.substring(0, className.length() - EXTENSION.length());
+ }
+
+ try {
+ pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
+
+ templateStream = getClass().getResourceAsStream("/com/mebigfatguy/polycasso/JavaSaver.template");
+ String template = IOUtils.toString(templateStream);
+ StringWriter insert = new StringWriter();
+ PrintWriter polyWriter = new PrintWriter(insert);
+ for (PolygonData pd : data) {
+ polyWriter.print(TABS);
+ polyWriter.println("poly.reset();");
+ Polygon poly = pd.getPolygon();
+
+ for (int p = 0; p < poly.npoints; p++) {
+ polyWriter.print(TABS);
+ polyWriter.print("poly.addPoint(");
+ polyWriter.print(poly.xpoints[p]);
+ polyWriter.print(", ");
+ polyWriter.print(poly.ypoints[p]);
+ polyWriter.println(");");
+ }
+ Color c = pd.getColor();
+ polyWriter.print(TABS);
+ polyWriter.print("color = new Color(");
+ polyWriter.print(c.getRed());
+ polyWriter.print(", ");
+ polyWriter.print(c.getGreen());
+ polyWriter.print(", ");
+ polyWriter.print(c.getBlue());
+ polyWriter.println(");");
+
+ polyWriter.print(TABS);
+ polyWriter.println("g2d.setColor(color);");
+
+ polyWriter.print(TABS);
+ polyWriter.print("composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ");
+ polyWriter.print(pd.getAlpha());
+ polyWriter.println("f);");
+
+ polyWriter.print(TABS);
+ polyWriter.println("g2d.setComposite(composite);");
+
+ polyWriter.print(TABS);
+ polyWriter.println("g2d.fillPolygon(poly);");
+ }
+ polyWriter.flush();
+
+ /* All the curly braces confuses MessageFormat, so just do it manually */
+ template = template.replaceAll("\\{0\\}", className);
+ template = template.replaceAll("\\{1\\}", String.valueOf(imageSize.width));
+ template = template.replaceAll("\\{2\\}", String.valueOf(imageSize.height));
+ template = template.replaceAll("\\{3\\}", insert.toString());
+
+ pw.println(template);
+
+ } catch (IOException ioe) {
+
+ } finally {
+ IOUtils.closeQuietly(templateStream);
+ IOUtils.closeQuietly(pw);
+ }
+
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|