Skip to content

Commit

Permalink
增加了配置文件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhoujay committed May 14, 2017
1 parent a319433 commit 8b4944e
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 1 addition & 17 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 2 additions & 0 deletions Lowpoly/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'Lowpoly'

41 changes: 41 additions & 0 deletions Lowpoly/src/main/java/com/zzhoujay/lowpoly/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.zzhoujay.lowpoly;

/**
* Created by zhou on 2017/5/14.
*/
public class Configuration {

public static final int DEFAULT_ACCURACY = 50;
public static final float DEFAULT_SCALE = 1;
public static final boolean DEFAULT_FILL = true;
public static final String DEFAULT_FORMAT = "png";
public static final boolean DEFAULT_ANTI_ALIASING = false;
public static final int DEFAULT_POINT_COUNT = 300;

public static final String ACCURACY = "accuracy";
public static final String SCALE = "scale";
public static final String FILL = "fill";
public static final String FORMAT = "format";
public static final String ANTI_ALIASING = "antiAliasing";
public static final String POINT_COUNT = "pointCount";

public final int accuracy;
public final float scale;
public final boolean fill;
public final String format;
public final boolean antiAliasing;
public final int pointCount;

public Configuration() {
this(DEFAULT_ACCURACY, DEFAULT_SCALE, DEFAULT_FILL, DEFAULT_FORMAT, DEFAULT_ANTI_ALIASING, DEFAULT_POINT_COUNT);
}

public Configuration(int accuracy, float scale, boolean fill, String format, boolean antiAliasing, int pointCount) {
this.accuracy = accuracy;
this.scale = scale;
this.fill = fill;
this.format = format;
this.antiAliasing = antiAliasing;
this.pointCount = pointCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
public final class LowPoly {

public static void generate(InputStream inputStream, OutputStream outputStream) throws IOException {
generate(inputStream, outputStream, 50, 1, true, "png", false, 300);
public static void generate(InputStream inputStream, OutputStream outputStream, Configuration configuration) throws IOException {
generate(inputStream, outputStream, configuration.accuracy, configuration.scale, configuration.fill, configuration.format, configuration.antiAliasing, configuration.pointCount);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions Lowpoly/src/main/java/com/zzhoujay/lowpoly/Simple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.zzhoujay.lowpoly;

import java.io.*;
import java.util.Properties;

/**
* Created by zhou on 16-5-12.
*/
public class Simple {

public static void main(String[] args) throws IOException {
if (args.length <= 0) {
return;
}
if (args.length == 1) {
FileOutputStream fileOutputStream = new FileOutputStream(args[0]);
createConfiguration(fileOutputStream);
System.out.println("默认配置文件已创建");
return;
}
String in = args[0];
String out = args[1];
File inFile = new File(in);
if (inFile.exists()) {
File outFile = new File(out);
FileInputStream inputStream = new FileInputStream(inFile);
FileOutputStream outputStream = new FileOutputStream(outFile);
long lastTime = System.currentTimeMillis();
Configuration configuration;
if (args.length >= 3) {
FileInputStream fileInputStream = new FileInputStream(args[2]);
configuration = loadConfiguration(fileInputStream);
} else {
configuration = new Configuration();
}

LowPoly.generate(inputStream, outputStream, configuration);
System.out.println("目标已保存至:" + outFile.getAbsolutePath());
System.out.println("用时:" + (System.currentTimeMillis() - lastTime));
} else {
System.out.println("源文件不存在");
}
}

private static void createConfiguration(OutputStream outputStream) throws IOException {
Properties properties = new Properties();
properties.setProperty(Configuration.ACCURACY, String.valueOf(Configuration.DEFAULT_ACCURACY));
properties.setProperty(Configuration.SCALE, String.valueOf(Configuration.DEFAULT_SCALE));
properties.setProperty(Configuration.FILL, String.valueOf(Configuration.DEFAULT_FILL));
properties.setProperty(Configuration.FORMAT, Configuration.DEFAULT_FORMAT);
properties.setProperty(Configuration.ANTI_ALIASING, String.valueOf(Configuration.DEFAULT_ANTI_ALIASING));
properties.setProperty(Configuration.POINT_COUNT, String.valueOf(Configuration.DEFAULT_POINT_COUNT));
properties.store(outputStream, "default");
}

private static Configuration loadConfiguration(InputStream inputStream) throws IOException {
Properties properties = new Properties();
properties.load(inputStream);

String accuracy = properties.getProperty(Configuration.ACCURACY, String.valueOf(Configuration.DEFAULT_ACCURACY));
String scale = properties.getProperty(Configuration.SCALE, String.valueOf(Configuration.DEFAULT_SCALE));
String fill = properties.getProperty(Configuration.FILL, String.valueOf(Configuration.DEFAULT_FILL));
String format = properties.getProperty(Configuration.FORMAT, Configuration.DEFAULT_FORMAT);
String antiAliasing = properties.getProperty(Configuration.ANTI_ALIASING, String.valueOf(Configuration.DEFAULT_ANTI_ALIASING));
String pointCount = properties.getProperty(Configuration.POINT_COUNT, String.valueOf(Configuration.DEFAULT_POINT_COUNT));

return new Configuration(Integer.valueOf(accuracy), Float.valueOf(scale), Boolean.valueOf(fill), format, Boolean.valueOf(antiAliasing), Integer.valueOf(pointCount));
}
}
42 changes: 0 additions & 42 deletions src/main/java/com/zzhoujay/lowpoly/Simple.java

This file was deleted.

0 comments on commit 8b4944e

Please sign in to comment.