Skip to content

Commit 1661740

Browse files
committed
cleaned code
1 parent 09cead0 commit 1661740

File tree

2 files changed

+79
-60
lines changed

2 files changed

+79
-60
lines changed

.classpath

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="lib" path="lib/j3dcore.jar"/>
6+
<classpathentry kind="lib" path="lib/j3dutils.jar"/>
7+
<classpathentry kind="lib" path="lib/vecmath.jar"/>
58
<classpathentry kind="output" path="bin"/>
69
</classpath>

src/ee/liiser/siim/galaxies/Main.java

Lines changed: 76 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -48,73 +48,21 @@ public class Main {
4848

4949
public static void main(String[] args) {
5050
Galaxy[] galaxies = null;
51-
52-
if(args.length >= 2){
53-
try {
54-
BufferedReader r = new BufferedReader(new FileReader(args[1]));
55-
String line;
56-
do{
57-
line = r.readLine();
58-
}while(line != null && line.startsWith("#"));
59-
r.close();
60-
String[] params = line.split(",");
61-
if(params[0].toLowerCase().equals("velocity")){
62-
METHOD = Method.VELOCITY_VERLET;
63-
}else if(params[0].toLowerCase().equals("basic")){
64-
METHOD = Method.BASIC_VERLET;
65-
}else{
66-
throw new IllegalArgumentException(params[0] + " is not a method in " + args[1]);
67-
}
68-
Calculator.dt = Double.parseDouble(params[1]);
69-
GraphicsThread.fps = Double.parseDouble(params[2]);
70-
71-
} catch (FileNotFoundException e) {
72-
e.printStackTrace();
73-
} catch (IOException e) {
74-
e.printStackTrace();
75-
}
76-
}
77-
51+
52+
if (args.length >= 2)
53+
setConf(args);
54+
7855
ObjectFactory factory = new ObjectFactory(METHOD);
79-
80-
56+
8157
if (args.length >= 1) {
82-
83-
try {
84-
BufferedReader r = new BufferedReader(new FileReader(args[0]));
85-
ArrayList<Galaxy> list = new ArrayList<Galaxy>();
86-
while(true){
87-
String line = r.readLine();
88-
if(line == null) break;
89-
if(line.isEmpty()) break;
90-
if(line.startsWith("#")) continue;
91-
92-
list.add(factory.makeGalaxy(line));
93-
94-
}
95-
r.close();
96-
galaxies = new Galaxy[list.size()];
97-
galaxies = list.toArray(galaxies);
98-
} catch (FileNotFoundException e) {
99-
e.printStackTrace();
100-
} catch (IOException e) {
101-
e.printStackTrace();
102-
}
103-
58+
galaxies = makeGalaxies(args, factory);
10459
} else {
105-
// Default galaxy configuration
106-
Galaxy galaxy1 = factory.makeGalaxy(new Vector3d(), new Vector3d(),
107-
new Vector3d(0, 0, 1), 1, STARCOUNT);
108-
Galaxy galaxy2 = factory.makeGalaxy(new Vector3d(0, 5, -30),
109-
new Vector3d(0, 0, 0.5), new Vector3d(0, 1, 0), 1,
110-
STARCOUNT);
111-
112-
galaxies = new Galaxy[] { galaxy1, galaxy2 };
60+
galaxies = makeDefaultGalaxies(factory);
11361
}
11462
run(galaxies);
11563

11664
}
117-
65+
11866
/**
11967
* Main entry point of the application. Call this with an array of galaxies
12068
* to simulate their movement
@@ -144,4 +92,72 @@ public static void run(Galaxy[] galaxies) {
14492
t.start();
14593
}
14694

95+
private static Galaxy[] makeDefaultGalaxies(ObjectFactory factory) {
96+
// Default galaxy configuration
97+
Galaxy galaxy1 = factory.makeGalaxy(new Vector3d(), new Vector3d(),
98+
new Vector3d(0, 0, 1), 1, STARCOUNT);
99+
Galaxy galaxy2 = factory.makeGalaxy(new Vector3d(0, 5, -30),
100+
new Vector3d(0, 0, 0.5), new Vector3d(0, 1, 0), 1, STARCOUNT);
101+
102+
return new Galaxy[] { galaxy1, galaxy2 };
103+
}
104+
105+
private static Galaxy[] makeGalaxies(String[] args, ObjectFactory factory) {
106+
107+
try {
108+
BufferedReader r = new BufferedReader(new FileReader(args[0]));
109+
ArrayList<Galaxy> list = new ArrayList<Galaxy>();
110+
while (true) {
111+
String line = r.readLine();
112+
if (line == null)
113+
break;
114+
if (line.isEmpty())
115+
break;
116+
if (line.startsWith("#"))
117+
continue;
118+
119+
list.add(factory.makeGalaxy(line));
120+
121+
}
122+
r.close();
123+
Galaxy[] galaxies = new Galaxy[list.size()];
124+
galaxies = list.toArray(galaxies);
125+
return galaxies;
126+
} catch (FileNotFoundException e) {
127+
e.printStackTrace();
128+
} catch (IOException e) {
129+
e.printStackTrace();
130+
}
131+
return null;
132+
133+
}
134+
135+
private static void setConf(String[] args) {
136+
try {
137+
BufferedReader r = new BufferedReader(new FileReader(args[1]));
138+
String line;
139+
do {
140+
line = r.readLine();
141+
} while (line != null && line.startsWith("#"));
142+
r.close();
143+
String[] params = line.split(",");
144+
if (params[0].toLowerCase().equals("velocity")) {
145+
METHOD = Method.VELOCITY_VERLET;
146+
} else if (params[0].toLowerCase().equals("basic")) {
147+
METHOD = Method.BASIC_VERLET;
148+
} else {
149+
throw new IllegalArgumentException(params[0]
150+
+ " is not a method in " + args[1]);
151+
}
152+
Calculator.dt = Double.parseDouble(params[1]);
153+
GraphicsThread.fps = Double.parseDouble(params[2]);
154+
155+
} catch (FileNotFoundException e) {
156+
e.printStackTrace();
157+
} catch (IOException e) {
158+
e.printStackTrace();
159+
}
160+
161+
}
162+
147163
}

0 commit comments

Comments
 (0)