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

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

I want to create some nodes that can move from one existing node to another node, using edgebetween the two nodes as the <strong> path (It will be used to show some Data Packetstransferred between nodes that look like Hosts).

There is information on the Internet on how to make JUNG nodes (vertices) moveable with the mouse, but there is no information on moving them to modifying values in the code.

Even if there is something to move the nodes, is it possible and efficient to move the node between nodes using the border between them as a moving path in the JUNG library ?

Any suggestions would be appreciated.

+4
source share
1 answer

You can force the vertex to be moved using the setLocationlayout method . I built something similar to your request. It creates a vertex that moves from vertex A to vertex B in a straight line. If your edges are straight, this 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 can create an instance of this example, for example, using 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

+1
source

Source: https://habr.com/ru/post/1568524/


All Articles