예제 #1
0
  void animateRoute(List<Path> path, Circle entranceMarker, GraphicsContext gc) {
    double entranceX = entranceMarker.getCenterX();
    double entranceY = entranceMarker.getCenterY();

    int currentKeyFrameTimeInMs = 0, keyframeTimeInMs = 50;

    List<KeyFrame> keyFrames = new ArrayList<>();
    List<AnimationTimer> timers = new ArrayList<>();

    for (int i = 0; i < path.size(); i++) {
      Path pathEntry = path.get(i);
      DoubleProperty opacity = new SimpleDoubleProperty();
      keyFrames.add(
          new KeyFrame(Duration.millis(currentKeyFrameTimeInMs), new KeyValue(opacity, 0)));
      keyFrames.add(
          new KeyFrame(
              Duration.millis(currentKeyFrameTimeInMs + keyframeTimeInMs),
              new KeyValue(opacity, 0.3)));

      timeline.setAutoReverse(true);
      timeline.setCycleCount(1);

      currentKeyFrameTimeInMs += keyframeTimeInMs;

      timers.add(
          new AnimationTimer() {
            @Override
            public void handle(long now) {
              gc.setFill(Color.FORESTGREEN.deriveColor(0, 1, 1, opacity.get()));
              if (maze.representation().length > 24) {
                gc.fillRect(
                    entranceX + calculateNextPosX(pathEntry.x, 2) - 20,
                    entranceY + calculateNextPosY(pathEntry.y, 2) - 176,
                    CELL_LENGTH,
                    CELL_LENGTH);
              } else {
                gc.fillRect(
                    entranceX + calculateNextPosX(pathEntry.x, 5) - 23,
                    entranceY + calculateNextPosY(pathEntry.y, 5) - 178,
                    CELL_LENGTH,
                    CELL_LENGTH);
              }
            }
          });
    }

    timeline.getKeyFrames().addAll(keyFrames);

    for (int i = 0; i < path.size(); i++) {
      timers.get(i).start();
    }
    timeline.play();
  }
 public void relocateText(Text text, double centerX, double centerY) {
   double textWidth = text.getBoundsInLocal().getWidth();
   double textHeight = text.getBoundsInLocal().getHeight();
   text.relocate(circle.getCenterX() - textWidth / 2, circle.getCenterY() - textHeight / 2);
 }
예제 #3
0
 private Node edge(Circle n, Circle m) {
   Line line = new Line(n.getCenterX(), n.getCenterY(), m.getCenterX(), m.getCenterY());
   line.setStroke(Color.BLACK);
   return line;
 }
예제 #4
0
  // 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));
    }
  }