@Override public void initialize(URL location, ResourceBundle resources) { // TODO Auto-generated method stub ObservableList<String> comboItems = FXCollections.observableArrayList("Guesser", "Setter"); modeSet.setItems(comboItems); modeSet.setValue("Guesser"); ASet.setValue(Color.RED); BSet.setValue(Color.BLUE); CSet.setValue(Color.GREEN); DSet.setValue(Color.CHOCOLATE); ESet.setValue(Color.PURPLE); FSet.setValue(Color.HOTPINK); A.setFill(ASet.getValue()); B.setFill(BSet.getValue()); C.setFill(CSet.getValue()); D.setFill(DSet.getValue()); E.setFill(ESet.getValue()); F.setFill(FSet.getValue()); winLose.setText(""); setPins.setDisable(true); blackPin.setOnMousePressed( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { // TODO Auto-generated method stub addBlackPin(); } }); whitePin.setOnMousePressed( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { // TODO Auto-generated method stub addWhitePin(); } }); }
@Override public void start(Stage primaryStage) throws Exception { // UI fields VBox mainBox = new VBox(); sequenceField = new TextField(myLabels.TEXTAREA_SEQUENCE); bracketField = new TextField(myLabels.TEXTAREA_BRACKET); HBox buttonBox = new HBox(); Button computeButton = new Button(myLabels.BUTTON_COMPUTE); Button drawButton = new Button(myLabels.BUTTON_DRAW); CheckBox animateChecker = new CheckBox(myLabels.CHECKBOX_ANIMATE); drawPane = new Pane(); buttonBox.getChildren().addAll(computeButton, drawButton, animateChecker); mainBox.getChildren().addAll(sequenceField, bracketField, buttonBox, drawPane); // Compute Button disable sequenceField .textProperty() .addListener( (observable, oldValue, newValue) -> { if (newValue.length() == 0) { computeButton.setDisable(true); } else { computeButton.setDisable(false); } }); // Animate Checkbox animateChecker.setOnAction( (value) -> { if (animateChecker.isSelected() != isAnimated) { isAnimated = animateChecker.isSelected(); // set Eventhandler für Drag & Drop for (Circle currentCircle : circleList) { currentCircle.setOnMousePressed(isAnimated ? circleOnMousePressedEventHandler : null); currentCircle.setOnMouseDragged(isAnimated ? circleOnMouseDraggedEventHandler : null); currentCircle.setOnMouseReleased( isAnimated ? circleOnMouseReleasedEventHandler : null); } } }); // computeButton computeButton.setOnAction( (value) -> { bracketField.setText(new Nussinov(sequenceField.getText()).getBracketNotation()); }); // drawButton drawButton.setOnAction( (value) -> { Graph myGraph = new Graph(); try { myGraph.parseNotation(bracketField.getText()); } catch (IOException e) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle(myLabels.ALERT_TITLE); alert.setHeaderText(myLabels.ALTERT_MESSAGE); alert.setContentText(bracketField.getText()); alert.showAndWait(); } coordsRepresentation[0] = SpringEmbedder.computeSpringEmbedding( iterations, myGraph.getNumberOfNodes(), myGraph.getEdges(), null); SpringEmbedder.centerCoordinates(coordsRepresentation[0], 50, 550, 50, 550); drawShapes( drawPane, coordsRepresentation[0], myGraph.getEdges(), myGraph.getNumberOfNodes()); }); // prepare scene Scene scene = new Scene(mainBox, 600, 800); primaryStage.setScene(scene); primaryStage.setTitle(myLabels.CAPTION); // show scene primaryStage.show(); }
/** * Drawing function for Circles and Lines * * @param drawPane * @param coords Circle Coordinates * @param edges array of edges * @param startIndex NumberOfNodes */ private void drawShapes(Pane drawPane, double[][] coords, int[][] edges, int startIndex) { // clear previous pane drawPane.getChildren().clear(); circleList = new ArrayList<>(); // generate Circles for (int i = 0; i < coords.length; i++) { Circle currentCircle = new Circle(coords[i][0], coords[i][1], nodeSize, Color.BLACK); String toolTipText = "Node " + Integer.toString(i + 1); // expand toolTip text with nucleotide and Circle color, if possible if (sequenceField.getText().length() > i) { toolTipText += ": " + sequenceField.getText().charAt(i); currentCircle.setFill(getNodeColor(sequenceField.getText().charAt(i))); } Tooltip.install(currentCircle, new Tooltip(toolTipText)); if (isAnimated) { currentCircle.setOnMousePressed(circleOnMousePressedEventHandler); currentCircle.setOnMouseDragged(circleOnMouseDraggedEventHandler); currentCircle.setOnMouseReleased(circleOnMouseReleasedEventHandler); } circleList.add(currentCircle); } // generate basic Lines ArrayList<Line> lineList = new ArrayList<>(); for (int i = 0; i < circleList.size() - 1; i++) { Line line = new Line(); line.setStroke(Color.BLACK); line.setFill(Color.BLACK); Circle circle1 = circleList.get(i); Circle circle2 = circleList.get(i + 1); // bind ends of line: line.startXProperty().bind(circle1.centerXProperty().add(circle1.translateXProperty())); line.startYProperty().bind(circle1.centerYProperty().add(circle1.translateYProperty())); line.endXProperty().bind(circle2.centerXProperty().add(circle2.translateXProperty())); line.endYProperty().bind(circle2.centerYProperty().add(circle2.translateYProperty())); lineList.add(line); } // generate edges for (int i = startIndex - 1; i < edges.length; i++) { Line line = new Line(); line.setStroke(Color.ORANGE); Circle circle1 = circleList.get(edges[i][0]); Circle circle2 = circleList.get(edges[i][1]); line.startXProperty().bind(circle1.centerXProperty().add(circle1.translateXProperty())); line.startYProperty().bind(circle1.centerYProperty().add(circle1.translateYProperty())); line.endXProperty().bind(circle2.centerXProperty().add(circle2.translateXProperty())); line.endYProperty().bind(circle2.centerYProperty().add(circle2.translateYProperty())); lineList.add(line); } drawPane.getChildren().addAll(lineList); drawPane.getChildren().addAll(circleList); }
// drawing public void drawGraph() { Timeline timeline = new Timeline(); if (graph != null && finalCoordinates != null) { // reset pane drawing.getChildren().clear(); SpringEmbedder.centerCoordinates(finalCoordinates, 20, 380, 20, 380); SpringEmbedder.centerCoordinates(startingCoordinates, 20, 380, 20, 380); // draw nucleotides circles = new Circle[finalCoordinates.length]; for (int i = 0; i < finalCoordinates.length; i++) { // create nodes Circle circle = new Circle(5); circle.setCenterX(finalCoordinates[i][0]); circle.setCenterY(finalCoordinates[i][1]); switch (graph.getSequence().charAt(i)) { case 'A': circle.setFill(Color.YELLOW); break; case 'a': circle.setFill(Color.YELLOW); break; case 'U': circle.setFill(Color.GREEN); break; case 'u': circle.setFill(Color.GREEN); break; case 'C': circle.setFill(Color.BLUE); break; case 'c': circle.setFill(Color.BLUE); break; case 'G': circle.setFill(Color.RED); break; case 'g': circle.setFill(Color.RED); break; } circle.setStroke(Color.BLACK); // add tooltip Tooltip t = new Tooltip("Nucleotide: " + graph.getSequence().charAt(i) + "\nPosition: " + i); Tooltip.install(circle, t); // add drag-and-drop behaviour circle.setOnMousePressed( event -> { if (isAnimationActivated) { dragStartX = circle.getCenterX(); dragStartY = circle.getCenterY(); } }); circle.setOnMouseDragged( event -> { if (isAnimationActivated) { circle.setCenterX(event.getX()); circle.setCenterY(event.getY()); } }); circle.setOnMouseReleased( event -> { if (isAnimationActivated) { Timeline dragTimeline = new Timeline(); KeyValue circlePositionXKey = new KeyValue(circle.centerXProperty(), dragStartX); KeyValue circlePositionYKey = new KeyValue(circle.centerYProperty(), dragStartY); dragTimeline .getKeyFrames() .add(new KeyFrame(Duration.millis(animationTime), circlePositionXKey)); dragTimeline .getKeyFrames() .add(new KeyFrame(Duration.millis(animationTime), circlePositionYKey)); dragTimeline.play(); } }); // save circle locally circles[i] = circle; // animate circles KeyValue startingXKey = new KeyValue(circle.centerXProperty(), startingCoordinates[i][0]); KeyValue finalXKey = new KeyValue(circle.centerXProperty(), circle.getCenterX()); KeyValue startingYKey = new KeyValue(circle.centerYProperty(), startingCoordinates[i][1]); KeyValue finalYKey = new KeyValue(circle.centerYProperty(), circle.getCenterY()); timeline.getKeyFrames().add(new KeyFrame(Duration.millis(0), startingXKey)); timeline.getKeyFrames().add(new KeyFrame(Duration.millis(0), startingYKey)); timeline.getKeyFrames().add(new KeyFrame(Duration.millis(animationTime), finalXKey)); timeline.getKeyFrames().add(new KeyFrame(Duration.millis(animationTime), finalYKey)); } // create edges int[][] edges = graph.getEdges(); for (int i = 0; i < edges.length; i++) { // bind edges to nodes Line line = new Line( finalCoordinates[edges[i][0]][0], finalCoordinates[edges[i][0]][1], finalCoordinates[edges[i][1]][0], finalCoordinates[edges[i][1]][1]); line.startYProperty().bind(circles[edges[i][0]].centerYProperty()); line.startXProperty().bind(circles[edges[i][0]].centerXProperty()); line.endYProperty().bind(circles[edges[i][1]].centerYProperty()); line.endXProperty().bind(circles[edges[i][1]].centerXProperty()); // draw edges if (edges[i][1] - edges[i][0] != 1) { line.setStroke(Color.CADETBLUE); } else { line.setFill(Color.DARKGRAY); } drawing.getChildren().add(line); } // draw all circles in order to overlap the edges drawing.getChildren().addAll(circles); if (isAnimationActivated) { timeline.play(); } } else { throw new IllegalArgumentException( "graph is null: " + graph.equals(null) + "\ncoordinates is null: " + finalCoordinates.equals(null)); } }