4

I am implementing an interface for creating graph nodes and connecting them using JUNG.

I want to create some nodes that can move from one existing node to another node using the edge between two nodes as their path (It will be used for showing some Data Packets being transferred between nodes that are like Hosts).

There is some information on the internet about how to make the JUNG nodes(vertices) movable by mouse but there is no info about moving them by modifying values in the code.

Even if there is someway for moving the nodes is it possible and efficient to move the node between nodes using the edge between them as the moving path in JUNG library?

Any suggestions would be appreciated.

4

1 回答 1

1

setLocation您可以使用布局的方法强制移动顶点。我已经建立了与您的要求非常相似的东西。它产生一个从顶点 A 直线移动到顶点 B 的顶点。如果你的边缘是直的,那么它可能会起作用:

import java.awt.geom.Point2D;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.util.IterativeProcess;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class VertexCollider extends IterativeProcess {

    private static final String COLLIDER = "Collider";
    private AbstractLayout<String, Number> layout;
    private VisualizationViewer<String, Number> vv;
    private Point2D startLocation;
    private Point2D endLocation;
    private Double moveX;
    private Double moveY;

    public VertexCollider(AbstractLayout<String, Number> layout, VisualizationViewer<String, Number> vv, String vertexA, String vertexB) {
        this.layout = layout;
        this.vv = vv;
        startLocation = layout.transform(vertexA);
        endLocation = layout.transform(vertexB);
    }

    public void initialize() {
        setPrecision(Double.MAX_VALUE);
        layout.getGraph().addVertex(COLLIDER);
        layout.setLocation(COLLIDER, startLocation);
        moveX = (endLocation.getX() - startLocation.getX()) / getMaximumIterations();
        moveY = (endLocation.getY() - startLocation.getY()) / getMaximumIterations();
    }

    @Override
    public void step() {
        layout.setLocation(COLLIDER, layout.getX(COLLIDER) + moveX, layout.getY(COLLIDER) + moveY);
        vv.repaint();
        setPrecision(Math.max(Math.abs(endLocation.getX() - layout.transform(COLLIDER).getX()),
            Math.abs(endLocation.getY() - layout.transform(COLLIDER).getY())));
        if (hasConverged()){
            layout.getGraph().removeVertex(COLLIDER);
        }
    }
}

例如,您可以使用以下代码实例化它:

    VertexCollider vtxCol = new VertexCollider(layout, vv, "nameOfVertexA", "nameOfVertexB");
    vtxCol.setMaximumIterations(100);
    vtxCol.setDesiredPrecision(1);
    vtxCol.initialize();
    Animator animator = new Animator(vtxCol);
    animator.start();

画直边:

代码示例

于 2014-12-31T09:41:58.253 回答