diff options
Diffstat (limited to 'src/jcgp/gui/population/ChromosomePane.java')
-rw-r--r-- | src/jcgp/gui/population/ChromosomePane.java | 185 |
1 files changed, 96 insertions, 89 deletions
diff --git a/src/jcgp/gui/population/ChromosomePane.java b/src/jcgp/gui/population/ChromosomePane.java index ede01e0..6a5b33f 100644 --- a/src/jcgp/gui/population/ChromosomePane.java +++ b/src/jcgp/gui/population/ChromosomePane.java @@ -6,13 +6,15 @@ import javafx.scene.control.ScrollPane; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import jcgp.backend.population.Chromosome; -import jcgp.backend.population.Connection; +import jcgp.backend.population.Gene; import jcgp.backend.population.Input; import jcgp.backend.population.Node; -import jcgp.backend.resources.Resources; +import jcgp.backend.population.Output; import jcgp.gui.GUI; -import jcgp.gui.constants.Constants; -import jcgp.gui.handlers.GUIHandlers; +import jcgp.gui.constants.Position; +import jcgp.gui.handlers.InputHandlers; +import jcgp.gui.handlers.NodeHandlers; +import jcgp.gui.handlers.OutputHandlers; /** * This extension of {@code ScrollPane} contains a series of @@ -28,135 +30,129 @@ public class ChromosomePane extends ScrollPane { private GUINode[][] guiNodes; private GUIInput[] guiInputs; private GUIOutput[] guiOutputs; - - private ArrayList<Line> connectionLines; - + private Pane content; - + private int rows, columns; - + private boolean target = false; - + public ChromosomePane(Chromosome chromosome) { super(); - - final Resources resources = GUI.resources; - - connectionLines = new ArrayList<Line>(); - - rows = resources.rows(); - columns = resources.columns(); - + + ArrayList<Line> connectionLines = new ArrayList<Line>(); + + rows = GUI.resources.rows(); + columns = GUI.resources.columns(); + content = new Pane(); content.setId("content pane for genes"); - + // generate the GUIGenes /* * inputs */ - guiInputs = new GUIInput[resources.inputs()]; + guiInputs = new GUIInput[GUI.resources.inputs()]; for (int i = 0; i < guiInputs.length; i++) { + // get the backend input + Input input = chromosome.getInput(i); // make the GUI elements - guiInputs[i] = new GUIInput(chromosome.getInput(i)); - guiInputs[i].relocate(Constants.NODE_RADIUS, - (chromosome.getInput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - GUIHandlers.addHandlers(guiInputs[i]); + guiInputs[i] = new GUIInput(input); + // assign the GUI object to the associated backend element + input.setGUIObject(guiInputs[i]); + // position, handlers + Position.place(guiInputs[i]); + InputHandlers.addHandlers(guiInputs[i]); } content.getChildren().addAll(guiInputs); - - /* + + /* * nodes */ guiNodes = new GUINode[rows][columns]; - double angle, xPos, yPos; - for (int r = 0; r < rows; r++) { - for (int c = 0; c < columns; c++) { + //double angle, xPos, yPos; + + for (int c = 0; c < columns; c++) { + for (int r = 0; r < rows; r++) { + // get the backend node + Node node = chromosome.getNode(r, c); // make the connection lines - Line lines[] = new Line[resources.arity()]; + Line lines[] = new Line[GUI.resources.arity()]; for (int l = 0; l < lines.length; l++) { - angle = ((((double) (l + 1)) / ((double) (lines.length + 1))) * Constants.THETA) - (Constants.THETA / 2); - xPos = (-Math.cos(angle) * Constants.NODE_RADIUS) + (((c + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - yPos = (Math.sin(angle) * Constants.NODE_RADIUS) + ((r * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); + lines[l] = new Line(); + +// angle = ((((double) (l + 1)) / ((double) (lines.length + 1))) * Constants.THETA) - (Constants.THETA / 2); +// xPos = (-Math.cos(angle) * Constants.NODE_RADIUS) + (((c + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS) + Constants.SOCKET_RADIUS; +// yPos = (Math.sin(angle) * Constants.NODE_RADIUS) + ((r * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); +// lines[l].setStartX(xPos); +// lines[l].setStartX(yPos); + + Position.connect(lines[l], (GUIGene) ((Gene) node.getConnection(l)).getGUIObject()); - lines[l] = new Line(xPos, yPos, xPos, yPos); lines[l].setMouseTransparent(true); - //lines[l].setVisible(false); + lines[l].setVisible(false); connectionLines.add(lines[l]); } // make the GUI elements - guiNodes[r][c] = new GUINode(chromosome.getNode(r, c), lines) { - @Override - public GUIConnection getGUIConnection(Connection connection) { - if (connection instanceof Input) { - return guiInputs[((Input) connection).getIndex()]; - } else if (connection instanceof Node) { - return guiNodes[((Node) connection).getRow()][((Node) connection).getColumn()]; - } else { - // something bad happened! - throw new ClassCastException(); - } - } - }; - guiNodes[r][c].relocate(((chromosome.getNode(r, c).getColumn() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS, - (chromosome.getNode(r, c).getRow() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - GUIHandlers.addHandlers(guiNodes[r][c]); + guiNodes[r][c] = new GUINode(node, lines); + // assign the GUI object to the associated backend element + node.setGUIObject(guiNodes[r][c]); + // position, handlers + Position.place(guiNodes[r][c]); + NodeHandlers.addHandlers(guiNodes[r][c]); + content.getChildren().add(guiNodes[r][c]); } - content.getChildren().addAll(guiNodes[r]); } - + /* * outputs */ - guiOutputs = new GUIOutput[resources.outputs()]; + guiOutputs = new GUIOutput[GUI.resources.outputs()]; for (int i = 0; i < guiOutputs.length; i++) { + Line line = new Line(); + // get the backend output + Output output = chromosome.getOutput(i); // make the GUI elements - guiOutputs[i] = new GUIOutput(chromosome.getOutput(i)) { - - @Override - public GUIConnection getGUIConnection(Connection connection) { - if (connection instanceof Input) { - return guiInputs[((Input) connection).getIndex()]; - } else if (connection instanceof Node) { - return guiNodes[((Node) connection).getRow()][((Node) connection).getColumn()]; - } else { - // something bad happened! - throw new ClassCastException(); - } - } - - }; - guiOutputs[i].relocate(((resources.columns() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS, - (chromosome.getOutput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - GUIHandlers.addHandlers(guiOutputs[i]); + guiOutputs[i] = new GUIOutput(output, line); + // assign the GUI object to the associated backend element + output.setGUIObject(guiOutputs[i]); + // position, handlers + Position.place(guiOutputs[i]); + OutputHandlers.addHandlers(guiOutputs[i]); + connectionLines.add(line); } content.getChildren().addAll(guiOutputs); - // add lines to the pane as on top of genes + // add lines to the pane on top of genes content.getChildren().addAll(connectionLines); - + setPrefWidth(620); setContent(content); } - -// protected GUIGene getGuiGene(Connection gene) { -// if (gene instanceof Input) { -// return guiInputs[((Input) gene).getIndex()]; -// } else if (gene instanceof Node) { -// return guiNodes[((Node) gene).getRow()][((Node) gene).getColumn()]; -// } else { -// // something bad happened! -// throw new ClassCastException(); -// } -// } - + + + /* + * does this work lol + */ + // protected GUIGene getGuiGene(Connection gene) { + // if (gene instanceof Input) { + // return guiInputs[((Input) gene).getIndex()]; + // } else if (gene instanceof Node) { + // return guiNodes[((Node) gene).getRow()][((Node) gene).getColumn()]; + // } else { + // // something bad happened! + // throw new ClassCastException(); + // } + // } + protected boolean isTarget() { return target; } - + protected void setTarget(boolean newValue) { target = newValue; } - + public void updateGenes(Chromosome chr) { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { @@ -167,7 +163,7 @@ public class ChromosomePane extends ScrollPane { guiOutputs[i].setOutput(chr.getOutput(i)); } } - + public static boolean isAllowed(GUIMutable source, GUIConnection target) { if (source instanceof GUINode) { // if the source is a node, all inputs and some nodes are valid @@ -193,4 +189,15 @@ public class ChromosomePane extends ScrollPane { // if the source was neither node nor output, something bad is happening throw new ClassCastException("Source was neither GUINode nor GUIOutput."); } + + // private GUIConnection getGUIConnection(Connection connection) { + // if (connection instanceof Input) { + // return guiInputs[((Input) connection).getIndex()]; + // } else if (connection instanceof Node) { + // return guiNodes[((Node) connection).getRow()][((Node) connection).getColumn()]; + // } else { + // // something bad happened! + // throw new ClassCastException(); + // } + // } } |