@@ -48,73 +48,21 @@ public class Main {
48
48
49
49
public static void main (String [] args ) {
50
50
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
+
78
55
ObjectFactory factory = new ObjectFactory (METHOD );
79
-
80
-
56
+
81
57
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 );
104
59
} 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 );
113
61
}
114
62
run (galaxies );
115
63
116
64
}
117
-
65
+
118
66
/**
119
67
* Main entry point of the application. Call this with an array of galaxies
120
68
* to simulate their movement
@@ -144,4 +92,72 @@ public static void run(Galaxy[] galaxies) {
144
92
t .start ();
145
93
}
146
94
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
+
147
163
}
0 commit comments