1
1
package gc ;
2
2
3
3
import javafx .application .Application ;
4
- import javafx .scene .Scene ;
5
- import javafx .scene .layout .Pane ;
6
- import javafx .scene .paint .Paint ;
7
- import javafx .scene .shape .Circle ;
8
- import javafx .scene .text .Font ;
9
- import javafx .scene .text .Text ;
10
- import javafx .stage .Stage ;
11
4
import kn .uni .voronoitreemap .j2d .Point2D ;
12
5
13
6
import java .util .ArrayList ;
14
- import java .util .Random ;
15
7
16
8
/*
17
9
Launches UI along with the Genetic Algorithm
18
10
*/
19
11
20
- public class Main extends Application {
12
+ public class Main {
21
13
22
- static Pane pane ;
23
- private Point2D [] circles ;
24
- private Random rand = new Random (seed );
25
-
26
- private static Circle geneticCircle ;
27
- private static Text genText ;
28
-
29
- static int geneLength , largestRadius ;
30
- private static long seed = 2 ;
14
+ static long seed ;
15
+ static int geneLength ;
31
16
32
17
public static void main (String [] args ) {
33
18
String generalInfo = "Run program with no arguments to output help on usage" ;
34
19
if (args .length == 1 && args [0 ].equals ("ui" )) {
35
20
seed = System .currentTimeMillis ();
36
- launch ();
21
+ launchUI ();
37
22
} else if (args .length == 2 && args [0 ].equals ("ui" )) {
38
23
try {
39
24
seed = Integer .valueOf (args [1 ]);
@@ -44,7 +29,7 @@ public static void main(String[] args) {
44
29
System .out .println ("Illegal argument. Please enter positive integer to be used as seed. " + generalInfo );
45
30
return ;
46
31
}
47
- launch ();
32
+ launchUI ();
48
33
} else if (args .length == 3 && args [0 ].equals ("headless" )) {
49
34
if ((args [1 ].equals ("true" ) || args [1 ].equals ("false" )))
50
35
DataCollector .generateFile = args [1 ].equals ("true" );
@@ -75,54 +60,8 @@ public static void main(String[] args) {
75
60
}
76
61
}
77
62
78
- @ Override
79
- public void start (Stage primaryStage ) throws Exception {
80
-
81
- // depending on window size, encoded chromosome length will vary due to more digits being present to represent bigger numbers
82
- if (GlobalVars .screenHeight > GlobalVars .screenWidth ) {
83
- geneLength = Integer .toBinaryString (GlobalVars .screenHeight ).length ();
84
- Chromosome .maxSize = GlobalVars .screenWidth / 2 ;
85
- } else {
86
- geneLength = Integer .toBinaryString (GlobalVars .screenWidth ).length ();
87
- Chromosome .maxSize = GlobalVars .screenHeight / 2 ;
88
- }
89
-
90
- pane = new Pane ();
91
- Scene scene = new Scene (pane , GlobalVars .screenWidth , GlobalVars .screenHeight );
92
-
93
- circles = new Point2D [rand .nextInt (85 ) + 15 ];
94
-
95
- // see fitness class for specification on what it does, returns largest radius that can be drawn in the window
96
- CircleData cd = new Fitness ().getBiggestCircle (circles , rand , false );
97
- largestRadius = cd .radius ;
98
- System .out .println ("largest radius: " + cd .radius );
99
-
100
- // add static circles to display
101
- for (Point2D circle : circles )
102
- pane .getChildren ().add (new Circle (circle .getX (), circle .getY (), GlobalVars .circlesRadius ));
103
-
104
- // draw largest circle as determined by the voronoi diagram
105
- pane .getChildren ().add (new Circle (cd .coords .getX (), cd .coords .getY (), cd .radius , Paint .valueOf ("green" )));
106
-
107
- // sets graphics elements
108
- geneticCircle = new Circle (-10 , -10 , 1 );
109
- pane .getChildren ().add (geneticCircle );
110
-
111
- genText = new Text (10 , 30 , "Generation: 0" );
112
- genText .setFont (Font .font (20 ));
113
- pane .getChildren ().add (genText );
114
-
115
- primaryStage .setScene (scene );
116
- primaryStage .show ();
117
-
118
- // launches genetic algorithm thread
119
-
120
- GeneticAlgorithm gA = new GeneticAlgorithm (circles , (int )seed , largestRadius );
121
- Thread t = new Thread (gA );
122
-
123
- t .setDaemon (true );
124
- t .setPriority (Thread .MAX_PRIORITY );
125
- t .start ();
63
+ static void launchUI () {
64
+ new Thread (() -> Application .launch (UI .class )).start ();
126
65
}
127
66
128
67
// returns whether circle is out of bounds of the window
@@ -160,20 +99,4 @@ static Chromosome selectFittest(ArrayList<Chromosome> arr) {
160
99
161
100
return c ;
162
101
}
163
-
164
- // draws best circle from the generation and updates text
165
- static void draw (int x , int y , int radius , int gen ) {
166
- //pane.getChildren().remove(geneticCircle);
167
- //geneticCircle = new Circle(x, y, radius, Paint.valueOf("red"));
168
- //pane.getChildren().add(geneticCircle);
169
- genText .setText ("Generation: " + gen );
170
- }
171
-
172
- // draws final circle that conforms to specifications
173
- static void draw (Circle c , int gen ) {
174
- pane .getChildren ().remove (geneticCircle );
175
- geneticCircle = c ;
176
- pane .getChildren ().add (geneticCircle );
177
- genText .setText ("Final generation: " + gen );
178
- }
179
102
}
0 commit comments