package jcgp; import javafx.application.Application; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.scene.Scene; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TabPane.TabClosingPolicy; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import jcgp.JCGP.Resources; import jcgp.gui.ChromosomePane; import jcgp.gui.GUIConsole; import jcgp.gui.settings.SettingsPane; public class GUI extends Application { /* Colours */ /** NEUTRAL_COLOUR is the colour elements should be when no interaction is occurring * (no hovering, clicking, dragging or any other form of interaction). */ public static final String NEUTRAL_COLOUR = "#FFFFFF"; public static final String HARD_HIGHLIGHT_COLOUR = "#5496FF"; // 89AAD6 public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF"; public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF"; // BDFFC2 public static final String GOOD_SELECTION_COLOUR = "#38C25B"; // FBFFB8 public static final String NEUTRAL_SELECTION_COLOUR = "#EDEB72"; // FF9C9C public static final String BAD_SELECTION_COLOUR = "#F53D3D"; /* Sizes and distances */ public static final double RESIZE_MARGIN = 5.0; public static final double SETTINGS_WIDTH = 200; public static final double CONSOLE_HEIGHT = 100; public static final double WRAP_WIDTH = 90; private static JCGP cgp; public static Resources resources; private BorderPane leftPane; private BorderPane window; private ChromosomePane[] chromosomes; private TabPane mainPane; private GUIConsole console = new GUIConsole(); private SettingsPane settings; private Service cgpService; private static boolean evolving = false; public static void main(String[] args) { cgp = new JCGP(); resources = cgp.getResources(); launch(); } @Override public void start(Stage primaryStage) throws Exception { cgpService = new Service () { @Override protected Task createTask() { Task t = new Task() { @Override protected Object call() throws Exception { while (true) { if (isCancelled()) { return null; } else { cgp.nextGeneration(); } } } }; return t; } }; /* * Instantiate the various GUI elements here. * * */ mainPane = new TabPane(); mainPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE); chromosomes = new ChromosomePane[cgp.getResources().getInt("popSize")]; Tab tab; for (int i = 0; i < chromosomes.length; i++) { chromosomes[i] = new ChromosomePane(cgp.getPopulation().getChromosome(i), resources); tab = new Tab("Chr " + i); tab.setContent(chromosomes[i]); mainPane.getTabs().add(tab); } settings = new SettingsPane(cgp, this); //mainPane.setPrefHeight(500); leftPane = new BorderPane(); leftPane.setCenter(mainPane); leftPane.setBottom(console); window = new BorderPane(); window.setCenter(leftPane); window.setRight(settings); //primaryStage.setMinHeight(600); //primaryStage.setMinWidth(800); primaryStage.setTitle("JCGP"); primaryStage.setScene(new Scene(window)); primaryStage.show(); } public void updateNodeGrids() { for (int i = 0; i < chromosomes.length; i++) { chromosomes[i].update(); } } public void playPause() { if (!evolving) { setEvolving(true); cgpService.restart(); } else { cgpService.cancel(); setEvolving(false); } } public void step() { if (!evolving) { Thread t = new Thread(new Task() { @Override protected Object call() throws Exception { cgp.nextGeneration(); setEvolving(false); return null; } }); setEvolving(true); t.start(); } } private void setEvolving(boolean value) { evolving = value; settings.toggleSettings(value); if (value) { settings.bindParameters(); } else { updateNodeGrids(); settings.unbindParameters(); } } public boolean isEvolving() { return evolving; } }