Skip to content

Commit 3381a95

Browse files
author
nanoandrew4
committed
Fixed bug when running in a headless system, isolated UI so program can launch without crashing
1 parent 1049460 commit 3381a95

File tree

4 files changed

+110
-89
lines changed

4 files changed

+110
-89
lines changed

gc/Fitness.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ CircleData getBiggestCircle(Point2D[] circles, Random rand, boolean drawVoronoi)
8181

8282
p.relocate(polygon.getBounds().getX(), polygon.getBounds().getY());
8383
p.setFill(Paint.valueOf(colors[rand.nextInt(colors.length)]));
84-
Main.pane.getChildren().add(p);
84+
UI.pane.getChildren().add(p);
8585
}
8686

8787
circles[i] = new Point2D(polygon.getCentroid().getX(), polygon.getCentroid().getY());

gc/GeneticAlgorithm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ else if (c2.fitness == Integer.MAX_VALUE && Main.isValid(c2.getCircleData(), cir
7474
c = c2;
7575

7676
if (c != null) {
77-
if (Main.pane != null) {
77+
if (UI.pane != null) {
7878
CircleData cd = c.getCircleData();
79-
Platform.runLater(() -> Main.draw(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("red")), gen));
79+
Platform.runLater(() -> UI.draw(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("red")), gen));
8080
}
8181
System.out.println(output = "Circle with seed " + seed + " from generation " + gen + " with proximity of " + ((Double.valueOf(c.decode().split("-")[0])) / largestRadius) * 100d
8282
+ "% and radius " + (Integer.valueOf(c.decode().split("-")[0])) + " is a valid solution");
@@ -102,8 +102,8 @@ else if (c2.fitness == Integer.MAX_VALUE && Main.isValid(c2.getCircleData(), cir
102102
//Platform.runLater(() -> {gc.Main.draw(Integer.valueOf(genes[1]), Integer.valueOf(genes[2]), Integer.valueOf(genes[0]));});
103103

104104
// will run algorithm faster
105-
if (Main.pane != null)
106-
Platform.runLater(() -> Main.draw(-1, -1, -1, gen));
105+
if (UI.pane != null)
106+
Platform.runLater(() -> UI.draw(-1, -1, -1, gen));
107107
}
108108
}
109109

gc/Main.java

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
package gc;
22

33
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;
114
import kn.uni.voronoitreemap.j2d.Point2D;
125

136
import java.util.ArrayList;
14-
import java.util.Random;
157

168
/*
179
Launches UI along with the Genetic Algorithm
1810
*/
1911

20-
public class Main extends Application {
12+
public class Main {
2113

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;
3116

3217
public static void main(String[] args) {
3318
String generalInfo = "Run program with no arguments to output help on usage";
3419
if (args.length == 1 && args[0].equals("ui")) {
3520
seed = System.currentTimeMillis();
36-
launch();
21+
launchUI();
3722
} else if (args.length == 2 && args[0].equals("ui")) {
3823
try {
3924
seed = Integer.valueOf(args[1]);
@@ -44,7 +29,7 @@ public static void main(String[] args) {
4429
System.out.println("Illegal argument. Please enter positive integer to be used as seed. " + generalInfo);
4530
return;
4631
}
47-
launch();
32+
launchUI();
4833
} else if (args.length == 3 && args[0].equals("headless")) {
4934
if ((args[1].equals("true") || args[1].equals("false")))
5035
DataCollector.generateFile = args[1].equals("true");
@@ -75,54 +60,8 @@ public static void main(String[] args) {
7560
}
7661
}
7762

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();
12665
}
12766

12867
// returns whether circle is out of bounds of the window
@@ -160,20 +99,4 @@ static Chromosome selectFittest(ArrayList<Chromosome> arr) {
16099

161100
return c;
162101
}
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-
}
179102
}

gc/UI.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package gc;
2+
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+
import kn.uni.voronoitreemap.j2d.Point2D;
12+
13+
import java.util.Random;
14+
15+
public class UI extends Application {
16+
17+
static Pane pane;
18+
19+
private Point2D[] circles;
20+
static Circle geneticCircle;
21+
private Random rand;
22+
23+
private int largestRadius, seed = (int)Main.seed;
24+
25+
private static Text genText;
26+
27+
public static void main(String[] args) {
28+
Application.launch(args);
29+
}
30+
31+
@Override
32+
public void start(Stage primaryStage) throws Exception {
33+
34+
rand = new Random(seed);
35+
36+
// depending on window size, encoded chromosome length will vary due to more digits being present to represent bigger numbers
37+
if (GlobalVars.screenHeight > GlobalVars.screenWidth) {
38+
Main.geneLength = Integer.toBinaryString(GlobalVars.screenHeight).length();
39+
Chromosome.maxSize = GlobalVars.screenWidth / 2;
40+
} else {
41+
Main.geneLength = Integer.toBinaryString(GlobalVars.screenWidth).length();
42+
Chromosome.maxSize = GlobalVars.screenHeight / 2;
43+
}
44+
45+
pane = new Pane();
46+
Scene scene = new Scene(pane, GlobalVars.screenWidth, GlobalVars.screenHeight);
47+
48+
circles = new Point2D[rand.nextInt(85) + 15];
49+
50+
// see fitness class for specification on what it does, returns largest radius that can be drawn in the window
51+
CircleData cd = new Fitness().getBiggestCircle(circles, rand, false);
52+
largestRadius = cd.radius;
53+
System.out.println("largest radius: " + cd.radius);
54+
55+
// add static circles to display
56+
for (Point2D circle : circles)
57+
pane.getChildren().add(new Circle(circle.getX(), circle.getY(), GlobalVars.circlesRadius));
58+
59+
// draw largest circle as determined by the voronoi diagram
60+
pane.getChildren().add(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("green")));
61+
62+
// sets graphics elements
63+
geneticCircle = new Circle(-10, -10, 1);
64+
pane.getChildren().add(geneticCircle);
65+
66+
genText = new Text(10, 30, "Generation: 0");
67+
genText.setFont(Font.font(20));
68+
pane.getChildren().add(genText);
69+
70+
primaryStage.setScene(scene);
71+
primaryStage.show();
72+
73+
// launches genetic algorithm thread
74+
75+
GeneticAlgorithm gA = new GeneticAlgorithm(circles, (int)seed, largestRadius);
76+
Thread t = new Thread(gA);
77+
78+
t.setDaemon(true);
79+
t.setPriority(Thread.MAX_PRIORITY);
80+
t.start();
81+
}
82+
83+
// draws best circle from the generation and updates text
84+
static void draw(int x, int y, int radius, int gen) {
85+
//pane.getChildren().remove(geneticCircle);
86+
//geneticCircle = new Circle(x, y, radius, Paint.valueOf("red"));
87+
//pane.getChildren().add(geneticCircle);
88+
genText.setText("Generation: " + gen);
89+
}
90+
91+
// draws final circle that conforms to specifications
92+
static void draw(Circle c, int gen) {
93+
pane.getChildren().remove(geneticCircle);
94+
geneticCircle = c;
95+
pane.getChildren().add(geneticCircle);
96+
genText.setText("Final generation: " + gen);
97+
}
98+
}

0 commit comments

Comments
 (0)