Exemplo n.º 1
0
 @Override
 protected void setUpGroup(Group g) {
   g.setOnMousePressed(MouseEvent::consume);
 }
  private Scene createScene() {
    final Group group = new Group();
    Scene scene = new Scene(group);

    final TestNode node1 = new TestNode(300, 300, "node1");
    final TestNode node2 = new TestNode(500, 200, "node2");
    final TestNode node3 = new TestNode(80, 100, "node3");
    final TestNode node4 = new TestNode(650, 500, "node4");
    connectNodes(node1, node2);
    connectNodes(node1, node3);
    connectNodes(node2, node3);
    connectNodes(node1, node4);

    ArrayList<Node> array = new ArrayList<Node>();
    for (Edge n : node1.lines) array.add(n.line);
    for (Edge n : node2.lines) array.add(n.line);
    for (Edge n : node3.lines) array.add(n.line);
    for (Edge n : node4.lines) array.add(n.line);
    LinkedHashSet<Node> lhs = new LinkedHashSet<Node>();
    lhs.addAll(array); // Remove duplicates
    array.clear();
    array.addAll(lhs);

    array.add(node1.circle);
    array.add(node1.text);
    array.add(node2.circle);
    array.add(node2.text);
    array.add(node3.circle);
    array.add(node3.text);
    array.add(node4.circle);
    array.add(node4.text);

    Collection<Node> c = array;

    // Won't work with JavaFX 2.2 ->
    // group.getChildren().add(new Group(c));

    group
        .getChildren()
        .add(
            new Group(
                // edge1.line, edge2.line, edge3.line, edge4.line,
                node1.circle, node1.text,
                node2.circle, node2.text,
                node3.circle, node3.text,
                node4.circle, node4.text));

    group.setOnMousePressed(
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent event) {
            lastX = event.getX();
            lastY = event.getY();
            if (ITERATION == 0) changePosition(node1, 100, 500); // TEMPORARY; FOR TESTING
            if (ITERATION == 1) changePosition(node2, 300, 200); // TEMPORARY; FOR TESTING
            if (ITERATION == 2) changePosition(node3, 600, 300); // TEMPORARY; FOR TESTING
            if (ITERATION == 3) changePosition(node4, 300, 530); // TEMPORARY; FOR TESTING
            if (ITERATION == 4) changePosition(node1, 500, 100); // TEMPORARY; FOR TESTING
            if (ITERATION == 5) changePosition(node1, 400, 350); // TEMPORARY; FOR TESTING
            if (ITERATION == 6) changePosition(node2, 200, 150); // TEMPORARY; FOR TESTING
            if (ITERATION == 6) changePosition(node3, 700, 200); // TEMPORARY; FOR TESTING
            ++ITERATION; // TEMPORARY; FOR TESTING
          }
        });

    group.addEventHandler(
        ZoomEvent.ZOOM_STARTED,
        new EventHandler<ZoomEvent>() {
          @Override
          public void handle(ZoomEvent event) {
            startScale = group.getScaleX();
            group.setScaleX(startScale);
            // group.setScaleY(startScale); // Is this necessary? Apparently not.
          }
        });
    group.addEventHandler(
        ZoomEvent.ZOOM,
        new EventHandler<ZoomEvent>() {
          @Override
          public void handle(ZoomEvent event) {
            group.setScaleX(startScale * event.getTotalZoomFactor());
            group.setScaleY(startScale * event.getTotalZoomFactor());
          }
        });

    // TODO: Adjust the text after rotation?

    group.addEventHandler(
        RotateEvent.ROTATION_STARTED,
        new EventHandler<RotateEvent>() {
          @Override
          public void handle(RotateEvent event) {
            startRotate = group.getRotate();
            group.setRotate(startRotate);
          }
        });
    group.addEventHandler(
        RotateEvent.ROTATE,
        new EventHandler<RotateEvent>() {
          @Override
          public void handle(RotateEvent event) {
            group.setRotate(startRotate + event.getTotalAngle());
          }
        });

    return scene;
  }