How do I initialize a new layout with vertex positions known beforehand?
I have created a custom JUNG layout class:
public class CustomLayout extends AbstractLayout {
AbstractLayout subLayout = null;
final int WIDTH = 500;
final int HEIGHT = 500;
public CustomLayout(Graph<Vertex, Edge> graph, Transformer<Vertex, Point2D> init) {
super(graph, init);
for (Vertex v : this.getGraph().getVertices()) {
// Assign each vertex a random initial position.
setLocation(v, new Point2D.Double(random * WIDTH, random * HEIGHT);
}
subLayout = new FRLayout(this.getGraph(), ...?, null);
// How do I pass each vertices prior positions?
}
}
That’s what the initializer (
Transformer<V,Point2D>) is for. This transformer should return the initial position of any vertex you pass into it. I see that FRLayout doesn’t accept an initializer in its constructor, but looks like you can callsetInitializer().In response to your question,
Transformeris a generic interface that, when given an input, produces a corresponding output. The implementation can be whatever you want. It could serve up a set of statically defined positions, or calculate positions on the fly. This gives you a lot of flexibility.You might notice that the interface bears a passing similarity to
Map<K,V>, and in fact a very simple wrapper implementation could be made as follows:So you could put your positions in a map and then wrap that map in the above
MapTransformer.