Exemplo n.º 1
0
  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);
    }
  }
Exemplo n.º 2
0
  /**
   * 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);
  }