How to move JUNG nodes(vertices) by changing their location in the code and not by mouse?

838 views Asked by At

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.

1

There are 1 answers

1
Moh-Aw On BEST ANSWER

You can forcibly move a vertex with the setLocation method of the layout. I have built something that is quite similar to your request. It produces a vertex that moves from vertex A to vertex B in a direct line. If your edges are straight then it might work:

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);
        }
    }
}

You could instantiate this for example with this code:

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

Painting straight edges:

Code Example