public void createNodes(Graph graph, double[][] coordinates) {
    nodeArray = new ArrayList<>();
    double radius = graph.getNumberOfNodes() < 25 ? 100 / graph.getNumberOfNodes() : 4;
    for (int i = 0; i < coordinates.length; i++) {
      Circle nodeI = new Circle(radius);
      nodeI.setTranslateX(coordinates[i][0]);
      nodeI.setTranslateY(coordinates[i][1]);
      Tooltip tip =
          new Tooltip("node number " + (i + 1) + ", \"" + root.sequence.getText().charAt(i) + "\"");
      Tooltip.install(nodeI, tip);
      nodeI.setId(root.sequence.getText().substring(i, i + 1));
      Timeline timeline = new Timeline();

      nodeI.setOnMouseDragged(
          event -> {
            nodeI.setTranslateX(event.getSceneX() - root.nodes.getLayoutX());
            nodeI.setTranslateY(event.getSceneY() - root.nodes.getLayoutY());
          });
      final int finalI = i;
      nodeI.setOnMouseReleased(
          event -> {
            double[][] newCoordinates =
                SpringEmbedder.computeSpringEmbedding(
                    100, graph.getNumberOfNodes(), graph.getEdges(), null);
            SpringEmbedder.centerCoordinates(
                newCoordinates,
                0,
                (int) root.getWidth(),
                20,
                (int) (root.getHeight() - root.vbox.getHeight() * 1.5));
            timeline
                .getKeyFrames()
                .add(
                    new KeyFrame(
                        Duration.millis(200),
                        new KeyValue(nodeI.translateXProperty(), coordinates[finalI][0])));
            timeline
                .getKeyFrames()
                .add(
                    new KeyFrame(
                        Duration.millis(200),
                        new KeyValue(nodeI.translateYProperty(), coordinates[finalI][1])));
            timeline.play();
          });

      nodeI.disableProperty().bind(Bindings.not(root.animate.selectedProperty()));
      root.nodes.getChildren().add(nodeI);
      nodeArray.add(nodeI);
    }
  }
 public void simpleDrawing() {
   Graph graph = createGraph();
   double[][] coordinates =
       SpringEmbedder.computeSpringEmbedding(
           100, graph.getNumberOfNodes(), graph.getEdges(), null);
   root.nodes.getChildren().clear();
   SpringEmbedder.centerCoordinates(
       coordinates,
       0,
       (int) root.getWidth(),
       20,
       (int) (root.getHeight() - root.vbox.getHeight() * 1.5));
   createNodes(graph, coordinates);
   createEdges(graph);
 }
 public void animateDrawing() {
   Graph graph = createGraph();
   double[][] coordinates = new double[graph.getNumberOfNodes()][2];
   if (nodeArray == null) {
     for (double[] point : coordinates) {
       point[0] = random();
       point[1] = random();
     }
   } else {
     for (int i = 0; i < coordinates.length; i++) {
       coordinates[i][0] = nodeArray.get(i).getTranslateX();
       coordinates[i][1] = nodeArray.get(i).getTranslateY();
     }
   }
   root.nodes.getChildren().clear();
   SpringEmbedder.centerCoordinates(
       coordinates,
       0,
       (int) root.getWidth(),
       20,
       (int) (root.getHeight() - root.vbox.getHeight() * 1.5));
   createNodes(graph, coordinates);
   createEdges(graph);
   animate(graph);
 }
 public void animate(Graph graph) {
   final Timeline timeline = new Timeline();
   double[][] endCoordinates =
       SpringEmbedder.computeSpringEmbedding(
           100, graph.getNumberOfNodes(), graph.getEdges(), null);
   SpringEmbedder.centerCoordinates(
       endCoordinates,
       0,
       (int) root.getWidth(),
       20,
       (int) (root.getHeight() - root.vbox.getHeight() * 1.5));
   Duration duration = Duration.millis(3000);
   for (int i = 0; i < nodeArray.size(); i++) {
     KeyFrame keyFrameX =
         new KeyFrame(
             duration, new KeyValue(nodeArray.get(i).translateXProperty(), endCoordinates[i][0]));
     KeyFrame keyFrameY =
         new KeyFrame(
             duration, new KeyValue(nodeArray.get(i).translateYProperty(), endCoordinates[i][1]));
     timeline.getKeyFrames().addAll(keyFrameX, keyFrameY);
   }
   timeline.play();
 }
Example #5
0
  @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();
  }